1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
package main
import (
"database/sql"
"flag"
"log/slog"
"net/http"
"os"
_ "github.com/go-sql-driver/mysql"
"github.com/yulqen/ded-go-core/internal/models"
)
type application struct {
logger *slog.Logger
operations *models.OperationModel
organisations *models.OrganisationModel
engagement_strategies *models.EngagementStrategyModel
}
func main() {
addr := flag.String("addr", ":4000", "HTTP network port")
dsn := flag.String("dsn", "web:dedpassword@/ded?parseTime=true", "MySQL data source name")
flag.Parse()
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
// Database connection
db, err := openDB(*dsn)
if err != nil {
logger.Error(err.Error())
os.Exit(1)
}
defer db.Close()
app := &application{
logger: logger,
operations: &models.OperationModel{DB: db},
organisations: &models.OrganisationModel{DB: db},
engagement_strategies: &models.EngagementStrategyModel{DB: db},
}
// mux := http.NewServeMux()
// mux.HandleFunc("/", home)
// log.Print("starting server on :4000")
logger.Info("starting server", "addr", *addr)
err = http.ListenAndServe(*addr, app.routes())
logger.Error(err.Error())
os.Exit(1)
}
// openDB wraps sql.Open() and returns a sql.DB connection pool
// for a given DSN.
func openDB(dsn string) (*sql.DB, error) {
db, err := sql.Open("mysql", dsn)
if err != nil {
return nil, err
}
err = db.Ping()
if err != nil {
db.Close()
return nil, err
}
return db, nil
}
|