Skip to content

Instantly share code, notes, and snippets.

@llimllib
Created February 16, 2021 15:06
Show Gist options
  • Save llimllib/e5c90ce5e720ae67b4cfd22f07c86ec2 to your computer and use it in GitHub Desktop.
Save llimllib/e5c90ce5e720ae67b4cfd22f07c86ec2 to your computer and use it in GitHub Desktop.
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