use environment variables instead of hardcoding values
git-svn-id: file:///srv/svn/repo/tokiko/trunk@11 8f5ca974-a7f8-e144-9f80-d41d5039c194
This commit is contained in:
parent
cf4e8d7553
commit
d4bd6e92a5
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +0,0 @@
|
||||
Gophermap
|
||||
test-gopher
|
4
Gophermap
Normal file
4
Gophermap
Normal file
@ -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.
|
7
README
7
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
|
||||
|
55
main.go
55
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user