Connecting to the database

This commit is contained in:
2024-08-04 23:06:13 +02:00
parent d3e477eebd
commit 42b8708243
6 changed files with 82 additions and 17 deletions

View File

@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"log"
"myapp/internal/driver"
"net/http"
"os"
"time"
@ -27,7 +28,7 @@ type config struct {
type application struct {
config config
infolog *log.Logger
infoLog *log.Logger
errorLog *log.Logger
version string
}
@ -42,7 +43,7 @@ func (app *application) serve() error {
WriteTimeout: 5 * time.Second,
}
app.infolog.Printf(
app.infoLog.Printf(
"Starting Backend server in %s mode on port %d",
app.config.env,
app.config.port,
@ -63,7 +64,7 @@ func main() {
flag.StringVar(
&cfg.db.dsn,
"dsn",
"root@tcp(localhost:6379)/widgets?parseTime=true&tls=false",
"root:example@tcp(localhost:3306)/widgets?parseTime=true&tls=false",
"Application environment {development|production}",
)
@ -75,14 +76,22 @@ func main() {
infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
errorLog := log.New(os.Stdout, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile)
conn, err := driver.OpenDB(cfg.db.dsn)
if err != nil {
errorLog.Fatal(err)
}
defer conn.Close()
app := &application{
version: version,
config: cfg,
infolog: infoLog,
infoLog: infoLog,
errorLog: errorLog,
}
err := app.serve()
app.infoLog.Println("Connected to MariaDB")
err = app.serve()
if err != nil {
log.Fatal(err)
}