45 lines
895 B
Go
45 lines
895 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"flag"
|
||
|
"log/slog"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type application struct {
|
||
|
logger *slog.Logger
|
||
|
}
|
||
|
|
||
|
// main run the webserverrrrr
|
||
|
func main() {
|
||
|
// configuration
|
||
|
addr := flag.String("addr", ":4000", "HTTP network address")
|
||
|
logfmt := flag.String("logfmt", "text", "Log output format")
|
||
|
loglevel := flag.String("loglevel", "INFO", "Log level: DEBUG, INFO, WARN, or ERROR")
|
||
|
flag.Parse()
|
||
|
|
||
|
// setup the application
|
||
|
app := &application{
|
||
|
logger: loggerBuilder(logfmt, loglevel),
|
||
|
}
|
||
|
|
||
|
|
||
|
srv := &http.Server{
|
||
|
Addr: *addr,
|
||
|
Handler: app.routes(),
|
||
|
ErrorLog: slog.NewLogLogger(app.logger.Handler(), slog.LevelError),
|
||
|
IdleTimeout: time.Minute,
|
||
|
ReadTimeout: 5 * time.Second,
|
||
|
WriteTimeout: 10 * time.Second,
|
||
|
}
|
||
|
|
||
|
app.logger.Info("starting server", slog.String("addr", *addr))
|
||
|
|
||
|
err := srv.ListenAndServe()
|
||
|
|
||
|
app.logger.Error(err.Error())
|
||
|
os.Exit(1)
|
||
|
}
|