-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (85 loc) · 2.3 KB
/
main.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"encoding/gob"
"flag"
"fmt"
"log"
"net/http"
"os"
"text/template"
"time"
"github.com/alexedwards/scs/v2"
"github.com/caleberi/gostripe/internal/driver"
"github.com/caleberi/gostripe/internal/models"
)
const version = "1.0.0"
const cssVersion = "1"
var session *scs.SessionManager
type config struct {
port int
env string
api string
db struct {
dns string
}
stripe struct {
key string
secret string
}
}
type application struct {
config config
infoLog *log.Logger
errorLog *log.Logger
templateCache map[string]*template.Template
version string
DB models.DBModel
Session *scs.SessionManager
}
func (app *application) serve() error {
srv := &http.Server{
Addr: fmt.Sprintf(":%d", app.config.port),
Handler: app.routes(),
ReadTimeout: 10 * time.Second,
IdleTimeout: 30 * time.Second,
ReadHeaderTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
}
app.infoLog.Printf("starting http server on port %[1]d with url: http://localhost:%[1]d ...", app.config.port)
return srv.ListenAndServe()
}
func main() {
gob.Register(TransactionData{})
var cfg config
flag.IntVar(&cfg.port, "port", 3000, "📌 app server port")
flag.StringVar(&cfg.env, "environment", "development", "📌 application runtime environment")
flag.StringVar(&cfg.api, "api", "http://localhost:4001", "📌 api endpoint entry for application")
flag.StringVar(&cfg.db.dns, "dsn", "root:root@tcp(localhost:3306)/gostripe?parseTime=true&tls=false", "📌 database domain service name (DSN)")
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)
conn, err := driver.OpenDB(cfg.db.dns)
if err != nil {
errorLog.Fatal(err)
}
defer conn.Close()
session = scs.New()
session.Lifetime = 24 * time.Hour
tc := make(map[string]*template.Template)
app := &application{
config: cfg,
templateCache: tc,
infoLog: infoLog,
errorLog: errorLog,
version: version,
DB: models.DBModel{
DB: conn,
},
Session: session,
}
if err := app.serve(); err != nil {
app.errorLog.Fatalln(err)
}
}