From db5bbc64052a187ab980ce0e7a0334d294df33d0 Mon Sep 17 00:00:00 2001 From: ditaajipratama Date: Sun, 12 May 2024 02:18:32 +0700 Subject: [PATCH] New branch: Bottle --- config/__init__.py | 0 config/database.py | 2 +- config/directory.py | 33 +++++------------- config/globalvar.py | 6 ++-- config/server.py | 30 +++++++--------- core/__init__.py | 0 core/staticdir.py | 9 +++++ core/{templatestaticdir.py => template.py} | 5 ++- core/uploading.py | 40 ---------------------- costa.py | 35 ++++++++----------- handler.py | 25 +++++++------- install.sh | 8 ++--- modules/__init__.py | 0 modules/public/__init__.py | 0 modules/public/home.py | 4 +-- page/error/403.html | 1 - page/error/404.html | 1 - page/error/500.html | 1 - {page => pages}/public/home.html | 0 scripts/__init__.py | 0 scripts/bodyfetcher.py | 13 ------- scripts/loggorilla.py | 13 ------- scripts/sendwave.py | 23 ------------- scripts/tokenguard.py | 23 ------------- templates/bare/main.py | 20 +++-------- templates/bare/static/css/style.css | 1 - 26 files changed, 72 insertions(+), 221 deletions(-) delete mode 100644 config/__init__.py mode change 100755 => 100644 config/database.py mode change 100755 => 100644 config/globalvar.py delete mode 100644 core/__init__.py create mode 100644 core/staticdir.py rename core/{templatestaticdir.py => template.py} (53%) delete mode 100644 core/uploading.py mode change 100755 => 100644 handler.py delete mode 100644 modules/__init__.py delete mode 100644 modules/public/__init__.py delete mode 100644 page/error/403.html delete mode 100644 page/error/404.html delete mode 100644 page/error/500.html rename {page => pages}/public/home.html (100%) delete mode 100644 scripts/__init__.py delete mode 100644 scripts/bodyfetcher.py delete mode 100644 scripts/loggorilla.py delete mode 100644 scripts/sendwave.py delete mode 100644 scripts/tokenguard.py delete mode 100644 templates/bare/static/css/style.css diff --git a/config/__init__.py b/config/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/config/database.py b/config/database.py old mode 100755 new mode 100644 index 11623eb..4291974 --- a/config/database.py +++ b/config/database.py @@ -1,4 +1,4 @@ -main_db = { +db_main = { 'host' : 'localhost', 'user' : 'root', 'password' : '', diff --git a/config/directory.py b/config/directory.py index 550c47a..366d6c2 100644 --- a/config/directory.py +++ b/config/directory.py @@ -1,30 +1,13 @@ -import os -from core import templatestaticdir +from core import template -# pages directory page = { - 'public' :'page/public' , - 'error' :'page/error' # Non-template + 'public' :'pages/public' } -# public staticdir -dirconfig = { - '/' : +static = [ { - 'tools.sessions.on' : True , - 'tools.staticdir.root' : os.path.abspath(os.getcwd()) , - }, - '/css' : - { - 'tools.staticdir.on' : True , - 'tools.staticdir.dir' : './static/css' , - }, - '/js' : - { - 'tools.staticdir.on' : True , - 'tools.staticdir.dir' : './static/js' , - }, -} - -# template staticdir: dirconfig dirtemplate -templatestaticdir.add(dirconfig, "templates") + "route" :"/css/", + "root" :"./static/css" + } +] +template.add(static, "templates") diff --git a/config/globalvar.py b/config/globalvar.py old mode 100755 new mode 100644 index c46ffbe..b022fb2 --- a/config/globalvar.py +++ b/config/globalvar.py @@ -1,4 +1,4 @@ -baseurl = "http://localhost:81" +baseurl = "http://127.0.0.1:15001" title = "CostaPy" menu = { @@ -10,13 +10,13 @@ menu = { "roles":["guest"] }, { - "name":"About", + "name":"Profile", "href":"#", "roles":["guest"] }, { "name":"CostaPy Website", - "href":"https://costapy.ditaajipratama.com", + "href":"https://costapy.ditaajipratama.net", "roles":["guest"] } ] diff --git a/config/server.py b/config/server.py index d8a1d1f..5830cfc 100644 --- a/config/server.py +++ b/config/server.py @@ -1,20 +1,16 @@ -from config import directory +host = "localhost" +port = 15001 +reloader = False +debug = False +server = 'gunicorn' # default = 'wsgiref' -update = { - 'server.socket_host' : "hostname" , - 'server.socket_port' : "port" , +# cors +# session - 'cors.expose.on' : True , - 'tools.sessions.on' : True , +# error page 403 +# error page 404 +# error page 500 - 'engine.autoreload.on' : False , - 'request.show_tracebacks' : False , - - 'error_page.403' : f'{directory.page["error"]}/403.html' , - 'error_page.404' : f'{directory.page["error"]}/404.html' , - 'error_page.500' : f'{directory.page["error"]}/500.html' , - - 'server.max_request_body_size' : 800 * 1024 * 1024 , # 800MB; Default 100MB - 'server.socket_timeout' : 60 , # Default 10s - 'response.timeout' : 3600 , # Default 300s -} +# max_request_body_size = 800 * 1024 * 1024 , # Multiply for 800MB result +# socket timeout = 60 +# response timeout = 3600 diff --git a/core/__init__.py b/core/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/core/staticdir.py b/core/staticdir.py new file mode 100644 index 0000000..487dffd --- /dev/null +++ b/core/staticdir.py @@ -0,0 +1,9 @@ +from bottle import Bottle, get, static_file +from config import directory + +app = Bottle() + +for items in directory.static: + @app.get(items['route']) + def static_items(filepath): + return static_file(filepath, root=items['root']) diff --git a/core/templatestaticdir.py b/core/template.py similarity index 53% rename from core/templatestaticdir.py rename to core/template.py index 9327389..9a0663a 100644 --- a/core/templatestaticdir.py +++ b/core/template.py @@ -1,9 +1,8 @@ import os def add(dirconfig, template_directory): - template_directory = "templates" - template_list = [d for d in os.listdir(template_directory) if os.path.isdir(os.path.join(template_directory, d))] + template_list = [d for d in os.listdir(template_directory) if os.path.isdir(os.path.join(template_directory, d))] for template_name in template_list: template_module = __import__(f"{template_directory}.{template_name}.main", fromlist=["static"]) for static in getattr(template_module, "static", []): - dirconfig[ static['name'] ] = static['value'] + dirconfig.append(static) diff --git a/core/uploading.py b/core/uploading.py deleted file mode 100644 index 2330a8a..0000000 --- a/core/uploading.py +++ /dev/null @@ -1,40 +0,0 @@ -import os -import cherrypy - -def main(file, rename, directory): - try: - os.makedirs(directory, exist_ok=True) - except OSError as error: - print(error) - try: - upload_path = directory - upload_filename = file.filename - upload_rename = rename - upload_file = os.path.normpath(os.path.join(upload_path, upload_rename)) - upload_size = 0 - - print("UPLOADING CORE: Directory: "+directory+"/"+rename) - if (os.path.isfile(directory+"/"+rename)): - print("UPLOADING CORE: Is exists! Removing!") - try: - os.remove(directory+"/"+rename) - except Exception as e: - print(f"UPLOADING CORE: removing failed: {e}") - else: - print("UPLOADING CORE: Is not exists! Removing skipped!") - with open(upload_file, 'wb') as upload_result: - while True: - data = file.file.read(8192) - if not data: - break - upload_result.write(data) - upload_size += len(data) - - print("UPLOAD PATH: " + str(upload_path)) - print("UPLOAD FILENAME: " + str(upload_filename)) - print("UPLOAD RENAME: " + str(upload_rename)) - print("UPLOAD FILE: " + str(upload_file)) - print("UPLOAD SIZE: " + str(upload_size)) - - except Exception as e: - print(f"ERROR CORE UPLOADING: {e}") diff --git a/costa.py b/costa.py index 1398eb3..32c9b57 100644 --- a/costa.py +++ b/costa.py @@ -1,25 +1,20 @@ -import sys -import cherrypy -import cherrypy_cors -import handler +import sys +from bottle import Bottle, run -from config import server -from config import directory +import handler -if __name__ == '__main__': +from core import staticdir +from config import server - dirconfig = directory.dirconfig - update = server.update +app = Bottle() - if len(sys.argv) >= 3: +app.merge(handler.app) +app.merge(staticdir.app) - 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 ) - - else: - print ("Usage : python costa.py ") - print ("Example : python3 costa.py localhost 81 CostaPySample") +run(app, + host = server.host, + port = server.port, + reloader = server.reloader, + server = server.server, + debug = server.debug +) diff --git a/handler.py b/handler.py old mode 100755 new mode 100644 index a0cb5f5..6a77593 --- a/handler.py +++ b/handler.py @@ -1,17 +1,16 @@ -import cherrypy -import json -import config.directory as directory +from bottle import Bottle, route +from config import directory -import templates.bare.main as bare +import templates.bare.main as template_public +import modules.public.home as public_home -import modules.public.home as public_home +app = Bottle() -@cherrypy.tools.accept(media="application/json") -class handler(): - - def index(self, **kwargs): - kwargs["mako"] = { - "website" : bare.main(directory.page["public"], "home") +@app.route('/') +def index(): + params = { + "mako":{ + "website" : template_public.main(directory.page["public"], "home") } - return public_home.main().html(kwargs) - index.exposed = True + } + return public_home.main().html(params) diff --git a/install.sh b/install.sh index 2437183..8c299c8 100644 --- a/install.sh +++ b/install.sh @@ -1,8 +1,4 @@ sudo apt-get install -y python3-pip pip install --upgrade pip -pip install cherrypy -pip install cherrypy-cors -pip install mako -pip install mysql-connector -pip install bcrypt -pip install pyjwt[crypto] +pip install bottle +pip install gunicorn diff --git a/modules/__init__.py b/modules/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/modules/public/__init__.py b/modules/public/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/modules/public/home.py b/modules/public/home.py index 51a4639..40d6474 100644 --- a/modules/public/home.py +++ b/modules/public/home.py @@ -1,5 +1,5 @@ -from mako.template import Template -import config.globalvar as globalvar +from mako.template import Template +from config import globalvar class main: diff --git a/page/error/403.html b/page/error/403.html deleted file mode 100644 index ff6968d..0000000 --- a/page/error/403.html +++ /dev/null @@ -1 +0,0 @@ -403 Forbidden diff --git a/page/error/404.html b/page/error/404.html deleted file mode 100644 index beef265..0000000 --- a/page/error/404.html +++ /dev/null @@ -1 +0,0 @@ -404 Not found diff --git a/page/error/500.html b/page/error/500.html deleted file mode 100644 index 0372581..0000000 --- a/page/error/500.html +++ /dev/null @@ -1 +0,0 @@ -500 Internal server error diff --git a/page/public/home.html b/pages/public/home.html similarity index 100% rename from page/public/home.html rename to pages/public/home.html diff --git a/scripts/__init__.py b/scripts/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/scripts/bodyfetcher.py b/scripts/bodyfetcher.py deleted file mode 100644 index aaa167c..0000000 --- a/scripts/bodyfetcher.py +++ /dev/null @@ -1,13 +0,0 @@ -import cherrypy -import cherrypy_cors -import json - -def body_json(): - result = None - if cherrypy.request.method == 'OPTIONS': - cherrypy_cors.preflight(allowed_methods=['GET', 'POST']) - if cherrypy.request.method == 'POST': - cherrypy.serving.response.headers['Content-Type'] = 'application/json' - body_request = cherrypy.request.body.read() - result = json.loads(body_request.decode()) - return result diff --git a/scripts/loggorilla.py b/scripts/loggorilla.py deleted file mode 100644 index 762b1fe..0000000 --- a/scripts/loggorilla.py +++ /dev/null @@ -1,13 +0,0 @@ -import datetime - -def prcss(loc, msg): - print(f"[loggorilla][{datetime.datetime.now()}][\033[32mprcss\033[39m][\033[95m{loc}\033[39m] {msg}") - -def accss(loc, msg): - print(f"[loggorilla][{datetime.datetime.now()}][\033[36maccss\033[39m][\033[95m{loc}\033[39m] {msg}") - -def fyinf(loc, msg): - print(f"[loggorilla][{datetime.datetime.now()}][\033[93mfyinf\033[39m][\033[95m{loc}\033[39m] {msg}") - -def error(loc, msg): - print(f"[loggorilla][{datetime.datetime.now()}][\033[31merror\033[39m][\033[95m{loc}\033[39m] {msg}") diff --git a/scripts/sendwave.py b/scripts/sendwave.py deleted file mode 100644 index 452424e..0000000 --- a/scripts/sendwave.py +++ /dev/null @@ -1,23 +0,0 @@ -from email.mime.multipart import MIMEMultipart -from email.mime.text import MIMEText -import smtplib - -def smtp(config): - - msg = MIMEMultipart('alternative') - msg['Subject' ] = config['subject' ] - msg['From' ] = config['from' ] - msg['To' ] = config['to' ] - - part1 = MIMEText(config['text'], 'plain') - part2 = MIMEText(config['html'], 'html' ) - - msg.attach(part1) - msg.attach(part2) - - smtp_server = smtplib.SMTP(config['server']['host'], config['server']['port']) - smtp_server.ehlo() - smtp_server.starttls() - smtp_server.login( config['login']['email'], config['login']['password'] ) - smtp_server.sendmail('&&&&&&', config['to'], msg.as_string() ) - smtp_server.quit() diff --git a/scripts/tokenguard.py b/scripts/tokenguard.py deleted file mode 100644 index b2d6ef6..0000000 --- a/scripts/tokenguard.py +++ /dev/null @@ -1,23 +0,0 @@ -from cryptography.hazmat.primitives import serialization -import jwt - -def encode(payload, id_rsa, passphrase): - private_key = open(id_rsa, 'r').read() - key = serialization.load_ssh_private_key(private_key.encode(), password=passphrase) - token = jwt.encode( - payload = payload, - key = key, - algorithm = 'RS256' - ) - return token - -def decode(token, id_rsa): - public_key = open(id_rsa, 'r').read() - key = serialization.load_ssh_public_key(public_key.encode()) - header = jwt.get_unverified_header(token) - payload = jwt.decode( - jwt = token, - key = key, - algorithms = [header['alg'], ] - ) - return payload diff --git a/templates/bare/main.py b/templates/bare/main.py index 4da79af..f5dcec0 100644 --- a/templates/bare/main.py +++ b/templates/bare/main.py @@ -2,18 +2,8 @@ from core import html static = [ { - 'name':'/bare/lib', - 'value':{ - 'tools.staticdir.on' : True , - 'tools.staticdir.dir' : './templates/bare/static/lib' , - } - }, - { - 'name':'/bare/css', - 'value':{ - 'tools.staticdir.on' : True , - 'tools.staticdir.dir' : './templates/bare/static/css' , - } + "route" :"/bare/lib/", + "root" :"./templates/bare/static/lib" } ] @@ -22,9 +12,9 @@ def main(dir, page): html_template = html.main.get_html("templates/bare/html") html_page = html.main.get_html(dir) params_list = { - "template" : html_template ["template.html" ] , - "topnav" : html_template ["topnav.html" ] , - "footer" : html_template ["footer.html" ] , + "template" : html_template ["template.html" ], + "topnav" : html_template ["topnav.html" ], + "footer" : html_template ["footer.html" ], "container" : html_page [ page+".html" ] } return params_list diff --git a/templates/bare/static/css/style.css b/templates/bare/static/css/style.css deleted file mode 100644 index ce7b74b..0000000 --- a/templates/bare/static/css/style.css +++ /dev/null @@ -1 +0,0 @@ -/* your style here */