gemini.git

going-flying.com gemini git repository

summary

tree

log

refs

164c175537d4f883a5060892fb554e544c4c4485 - Matthew Ernisse - 1614644038

add features to gmicgi

view tree

view raw

diff --git a/cgi-bin/example.py b/cgi-bin/example.py
index c73a903..9b9076b 100755
--- a/cgi-bin/example.py
+++ b/cgi-bin/example.py
@@ -3,8 +3,14 @@
 import os
 import sys
 
-if __name__ == '__main__':
-	print('20 text/gemini\r\n')
+from gmicgi import GeminiCGI
+
+cgi = GeminiCGI()
+
+@cgi.route('')
+def default():
+	''' This should be the default route.'''
+	cgi.Response.Ok('text/gemini')
 	print('''
 
 This is a CGI test.  Below enumerates the environment passed by the server to the script.
diff --git a/cgi-bin/gmicgi/__init__.py b/cgi-bin/gmicgi/__init__.py
index 1bb85c1..0f7408b 100755
--- a/cgi-bin/gmicgi/__init__.py
+++ b/cgi-bin/gmicgi/__init__.py
@@ -95,7 +95,16 @@ class GeminiCGI(object):
 			return cls(30, meta)
 
 	def __init__(self):
+		''' Setup an instance of the GeminiCGI processor, which will
+		process the PATH_INFO and QUERY_STRING environment variables.
+
+		It also will walk through all the functions that were decorated
+		with the route decorator and if they match the current
+		PATH_INFO, it will call them.  You are of course free to not
+		use the routing though.
+		'''
 		self.path_info = os.environ.get('PATH_INFO')
+		self.routes = {}
 		self.script_path = os.environ.get('SCRIPT_PATH')
 		self.query_string = os.environ.get('QUERY_STRING').strip()
 
@@ -105,3 +114,18 @@ class GeminiCGI(object):
 		if self.query_string:
 			self.query_dequoted = unquote(self.query_string)
 			self.query_parsed = parse_qs(self.query_string)
+
+		if self.path_info in self.routes.keys():
+			for func in self.routes[self.path_info]:
+				func()
+
+	def route(self, path):
+		''' Decorator to create a route.'''
+		def decorator(f):
+			if path not in self.routes.keys():
+				self.routes[path] = [f]
+			else:
+				self.routes[path].append(f)
+			return f
+
+		return decorator