56 lines
1.1 KiB
Go
56 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
|
|
"git.rootprojects.org/root/go-again/data/jsondb"
|
|
)
|
|
|
|
func main() {
|
|
portEnv := os.Getenv("PORT")
|
|
|
|
portInt := flag.Int("port", 0, "port on which to serve http")
|
|
addr := flag.String("addr", "", "address on which to serve http")
|
|
flag.Parse()
|
|
|
|
if "" != portEnv {
|
|
if 0 != *portInt {
|
|
log.Fatal("You may set PORT or --port, but not both.")
|
|
return
|
|
}
|
|
n, err := strconv.Atoi(portEnv)
|
|
if nil != err {
|
|
log.Fatalf("Could not parse PORT=%q.", n)
|
|
return
|
|
}
|
|
*portInt = n
|
|
}
|
|
if *portInt < 1024 || *portInt > 65535 {
|
|
log.Fatalf("port should be between 1024 and 65535, not %d.", *portInt)
|
|
return
|
|
}
|
|
portEnv = strconv.Itoa(*portInt)
|
|
|
|
server := &http.Server{
|
|
Addr: fmt.Sprintf("%s:%s", *addr, portEnv),
|
|
Handler: http.HandlerFunc(handleFunc),
|
|
ReadTimeout: 10 * time.Second,
|
|
WriteTimeout: 10 * time.Second,
|
|
MaxHeaderBytes: 1 << 20,
|
|
}
|
|
|
|
fmt.Println("Listening on", server.Addr)
|
|
log.Fatal(server.ListenAndServe())
|
|
}
|
|
|
|
func handleFunc(w http.ResponseWriter, r *http.Request) {
|
|
jsondb.List()
|
|
w.Write([]byte("Hello, World!"))
|
|
}
|