Skip to content

Instantly share code, notes, and snippets.

@alexedwards
Created May 5, 2017 08:38
Show Gist options
  • Save alexedwards/e130c6e49303dba494f5ad59a01b8e9a to your computer and use it in GitHub Desktop.
Save alexedwards/e130c6e49303dba494f5ad59a01b8e9a to your computer and use it in GitHub Desktop.
02.02-01.gist
package main
import (
"flag"
"log"
"net/http"
"os"
"snippetbox.org/pkg/handlers/web"
)
var (
addr = flag.String("addr", ":4000", "HTTP service address")
public = flag.String("public", "./public", "Public asset directory")
)
func main() {
flag.Parse()
f, err := os.OpenFile("/tmp/error.log", os.O_RDWR|os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
log.Fatal(err)
}
defer f.Close()
errorLog := log.New(f, "ERROR\t", log.Ldate|log.Ltime|log.Llongfile)
fileServer := http.FileServer(http.Dir(*public))
mux := http.NewServeMux()
mux.HandleFunc("/", web.Home)
mux.HandleFunc("/snippets", web.Create)
mux.HandleFunc("/snippets/", web.Show)
mux.Handle("/public/", http.StripPrefix("/public", fileServer))
srv := http.Server{
Addr: *addr,
ErrorLog: errorLog,
Handler: mux,
}
err = srv.ListenAndServe()
errorLog.Fatal(err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment