From 96f3a7607ffbdb349a4c2eff35efdf11b8d35a4e Mon Sep 17 00:00:00 2001 From: tjpcc Date: Tue, 10 Jan 2023 13:46:35 -0700 Subject: Add a CGI contrib --- examples/cgi/main.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 examples/cgi/main.go (limited to 'examples/cgi/main.go') diff --git a/examples/cgi/main.go b/examples/cgi/main.go new file mode 100644 index 0000000..e784876 --- /dev/null +++ b/examples/cgi/main.go @@ -0,0 +1,49 @@ +package main + +import ( + "context" + "log" + "os" + + "tildegit.org/tjp/gus/contrib/cgi" + guslog "tildegit.org/tjp/gus/contrib/log" + "tildegit.org/tjp/gus/gemini" +) + +func main() { + // GET TLS files from the environment + certfile, keyfile := envConfig() + + // build a TLS configuration suitable for gemini + tlsconf, err := gemini.FileTLS(certfile, keyfile) + if err != nil { + log.Fatal(err) + } + + // make use of a CGI request handler + cgiHandler := cgi.CGIHandler("/cgi-bin", "cgi-bin") + + // add stdout logging to the request handler + handler := guslog.Requests(os.Stdout, nil)(cgiHandler) + + // run the server + server, err := gemini.NewServer(context.Background(), tlsconf, "tcp4", ":1965", handler) + if err != nil { + log.Fatal(err) + } + server.Serve() +} + +func envConfig() (string, string) { + certfile, ok := os.LookupEnv("SERVER_CERTIFICATE") + if !ok { + log.Fatal("missing SERVER_CERTIFICATE environment variable") + } + + keyfile, ok := os.LookupEnv("SERVER_PRIVATEKEY") + if !ok { + log.Fatal("missing SERVER_PRIVATEKEY environment variable") + } + + return certfile, keyfile +} -- cgit v1.2.3