From 89d16bb05a61936658ae2939d57e2c3e5f726a6b Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Sat, 13 Jun 2020 22:51:08 +0800 Subject: Initial commit: MVP --- git-gmi/cgi.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100755 git-gmi/cgi.py (limited to 'git-gmi/cgi.py') diff --git a/git-gmi/cgi.py b/git-gmi/cgi.py new file mode 100755 index 0000000..c243ccd --- /dev/null +++ b/git-gmi/cgi.py @@ -0,0 +1,77 @@ +#!/home/fakefred/p/git.gmi/venv/bin/python3.8 +# gotta change the executable path before running +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): + # 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("Welcome to the git.gmi demo") + print("Available repositories:") + print("\n".join([f"=> {dir}/summary" 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: + pass # TODO: summary + + 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] + + if len(path_trace) == 3: + location = [] + else: + location = path_trace[3:] + + try: # is dir + print(repo.view_tree(branch, location)) + except FileNotFoundError: # is file + try: + print(repo.view_blob(branch, location)) + except FileNotFoundError: + print("50 Error locating content") + + elif view == "log": + try: + print(repo.view_log()) + return + except: + print(STATUS_TEMPORARY_FAILURE) + return + + +handle_cgi_request(environ.get("PATH_INFO")) -- cgit v1.2.3