Created
February 16, 2021 15:06
-
-
Save llimllib/e5c90ce5e720ae67b4cfd22f07c86ec2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// An extremely simple web application skeleton | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
"os" | |
) | |
func index(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "hello world") | |
} | |
func logreq(f func(w http.ResponseWriter, r *http.Request)) http.Handler { | |
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
log.Printf("path: %s", r.URL.Path) | |
f(w, r) | |
}) | |
} | |
type App struct { | |
Port string | |
} | |
func (a *App) Start() { | |
http.Handle("/", logreq(index)) | |
addr := fmt.Sprintf(":%s", a.Port) | |
log.Printf("Starting app on %s", addr) | |
log.Fatal(http.ListenAndServe(addr, nil)) | |
} | |
func env(key, adefault string) string { | |
val, ok := os.LookupEnv(key) | |
if !ok { | |
return adefault | |
} | |
return val | |
} | |
func main() { | |
server := App{ | |
Port: env("PORT", "8080"), | |
} | |
server.Start() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment