Connecting to the database
This commit is contained in:
parent
d3e477eebd
commit
42b8708243
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -57,12 +57,22 @@ Virtual Terminal
|
||||
<span class="visually-hidden">Loading...</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<input id="payment_intent" type="hidden" name="payment_intent" value="payment_intent">
|
||||
<input id="payment_method" type="hidden" name="payment_method" value="payment_method">
|
||||
<input id="payment_amount" type="hidden" name="payment_amount" value="payment_amount">
|
||||
<input id="payment_currency" type="hidden" name="payment_currency" value="payment_currency">
|
||||
|
||||
<input id="payment_intent"
|
||||
type="hidden"
|
||||
name="payment_intent"
|
||||
value="payment_intent">
|
||||
<input id="payment_method"
|
||||
type="hidden"
|
||||
name="payment_method"
|
||||
value="payment_method">
|
||||
<input id="payment_amount"
|
||||
type="hidden"
|
||||
name="payment_amount"
|
||||
value="payment_amount">
|
||||
<input id="payment_currency"
|
||||
type="hidden"
|
||||
name="payment_currency"
|
||||
value="payment_currency">
|
||||
</form>
|
||||
{{ end }}
|
||||
{{ define "js" }}
|
||||
|
3
go.mod
3
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
|
||||
|
4
go.sum
4
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=
|
||||
|
30
internal/driver/driver.go
Normal file
30
internal/driver/driver.go
Normal file
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user