-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
158 lines (124 loc) · 4.18 KB
/
app.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from common.config import config, getDate, getTime
from common.controller import Controller
from common.report import Report
from common.reportViewer import Viewer
from controllers.container import Container
from controllers.reportController import ReportController
from controllers.static import Static
from controllers.system import System
from models.models import Session
__author__ = 'Siamand'
import os
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import urlparse
# Classes
class AuthControler:
Authenticated = False
SessionObject = None
Token = 'NONE'
def __init__(self, router):
self.router = router
self.request = router.request
token = self.router.getCookie('token')
if (token != None):
try:
session = Session.get((Session.token == token) & (Session.status == 1))
self.Authenticated = True
self.SessionObject = session
self.Token = session.token
except:
self.Authenticated = False
self.SessionObject = None
self.Token = 'NONE'
pass
class MainRouter(Controller):
def __init__(self, reuqest):
self.request = reuqest
self.authentication = AuthControler(self)
pass
def Route(self):
path = self.request.path
parsed_path = urlparse(path)
full_path = parsed_path.path
spath = full_path.split('/', 2)
fpath = 'empty'
if len(spath) > 2:
fpath = spath[2]
section = spath[1]
mdl = Container()
if (section == ''):
mdl = Container()
elif (section == 'static'):
mdl = Static()
elif (section == 'system'):
mdl = System()
elif (self.authentication.Authenticated):
if(section=='report'):
mdl = Report
elif(section=='viewer'):
mdl=Viewer
elif (section=='template'):
mdl = ReportController
else:
mdl = Controller.controllers[section]
if mdl is None:
mdl = Static()
fpath = 'security.htm'
else:
mdl = mdl()
mdl.router = self
mdl.query = parsed_path.query
mdl.request = self.request
mdl.authentication = self.authentication
mdl.Process(fpath)
return mdl
class RequestHandler(BaseHTTPRequestHandler):
'''
classdocs
'''
def do_HEAD(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
ProcessRequeset(self)
def do_POST(self):
ProcessRequeset(self)
def ProcessRequeset(req):
template = MainRouter(req).Route()
template.Render()
req.send_response(template.statuscode)
req.send_header("Content-type", template.mimetype)
if len(template.cookies) > 0:
# cookies_output = template.cookies.output(header='')
cookies_output = ""
for key in template.cookies:
cookies_output += ("%s=%s;Path=/;" % (key, template.cookies[key]))
req.send_header('Set-Cookie', cookies_output)
req.end_headers()
fout = template.output
if isinstance(fout, str):
fout = bytes(fout.encode('utf-8'))
req.wfile.write(fout)
if __name__ == '__main__':
# global _
_ = config.i18n
PORT = int(os.getenv('PORT', '8000'))
HOST = os.getenv('HOST', 'localhost')
httpd = HTTPServer((HOST, PORT), RequestHandler)
# config.makeTempLang(Menu.select())
# DbSetup().SetupDataBase()
# DbSetup().BackUp()
IS_TEST = os.getenv('BUILD_TEST')
if IS_TEST != 'TEST':
try:
print (_('Samal web application server'))
print (_('Starting server at') + ' http://%s:%s' % (HOST, PORT))
print ('> %s - %s' % (getDate(), getTime()))
httpd.serve_forever()
httpd.server_close()
print (_('End server listening'))
except KeyboardInterrupt:
print (_('Server is Stopped'))
print ('> %s - %s' % (getDate(), getTime()))
pass