forked from garsh0p/garpr
-
Notifications
You must be signed in to change notification settings - Fork 14
/
serve_api.tac
54 lines (43 loc) · 1.84 KB
/
serve_api.tac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import os
import sys
import OpenSSL
from twisted.application import internet, service
from twisted.internet import reactor, ssl
from twisted.web.wsgi import WSGIResource
from twisted.web.server import Site
from config.config import Config
from ssl_util import CustomOpenSSLContextFactory
import server
config = Config()
ROOT_PATH = os.path.dirname(__file__)
class CustomOpenSSLContextFactory(ssl.DefaultOpenSSLContextFactory):
def __init__(self, privateKeyFileName, certificateChainFileName,
sslmethod=OpenSSL.SSL.SSLv23_METHOD):
"""
@param privateKeyFileName: Name of a file containing a private key
@param certificateChainFileName: Name of a file containing a certificate chain
@param sslmethod: The SSL method to use
"""
self.privateKeyFileName = privateKeyFileName
self.certificateChainFileName = certificateChainFileName
self.sslmethod = sslmethod
self.cacheContext()
def cacheContext(self):
ctx = OpenSSL.SSL.Context(self.sslmethod)
ctx.use_certificate_chain_file(self.certificateChainFileName)
ctx.use_privatekey_file(self.privateKeyFileName)
ctx.set_options(OpenSSL.SSL.OP_NO_SSLv2)
ctx.set_options(OpenSSL.SSL.OP_NO_SSLv3)
self._context = ctx
def getWebService():
key_path = config.get_ssl_key_path()
cert_path = config.get_ssl_cert_path()
ssl_context = CustomOpenSSLContextFactory(key_path, cert_path)
api_port = int(config.get_environment_api_port())
api_resource = WSGIResource(reactor, reactor.getThreadPool(), server.app)
api_server = Site(api_resource)
return internet.SSLServer(api_port, api_server, ssl_context)
application = service.Application("GARPR webapp")
# attach the service to its parent application
api_service = getWebService()
api_service.setServiceParent(application)