diff --git a/cmd/api/api.go b/cmd/api/api.go index 020f1d8..45904a4 100644 --- a/cmd/api/api.go +++ b/cmd/api/api.go @@ -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) } diff --git a/cmd/web/main.go b/cmd/web/main.go index c0a87f0..32b806c 100644 --- a/cmd/web/main.go +++ b/cmd/web/main.go @@ -5,6 +5,7 @@ import ( "fmt" "html/template" "log" + "myapp/internal/driver" "net/http" "os" "time" @@ -30,7 +31,7 @@ type config struct { type application struct { config config - infolog *log.Logger + infoLog *log.Logger errorLog *log.Logger templateCache map[string]*template.Template version string @@ -46,7 +47,7 @@ func (app *application) serve() error { WriteTimeout: 5 * time.Second, } - app.infolog.Printf( + app.infoLog.Printf( "Starting HTTP server in %s mode on port %d", app.config.env, app.config.port, @@ -67,8 +68,8 @@ func main() { flag.StringVar( &cfg.db.dsn, "dsn", - "root@tcp(localhost:6379)/widgets?parseTime=true&tls=false", - "Application environment {development|production}", + "root:example@tcp(localhost:3306)/widgets?parseTime=true&tls=false", + "DSN", ) flag.StringVar(&cfg.api, "api", "http://localhost:4001", "URL to api") @@ -80,17 +81,25 @@ 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() + tc := make(map[string]*template.Template) app := &application{ config: cfg, - infolog: infoLog, + infoLog: infoLog, errorLog: errorLog, templateCache: tc, version: version, } - err := app.serve() + app.infoLog.Println("Connected to MariaDB") + + err = app.serve() if err != nil { app.errorLog.Println(err) log.Fatal(err) diff --git a/cmd/web/templates/terminal.page.gohtml b/cmd/web/templates/terminal.page.gohtml index b1e000b..c2b1395 100644 --- a/cmd/web/templates/terminal.page.gohtml +++ b/cmd/web/templates/terminal.page.gohtml @@ -57,12 +57,22 @@ Virtual Terminal Loading... - - - - - - + + + + {{ end }} {{ define "js" }} diff --git a/go.mod b/go.mod index 0622629..4e4c45d 100644 --- a/go.mod +++ b/go.mod @@ -5,5 +5,8 @@ go 1.22.5 require ( github.com/go-chi/chi/v5 v5.1.0 github.com/go-chi/cors v1.2.1 + github.com/go-sql-driver/mysql v1.8.1 github.com/stripe/stripe-go/v79 v79.6.0 ) + +require filippo.io/edwards25519 v1.1.0 // indirect diff --git a/go.sum b/go.sum index 5f8cffd..9d8e030 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,13 @@ +filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= +filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4= github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58= +github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= +github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/internal/driver/driver.go b/internal/driver/driver.go new file mode 100644 index 0000000..045ba79 --- /dev/null +++ b/internal/driver/driver.go @@ -0,0 +1,30 @@ +package driver + +import ( + "context" + "database/sql" + "fmt" + "time" + + _ "github.com/go-sql-driver/mysql" +) + +func OpenDB(dsn string) (*sql.DB, error) { + db, err := sql.Open("mysql", dsn) + if err != nil { + return nil, err + } + + db.SetConnMaxLifetime(time.Minute * 3) + db.SetMaxOpenConns(10) + db.SetMaxIdleConns(10) + + ctx, _ := context.WithTimeout(context.Background(), 3*time.Second) + err = db.PingContext(ctx) + if err != nil { + fmt.Println(err) + return nil, err + } + + return db, nil +}