ch3.2
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"flag"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
type config struct {
|
||||
@@ -17,6 +18,9 @@ func main() {
|
||||
flag.StringVar(&cfg.staticDir, "static-dir", "./ui/static", "Path to static assets")
|
||||
flag.Parse()
|
||||
|
||||
infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
|
||||
errorLog := log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
fileServer := http.FileServer(http.Dir("./ui/static/"))
|
||||
|
||||
@@ -25,7 +29,13 @@ func main() {
|
||||
mux.HandleFunc("/snippet/create", snippetCreate)
|
||||
mux.Handle("/static/", http.StripPrefix("/static", fileServer))
|
||||
|
||||
log.Printf("Starting server on %s", cfg.addr)
|
||||
err := http.ListenAndServe(cfg.addr, mux)
|
||||
log.Fatal(err)
|
||||
srv := &http.Server{
|
||||
Addr: cfg.addr,
|
||||
ErrorLog: errorLog,
|
||||
Handler: mux,
|
||||
}
|
||||
|
||||
infoLog.Printf("Starting server on %s", cfg.addr)
|
||||
err := srv.ListenAndServe()
|
||||
errorLog.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user