From 04145b89c48ad37e7c8fa6e1cf0a83fa7d0e395c Mon Sep 17 00:00:00 2001 From: Dita Aji Pratama Date: Mon, 30 Jan 2023 20:09:40 +0700 Subject: [PATCH] Support CORS policy for create a JSON API, Use library for JWT, and adding custom error page --- config/server.py | 17 ++++++++++++----- costa.py | 2 ++ handler.py | 2 ++ install.sh | 2 ++ static/error/403.html | 1 + static/error/404.html | 1 + static/error/500.html | 1 + 7 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 static/error/403.html create mode 100644 static/error/404.html create mode 100644 static/error/500.html diff --git a/config/server.py b/config/server.py index 6ba7126..4d11f43 100644 --- a/config/server.py +++ b/config/server.py @@ -1,7 +1,14 @@ +def erpadir(err): + return f'static/error/{err}.html' + update = { - 'server.socket_host' : "hostname" , - 'server.socket_port' : "port" , - 'tools.sessions.on' : True , - 'engine.autoreload.on' : False , - 'request.show_tracebacks' : False , + 'server.socket_host' : "hostname" , + 'server.socket_port' : "port" , + 'cors.expose.on' : True , + 'tools.sessions.on' : True , + 'engine.autoreload.on' : False , + 'request.show_tracebacks' : False , + 'error_page.403' : erpadir(403) , + 'error_page.404' : erpadir(404) , + 'error_page.500' : erpadir(500) , } diff --git a/costa.py b/costa.py index 4deec72..1398eb3 100644 --- a/costa.py +++ b/costa.py @@ -1,5 +1,6 @@ import sys import cherrypy +import cherrypy_cors import handler from config import server @@ -15,6 +16,7 @@ if __name__ == '__main__': update["server.socket_host"] = sys.argv[1] update["server.socket_port"] = int(sys.argv[2]) + cherrypy_cors.install() cherrypy.config.update ( update ) cherrypy.quickstart ( handler.handler(), config = dirconfig ) diff --git a/handler.py b/handler.py index 9712349..3183794 100755 --- a/handler.py +++ b/handler.py @@ -1,4 +1,5 @@ import cherrypy +import cherrypy_cors import json import core.authentication as authentication @@ -8,6 +9,7 @@ import config.template as pages import modules.user.home as user_home +@cherrypy.tools.accept(media="application/json") class handler(pages.main): def __init__(self): diff --git a/install.sh b/install.sh index 66da380..edc60b7 100644 --- a/install.sh +++ b/install.sh @@ -1,6 +1,8 @@ sudo apt-get install -y python3-pip pip3 install --upgrade pip pip3 install cherrypy +pip3 install cherrypy-cors pip3 install mako pip3 install mysql-connector pip3 install bcrypt +pip3 install pyjwt[crypto] diff --git a/static/error/403.html b/static/error/403.html new file mode 100644 index 0000000..ff6968d --- /dev/null +++ b/static/error/403.html @@ -0,0 +1 @@ +403 Forbidden diff --git a/static/error/404.html b/static/error/404.html new file mode 100644 index 0000000..beef265 --- /dev/null +++ b/static/error/404.html @@ -0,0 +1 @@ +404 Not found diff --git a/static/error/500.html b/static/error/500.html new file mode 100644 index 0000000..0372581 --- /dev/null +++ b/static/error/500.html @@ -0,0 +1 @@ +500 Internal server error