webweav.ing/cmd/web/main.go

46 lines
898 B
Go
Raw Permalink Normal View History

2024-01-19 03:33:37 +00:00
package main
import (
"flag"
"log"
"net/http"
"os"
2024-01-22 20:43:46 +00:00
"text/template"
2024-01-19 03:33:37 +00:00
)
type application struct {
errorLog *log.Logger
infoLog *log.Logger
2024-01-22 20:43:46 +00:00
templateCache map[string]*template.Template
2024-01-19 03:33:37 +00:00
}
func main() {
addr := flag.String("addr", ":8000", "HTTP network address")
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)
2024-01-22 20:43:46 +00:00
templateCache, err := newTemplateCache()
if err != nil {
errorLog.Fatal(err)
}
2024-01-19 03:33:37 +00:00
app := &application {
errorLog: errorLog,
infoLog: infoLog,
2024-01-22 20:43:46 +00:00
templateCache: templateCache,
2024-01-19 03:33:37 +00:00
}
srv := &http.Server {
Addr: *addr,
ErrorLog: errorLog,
Handler: app.routes(),
}
infoLog.Printf("Starting server on %s", *addr)
2024-01-22 20:43:46 +00:00
err = srv.ListenAndServe()
2024-01-19 03:33:37 +00:00
errorLog.Fatal(err)
}