learn-go/snippetbox/cmd/web/main.go

111 lines
2.5 KiB
Go
Raw Normal View History

package main
import (
2024-02-07 18:15:05 +00:00
"crypto/tls"
2024-01-25 20:41:08 +00:00
"database/sql"
2024-01-23 22:08:59 +00:00
"flag"
2024-01-25 23:06:28 +00:00
"html/template"
"log/slog"
"net/http"
2024-01-25 20:41:08 +00:00
"regexp"
"os"
2024-02-07 05:06:48 +00:00
"time"
2024-01-25 20:41:08 +00:00
"snippetbox.chaosfem.tw/internal/models"
2024-02-07 05:06:48 +00:00
"github.com/alexedwards/scs/mysqlstore"
"github.com/alexedwards/scs/v2"
2024-02-07 00:46:46 +00:00
"github.com/go-playground/form/v4"
2024-01-25 20:41:08 +00:00
_ "github.com/go-sql-driver/mysql"
)
// for application wide dependencies
type application struct {
logger *slog.Logger
2024-01-25 20:41:08 +00:00
snippets *models.SnippetModel
users *models.UserModel
2024-01-25 23:06:28 +00:00
templateCache map[string]*template.Template
2024-02-07 00:46:46 +00:00
formDecoder *form.Decoder
2024-02-07 05:06:48 +00:00
sessionManager *scs.SessionManager
}
// main it's the snippetbox webapp
func main() {
// configuration
2024-01-23 22:08:59 +00:00
addr := flag.String("addr", ":4000", "HTTP network address")
2024-01-25 20:41:08 +00:00
dsn := flag.String("dsn", "web:dbpass@/snippetbox?parseTime=true", "DB data source name")
logfmt := flag.String("logfmt", "text", "Log output format")
loglevel := flag.String("loglevel", "INFO", "Log level: DEBUG, INFO, WARN, or ERROR")
2024-01-23 22:08:59 +00:00
flag.Parse()
2024-01-25 20:41:08 +00:00
logger := loggerBuilder(logfmt, loglevel)
db, err := openDB(*dsn, logger)
if err != nil {
logger.Error(err.Error())
os.Exit(1)
}
defer db.Close()
2024-01-25 23:06:28 +00:00
templateCache, err := newTemplateCache()
if err != nil {
logger.Error(err.Error())
os.Exit(1)
}
2024-02-07 00:46:46 +00:00
formDecoder := form.NewDecoder()
2024-02-07 05:06:48 +00:00
sessionManager := scs.New()
sessionManager.Store = mysqlstore.New(db)
sessionManager.Lifetime = 12 * time.Hour
// setup the application
app := &application{
2024-01-25 20:41:08 +00:00
logger: logger,
snippets: &models.SnippetModel{DB: db},
users: &models.UserModel{DB: db},
2024-01-25 23:06:28 +00:00
templateCache: templateCache,
2024-02-07 00:46:46 +00:00
formDecoder: formDecoder,
2024-02-07 05:06:48 +00:00
sessionManager: sessionManager,
}
2024-02-07 18:15:05 +00:00
tlsConfig := &tls.Config{
CurvePreferences: []tls.CurveID{tls.X25519, tls.CurveP256},
}
2024-02-07 17:19:49 +00:00
srv := &http.Server{
2024-02-07 18:22:32 +00:00
Addr: *addr,
Handler: app.routes(),
ErrorLog: slog.NewLogLogger(logger.Handler(), slog.LevelError),
TLSConfig: tlsConfig,
IdleTimeout: time.Minute,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
2024-02-07 17:19:49 +00:00
}
logger.Info("starting server", slog.String("addr", srv.Addr))
2024-02-07 18:03:56 +00:00
err = srv.ListenAndServeTLS("./tls/cert.pem", "./tls/key.pem")
2024-02-07 17:19:49 +00:00
2024-01-25 20:41:08 +00:00
logger.Error(err.Error())
os.Exit(1)
}
2024-01-25 20:41:08 +00:00
func openDB(dsn string, logger *slog.Logger) (*sql.DB, error) {
db, err := sql.Open("mysql", dsn)
if err != nil {
return nil, err
}
err = db.Ping()
if err != nil {
db.Close()
return nil, err
}
passRegexp := regexp.MustCompile(":\\S+@")
logger.Info("Opened DB connection pool", slog.String("adapter", "mysql"), slog.String("dsn", passRegexp.ReplaceAllString(dsn, ":<REDACTED>@")))
return db, nil
}