udemy-go-web-2/cmd/web/main.go

130 lines
2.6 KiB
Go
Raw Normal View History

2024-08-03 10:17:39 +00:00
package main
import (
"encoding/gob"
2024-08-03 10:17:39 +00:00
"flag"
"fmt"
"html/template"
"log"
2024-08-04 21:06:13 +00:00
"myapp/internal/driver"
"myapp/internal/models"
2024-08-03 10:17:39 +00:00
"net/http"
"os"
"time"
2024-08-10 09:10:27 +00:00
"github.com/alexedwards/scs/mysqlstore"
2024-08-10 09:10:27 +00:00
"github.com/alexedwards/scs/v2"
2024-08-03 10:17:39 +00:00
)
const (
version = "1.0.0"
cssVersion = "1"
)
2024-08-10 09:10:27 +00:00
var session *scs.SessionManager
2024-08-03 10:17:39 +00:00
type config struct {
port int
env string
api string
db struct {
dsn string
}
stripe struct {
secret string
key string
}
secretkey string
frontend string
2024-08-03 10:17:39 +00:00
}
type application struct {
config config
2024-08-04 21:06:13 +00:00
infoLog *log.Logger
2024-08-03 10:17:39 +00:00
errorLog *log.Logger
templateCache map[string]*template.Template
version string
DB models.DBModel
2024-08-10 09:10:27 +00:00
Session *scs.SessionManager
2024-08-03 10:17:39 +00:00
}
func (app *application) serve() error {
srv := &http.Server{
Addr: fmt.Sprintf(":%d", app.config.port),
Handler: app.routes(),
IdleTimeout: 30 * time.Second,
ReadTimeout: 10 * time.Second,
ReadHeaderTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
}
2024-08-04 21:06:13 +00:00
app.infoLog.Printf(
2024-08-03 10:17:39 +00:00
"Starting HTTP server in %s mode on port %d",
app.config.env,
app.config.port,
)
return srv.ListenAndServe()
}
func main() {
gob.Register(TransactionData{})
2024-08-03 10:17:39 +00:00
var cfg config
flag.IntVar(&cfg.port, "port", 4000, "Server port to listen on")
flag.StringVar(
&cfg.env,
"env",
"development",
"Application environment {development|production}",
)
2024-08-04 14:25:16 +00:00
flag.StringVar(
&cfg.db.dsn,
"dsn",
2024-08-04 21:06:13 +00:00
"root:example@tcp(localhost:3306)/widgets?parseTime=true&tls=false",
"DSN",
2024-08-04 14:25:16 +00:00
)
2024-08-03 10:17:39 +00:00
flag.StringVar(&cfg.api, "api", "http://localhost:4001", "URL to api")
flag.StringVar(&cfg.secretkey, "secret", "secRetKeY", "secret key")
flag.StringVar(&cfg.frontend, "frontend", "http://localhost:4000", "frontend address")
2024-08-03 10:17:39 +00:00
flag.Parse()
cfg.stripe.key = os.Getenv("STRIPE_KEY")
cfg.stripe.secret = os.Getenv("STRIPE_SECRET")
infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
errorLog := log.New(os.Stdout, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile)
2024-08-04 21:06:13 +00:00
conn, err := driver.OpenDB(cfg.db.dsn)
if err != nil {
errorLog.Fatal(err)
}
defer conn.Close()
// set up session
session = scs.New()
session.Lifetime = 24 * time.Hour
session.Store = mysqlstore.New(conn)
2024-08-03 10:17:39 +00:00
tc := make(map[string]*template.Template)
app := &application{
config: cfg,
2024-08-04 21:06:13 +00:00
infoLog: infoLog,
2024-08-03 10:17:39 +00:00
errorLog: errorLog,
templateCache: tc,
version: version,
DB: models.DBModel{DB: conn},
2024-08-10 09:10:27 +00:00
Session: session,
2024-08-03 10:17:39 +00:00
}
2024-08-04 21:06:13 +00:00
app.infoLog.Println("Connected to MariaDB")
err = app.serve()
2024-08-03 10:17:39 +00:00
if err != nil {
app.errorLog.Println(err)
log.Fatal(err)
}
}