chore: basic server setup
91f579dc
1 file(s) · +29 −1
| 1 | 1 | package main |
|
| 2 | 2 | ||
| 3 | + | import ( |
|
| 4 | + | "fmt" |
|
| 5 | + | "log" |
|
| 6 | + | "net/http" |
|
| 7 | + | "os" |
|
| 8 | + | ) |
|
| 9 | + | ||
| 10 | + | var portEnv = os.Getenv("PORT") |
|
| 11 | + | ||
| 12 | + | func indexHandler(writer http.ResponseWriter, req *http.Request) { |
|
| 13 | + | fmt.Fprintf(writer, "hello world\n") |
|
| 14 | + | } |
|
| 15 | + | ||
| 16 | + | func logRequest(handler http.Handler) http.Handler { |
|
| 17 | + | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
|
| 18 | + | log.Printf("%s %s %s\n", r.Method, r.URL, r.RemoteAddr) |
|
| 19 | + | handler.ServeHTTP(w, r) |
|
| 20 | + | }) |
|
| 21 | + | } |
|
| 22 | + | ||
| 3 | 23 | func main() { |
|
| 4 | - | ||
| 24 | + | if portEnv == "" { |
|
| 25 | + | portEnv = "8090" |
|
| 26 | + | } |
|
| 27 | + | ||
| 28 | + | http.HandleFunc("/", indexHandler) |
|
| 29 | + | ||
| 30 | + | port := fmt.Sprintf(":%s", portEnv) |
|
| 31 | + | log.Println("Listening on", port) |
|
| 32 | + | log.Fatal(http.ListenAndServe(port, logRequest(http.DefaultServeMux))) |
|
| 5 | 33 | } |