From d4bd6e92a5fc05f3931bde7566da445861ea38f4 Mon Sep 17 00:00:00 2001 From: shokara Date: Sat, 25 Dec 2021 03:42:55 +0000 Subject: [PATCH] use environment variables instead of hardcoding values git-svn-id: file:///srv/svn/repo/tokiko/trunk@11 8f5ca974-a7f8-e144-9f80-d41d5039c194 --- .gitignore | 2 -- Gophermap | 4 ++++ README | 7 +++++++ main.go | 55 +++++++++++++++++++++++++++++++++++++++++++----------- 4 files changed, 55 insertions(+), 13 deletions(-) delete mode 100644 .gitignore create mode 100644 Gophermap diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 4026300..0000000 --- a/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -Gophermap -test-gopher diff --git a/Gophermap b/Gophermap new file mode 100644 index 0000000..43bf7b5 --- /dev/null +++ b/Gophermap @@ -0,0 +1,4 @@ +iIf you see this, then thomomys is probably running! If you are the +isysadmin running thomomys and were expecting something else, try +isetting the GOPHER_DIR environment variable to a directory containing +ithe gopherhole. See the README for more details. diff --git a/README b/README index 02d6bef..24dc037 100644 --- a/README +++ b/README @@ -1,3 +1,10 @@ Thomomys ======== A simple gopher daemon written in Golang. + +Environment Variables +--------------------- +GOPHER_IP = IP address for Thomomys to listen on +GOPHER_PORT = Port for Thomomys to listen on +GOPHER_HOSTNAME = Default hostname that gets printed for links +GOPHER_DIR = Root directory that gets served diff --git a/main.go b/main.go index de813ff..2cd7398 100644 --- a/main.go +++ b/main.go @@ -10,10 +10,12 @@ import ( "strings" ) -const HOST = "0.0.0.0" -const PORT = "7070" -const HOSTNAME = "demiurge.shoko.home" -const DIR = "/home/kou/src/gopherhole" +var ( + ADDR_IP string + ADDR_PORT string + HOSTNAME string + SRVDIR string +) func formatLine(line string) string { trimmed := strings.TrimRight(line, "\r\n") @@ -22,9 +24,9 @@ func formatLine(line string) string { if len(splitted) == 3 { return line } else if len(splitted) == 2 { - line += "\t" + HOSTNAME + "\t" + PORT + line += "\t" + HOSTNAME + "\t" + ADDR_PORT } else if len(splitted) == 1 { - line += "\tErr\t" + HOSTNAME + "\t" + PORT + line += "\tErr\t" + HOSTNAME + "\t" + ADDR_PORT } return line + "\n" @@ -66,7 +68,6 @@ func printFile(c net.Conn, path string) { } }() - // 1024 bytes or 1K for buffer const bufSz = 1024 b := make([]byte, bufSz) @@ -119,12 +120,44 @@ func connHandle(c net.Conn) { } func main() { - const ADDR = HOST + ":" + PORT - log.Printf("Starting thomomys on %s\n", ADDR) + // set default values if environment variables aren't set + ADDR_IP = os.Getenv("GOPHER_IP") + if len(ADDR_IP) == 0 { + ADDR_IP = "0.0.0.0" + log.Printf("$GOPHER_IP is not set. Falling back to %s.\n", ADDR_IP) + } - os.Chdir(DIR) + ADDR_PORT = os.Getenv("GOPHER_PORT") + if len(ADDR_PORT) == 0 { + ADDR_PORT = "70" + log.Printf("$GOPHER_PORT is not set. Falling back to port %s.\n", ADDR_PORT) + } - l, err := net.Listen("tcp", ADDR) + HOSTNAME = os.Getenv("GOPHER_HOSTNAME") + if len(HOSTNAME) == 0 { + log.Println("$GOPHER_HOSTNAME is not set. Falling back to system hostname.") + + value, err := os.Hostname() + if err != nil { + log.Fatal(err) + return + } + + HOSTNAME = value + } + + SRVDIR = os.Getenv("GOPHER_DIR") + if len(SRVDIR) == 0 { + SRVDIR = "." + log.Println("$GOPHER_DIR is not set. Falling back to current directory.") + } + + LISTEN_ADDR := ADDR_IP + ":" + ADDR_PORT + log.Printf("Starting thomomys on %s\n", LISTEN_ADDR) + + os.Chdir(SRVDIR) + + l, err := net.Listen("tcp", LISTEN_ADDR) if err != nil { log.Fatal(err) return