Skip to content

Instantly share code, notes, and snippets.

@llimllib

llimllib/main.go

Created Feb 16, 2021
Embed
What would you like to do?
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