diff options
author | Frederick Yin <fkfd@macaw.me> | 2020-06-14 18:03:01 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@macaw.me> | 2020-06-14 18:03:01 +0800 |
commit | 59c2e948797d088e795cc2290fa8c3004ddb71c1 (patch) | |
tree | 472fdeb60b9431efb9b6f75cdee03efe3e69765a /git-gmi/gateway.py | |
parent | ee8f25fd963dec48b31ff4072afd2c95f24d518e (diff) |
Massive improvements
- Python CGI server script renamed to gateway.py to avoid confusion
- Repo not found error handling
- Page header & navs
- Fix faulty 20 response header
- View (non-)raw blob
Diffstat (limited to 'git-gmi/gateway.py')
-rw-r--r-- | git-gmi/gateway.py | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/git-gmi/gateway.py b/git-gmi/gateway.py new file mode 100644 index 0000000..0ae963f --- /dev/null +++ b/git-gmi/gateway.py @@ -0,0 +1,76 @@ +from git import * +from const import * +from os import environ, listdir + +# be careful when using print(); stdout is passed to the client. +# this cgi uses \n as newline. + + +def generate_navigation(repo_name: str): + pass # TODO + + +def handle_cgi_request(path: str, query: str): + # intended to work with Jetforce. + # url: gemini://git.gemini.site/cgi-bin/cgi.py/repo/src/static/css/[index.css] + # path: /repo/src/static/css/[index.css] + # path_trace = ['repo', 'src', 'static', 'css', 'index.css'] + path_trace = path[1:].split("/") + if path_trace == [""]: # empty path + print(f"{STATUS_SUCCESS} {META_GEMINI}") # welcome page + print(f"# Welcome to {GIT_GMI_SITE_TITLE}") + print("## Available repositories:") + print("\n".join([f"=> {dir}/" for dir in listdir(GIT_CATALOG)])) + return + + try: + repo = GitGmiRepo(path_trace[0], f"{GIT_CATALOG}/{path_trace[0]}") + except FileNotFoundError: + print(STATUS_NOT_FOUND) + return + + if len(path_trace) > 1: + view = path_trace[1] # e.g. summary, tree, log + else: + print("31 summary") + return + + if view == "summary": + try: + print(repo.view_summary()) + return + except: + print(STATUS_TEMPORARY_FAILURE) + return + + elif view == "tree": + if len(path_trace) == 2: + print("31 master/") + return + + if len(path_trace) > 2: + branch = path_trace[2] + + location = path_trace[3:] + + try: # is dir + print(repo.view_tree(branch, location)) + except FileNotFoundError: # is file + try: + if query == "raw": + print(repo.view_raw_blob(branch, location)) + else: + print(repo.view_blob(branch, location)) + except FileNotFoundError: + print(STATUS_NOT_FOUND) + + elif view == "log": + try: + print(repo.view_log()) + return + except: + print(STATUS_TEMPORARY_FAILURE) + return + + +handle_cgi_request(environ.get("PATH_INFO"), environ.get("QUERY_STRING")) |