< ~wiki

gemini cgi guide

where to put CGI scripts

you can put CGI scripts anywhere. as long as you make the file executable and have it output the correct format for gemini. the server calls a new instance of the program for every request it has to handle, meaning that data isn't saved between runs unless you manually save the data to a file or database and load the data on the next run.

what languages can be used to write CGI

any language that can read environment variables can be used for CGI. even shell scripting can be used for CGI, given you provide the correct shebang.

how to write a gemini CGI script

these are the environment variables gemserv (our trusty gemini server) gives a CGI program:

also, if the user supplies a cert:

in order to write a CGI program, simply use these environment variables to figure out what you want to do and then do it. for a simple hello world:

printf '20 text/gemini\r\n'
printf 'Hello world from CGI!\r\n'

do note that the first line MUST use \r\n as a terminator. after that, if you're serving gemtext, you can use whatever LF/CRLF you want for line endings as long as you stay consistent.

for a more complex hello world that asks for input, here's a python script that asks for your name and says hello to you:

from urllib.parse import unquote
from os import environ
from sys import exit

if "QUERY_STRING" not in environ:
    print("10 Please enter your name",end="\r\n")
name = unquote(environ["QUERY_STRING"])

print("20 text/gemini",end="\r\n")
print(f"Hello {name}!")

remember, anything that can read environment variables and write to stdout can be used to make CGI, as long as the server can figure out how to execute it.