1
1
import quart_flask_patch
2
2
import logging , os , asyncio , json
3
+ from datetime import date , datetime , timedelta , timezone
3
4
from urllib import parse
4
5
from hypercorn .config import Config
5
- from quart import Quart , request
6
+ from hypercorn .middleware import HTTPToHTTPSRedirectMiddleware
7
+ from quart import Quart , Response
6
8
from flask_healthz import Healthz , HealthError
7
9
from quart_wtf .csrf import CSRFProtect
8
10
from quart_cors import cors
20
22
logging .basicConfig (format = '%(asctime)s %(levelname)-8s %(message)s' , level = logging .INFO , datefmt = '%Y-%m-%d %H:%M:%S' )
21
23
#oidc.init_app(app)
22
24
25
+ def _add_secure_headers (response : Response ) -> Response :
26
+ response .headers ["Strict-Transport-Security" ] = (
27
+ "max-age=63072000; includeSubDomains; preload"
28
+ )
29
+ response .headers ["X-Content-Type-Options" ] = "nosniff"
30
+ return response
31
+
23
32
def create_app () -> Quart :
24
33
"""
25
34
Create App
26
35
"""
27
36
# App initialization
28
37
app = Quart (__name__ , template_folder = 'view/templates' , static_url_path = '' , static_folder = 'view/static' )
29
38
app .config .from_file ("/etc/pythonrestapi_config.json" , json .load )
39
+ app .config ["SEND_FILE_MAX_AGE_DEFAULT" ] = timedelta (days = 90 )
30
40
app .config ["SQLALCHEMY_DATABASE_URI" ] = f"postgresql+psycopg://{ os .environ .get ('DB_USERNAME' )} :{ parse .quote (os .environ .get ('DB_PASSWORD' ))} @{ app .config ['DB_HOST' ]} /library"
31
41
app .config ["POSTGRESQL_DATABASE_URI" ] = f"postgresql://{ os .environ .get ('DB_USERNAME' )} :{ parse .quote (os .environ .get ('DB_PASSWORD' ))} @{ app .config ['DB_HOST' ]} /library"
42
+ app .after_request (_add_secure_headers )
32
43
app .register_blueprint (home_blueprint , url_prefix = "/" )
33
44
app .register_blueprint (fibonacci_blueprint , url_prefix = "/fibonacci" )
34
45
app .register_blueprint (auth_blueprint , url_prefix = "/auth" )
@@ -41,6 +52,10 @@ def create_app() -> Quart:
41
52
csrf = CSRFProtect (app )
42
53
bcrypt .init_app (app )
43
54
db .init_app (app )
55
+ if app .debug :
56
+ return HTTPToHTTPSRedirectMiddleware (app , "khteh.com" ) # type: ignore - Defined in hypercorn.toml server_names
57
+ else :
58
+ app .config ["TEMPLATES_AUTO_RELOAD" ] = True
44
59
return app
45
60
46
61
def liveness ():
0 commit comments