2024-08-04 14:26:02 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2024-08-04 21:06:13 +00:00
|
|
|
"myapp/internal/driver"
|
2024-08-06 19:35:28 +00:00
|
|
|
"myapp/internal/models"
|
2024-08-04 14:26:02 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
version = "1.0.0"
|
|
|
|
)
|
|
|
|
|
|
|
|
type config struct {
|
|
|
|
port int
|
|
|
|
env string
|
|
|
|
db struct {
|
|
|
|
dsn string
|
|
|
|
}
|
|
|
|
stripe struct {
|
|
|
|
secret string
|
|
|
|
key string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type application struct {
|
|
|
|
config config
|
2024-08-04 21:06:13 +00:00
|
|
|
infoLog *log.Logger
|
2024-08-04 14:26:02 +00:00
|
|
|
errorLog *log.Logger
|
|
|
|
version string
|
2024-08-06 19:35:28 +00:00
|
|
|
DB models.DBModel
|
2024-08-04 14:26:02 +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-04 14:26:02 +00:00
|
|
|
"Starting Backend server in %s mode on port %d",
|
|
|
|
app.config.env,
|
|
|
|
app.config.port,
|
|
|
|
)
|
|
|
|
return srv.ListenAndServe()
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var cfg config
|
|
|
|
|
|
|
|
flag.IntVar(&cfg.port, "port", 4001, "Server port to listen on")
|
|
|
|
flag.StringVar(
|
|
|
|
&cfg.env,
|
|
|
|
"env",
|
|
|
|
"development",
|
|
|
|
"Application environment {development|production|maintenance}",
|
|
|
|
)
|
|
|
|
flag.StringVar(
|
|
|
|
&cfg.db.dsn,
|
|
|
|
"dsn",
|
2024-08-04 21:06:13 +00:00
|
|
|
"root:example@tcp(localhost:3306)/widgets?parseTime=true&tls=false",
|
2024-08-04 14:26:02 +00:00
|
|
|
"Application environment {development|production}",
|
|
|
|
)
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
2024-08-04 14:26:02 +00:00
|
|
|
app := &application{
|
|
|
|
version: version,
|
|
|
|
config: cfg,
|
2024-08-04 21:06:13 +00:00
|
|
|
infoLog: infoLog,
|
2024-08-04 14:26:02 +00:00
|
|
|
errorLog: errorLog,
|
2024-08-06 19:35:28 +00:00
|
|
|
DB: models.DBModel{DB: conn},
|
2024-08-04 14:26:02 +00:00
|
|
|
}
|
|
|
|
|
2024-08-04 21:06:13 +00:00
|
|
|
app.infoLog.Println("Connected to MariaDB")
|
|
|
|
|
|
|
|
err = app.serve()
|
2024-08-04 14:26:02 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|