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/cgi-bin/cowsay | 16 +++++++++++++++ examples/cgi/main.go | 49 +++++++++++++++++++++++++++++++++++++++++++++ examples/cowsay/main.go | 13 +++++------- examples/fileserver/main.go | 13 +++++------- examples/inspectls/main.go | 15 ++++++-------- 5 files changed, 81 insertions(+), 25 deletions(-) create mode 100755 examples/cgi/cgi-bin/cowsay create mode 100644 examples/cgi/main.go (limited to 'examples') diff --git a/examples/cgi/cgi-bin/cowsay b/examples/cgi/cgi-bin/cowsay new file mode 100755 index 0000000..e63e909 --- /dev/null +++ b/examples/cgi/cgi-bin/cowsay @@ -0,0 +1,16 @@ +#!/usr/bin/env sh + +set -euo pipefail + +if [ -z "$QUERY_STRING" ]; +then + printf "10 Enter a phrase.\r\n" + exit 0 +fi + +decodeURL() { printf "%b\n" "$(sed 's/+/ /g; s/%\([0-9a-fA-F][0-9a-fA-F]\)/\\x\1/g;')"; } + +printf "20 text/gemini\r\n\`\`\`\n" +echo $QUERY_STRING | decodeURL | cowsay +echo "\`\`\`" +echo "\n=> $SCRIPT_NAME again" 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 +} diff --git a/examples/cowsay/main.go b/examples/cowsay/main.go index e724421..be81f50 100644 --- a/examples/cowsay/main.go +++ b/examples/cowsay/main.go @@ -5,7 +5,6 @@ import ( "context" "io" "log" - "net" "os" "os/exec" @@ -23,17 +22,15 @@ func main() { log.Fatal(err) } - // set up the network listener - listener, err := net.Listen("tcp4", ":1965") - if err != nil { - log.Fatal(err) - } - // add request logging to the request handler handler := guslog.Requests(os.Stdout, nil)(cowsayHandler) // run the server - gemini.NewServer(context.Background(), tlsconf, listener, handler).Serve() + server, err := gemini.NewServer(context.Background(), tlsconf, "tcp4", ":1965", handler) + if err != nil { + log.Fatal(err) + } + server.Serve() } func cowsayHandler(ctx context.Context, req *gemini.Request) *gemini.Response { diff --git a/examples/fileserver/main.go b/examples/fileserver/main.go index 01d22ee..d7f628b 100644 --- a/examples/fileserver/main.go +++ b/examples/fileserver/main.go @@ -3,7 +3,6 @@ package main import ( "context" "log" - "net" "os" "tildegit.org/tjp/gus/contrib/fs" @@ -21,12 +20,6 @@ func main() { log.Fatal(err) } - // set up the network listen - listener, err := net.Listen("tcp4", ":1965") - if err != nil { - log.Fatal(err) - } - // build the request handler fileSystem := os.DirFS(".") // Fallthrough tries each handler in succession until it gets something other than "51 Not Found" @@ -42,7 +35,11 @@ func main() { handler = guslog.Requests(os.Stdout, nil)(handler) // run the server - gemini.NewServer(context.Background(), tlsconf, listener, handler).Serve() + server, err := gemini.NewServer(context.Background(), tlsconf, "tcp4", ":1965", handler) + if err != nil { + log.Fatal(err) + } + server.Serve() } func envConfig() (string, string) { diff --git a/examples/inspectls/main.go b/examples/inspectls/main.go index a315e40..d690af1 100644 --- a/examples/inspectls/main.go +++ b/examples/inspectls/main.go @@ -9,12 +9,11 @@ import ( "encoding/hex" "fmt" "log" - "net" "os" "strings" - "tildegit.org/tjp/gus/gemini" guslog "tildegit.org/tjp/gus/contrib/log" + "tildegit.org/tjp/gus/gemini" ) func main() { @@ -27,17 +26,15 @@ func main() { log.Fatal(err) } - // set up the network listener - listener, err := net.Listen("tcp4", ":1965") - if err != nil { - log.Fatal(err) - } - // add stdout logging to the request handler handler := guslog.Requests(os.Stdout, nil)(inspectHandler) // run the server - gemini.NewServer(context.Background(), tlsconf, listener, handler).Serve() + server, err := gemini.NewServer(context.Background(), tlsconf, "tcp4", ":1965", handler) + if err != nil { + log.Fatal(err) + } + server.Serve() } func envConfig() (string, string) { -- cgit v1.2.3