/
main.go
178 lines (148 loc) · 4.58 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"context"
"database/sql"
"expvar"
"flag"
"fmt"
"github.com/ejacobg/greenlight/internal/data"
"github.com/ejacobg/greenlight/internal/jsonlog"
"github.com/ejacobg/greenlight/internal/mailer"
"os"
"runtime"
"strings"
"sync"
"time"
_ "github.com/lib/pq"
)
var (
// buildTime will be populated using the -X linker flag.
buildTime string
version string
)
type config struct {
port int
env string
db struct {
dsn string
maxOpenConns int
maxIdleConns int
maxIdleTime string
}
// Settings for the rate limiter.
limiter struct {
rps float64 // Requests per second (ie. refill rate)
burst int // Maximum number of simultaneous requests (bucket capacity)
enabled bool // Allow enabling/disabling of rate limiter
}
// Mailtrap inbox credentials.
smtp struct {
host string
port int
username string
password string
sender string
}
// CORS settings.
cors struct {
trustedOrigins []string
}
// JWT settings.
jwt struct {
secret string
}
}
type application struct {
config config
logger *jsonlog.Logger
models data.Models
mailer mailer.Mailer
wg sync.WaitGroup
}
func main() {
var cfg config
flag.IntVar(&cfg.port, "port", 4000, "API server port")
flag.StringVar(&cfg.env, "env", "development", "Environment (development|staging|production)")
// Update the default value with your password.
flag.StringVar(&cfg.db.dsn, "db-dsn", "", "PostgreSQL DSN")
// Database configuration
flag.IntVar(&cfg.db.maxOpenConns, "db-max-open-conns", 25, "PostgreSQL max open connections")
flag.IntVar(&cfg.db.maxIdleConns, "db-max-idle-conns", 25, "PostgreSQL max idle connections")
flag.StringVar(&cfg.db.maxIdleTime, "db-max-idle-time", "15m", "PostgreSQL max connection idle time")
// Rate limiter configuration
flag.Float64Var(&cfg.limiter.rps, "limiter-rps", 2, "Rate limiter maximum requests per second")
flag.IntVar(&cfg.limiter.burst, "limiter-burst", 4, "Rate limiter maximum burst")
flag.BoolVar(&cfg.limiter.enabled, "limiter-enabled", true, "Enable rate limiter") // Rate limiter is on by default.
// Mailtrap configuration
flag.StringVar(&cfg.smtp.host, "smtp-host", "", "SMTP host")
flag.IntVar(&cfg.smtp.port, "smtp-port", 0, "SMTP port")
flag.StringVar(&cfg.smtp.username, "smtp-username", "", "SMTP username")
flag.StringVar(&cfg.smtp.password, "smtp-password", "", "SMTP password")
flag.StringVar(&cfg.smtp.sender, "smtp-sender", "", "SMTP sender")
// CORS configuration
flag.Func("cors-trusted-origins", "Trusted CORS origins (space separated)", func(val string) error {
cfg.cors.trustedOrigins = strings.Fields(val)
return nil
})
// JWT configuration
// If a secret is provided, then the application will use JWTs for authentication. Otherwise, it will default to using stateful tokens.
flag.StringVar(&cfg.jwt.secret, "jwt-secret", "", "JWT secret")
displayVersion := flag.Bool("version", false, "Display version and exit")
flag.Parse()
if *displayVersion {
fmt.Printf("Version:\t%s\n", version)
fmt.Printf("Build time:\t%s\n", buildTime)
os.Exit(0)
}
logger := jsonlog.New(os.Stdout, jsonlog.LevelInfo)
db, err := openDB(cfg)
if err != nil {
logger.PrintFatal(err, nil)
}
defer db.Close()
logger.PrintInfo("database connection pool established", nil)
// Add the current version number to our debug output.
expvar.NewString("version").Set(version)
// Publish the number of active goroutines.
expvar.Publish("goroutines", expvar.Func(func() interface{} {
return runtime.NumGoroutine()
}))
// Publish the database connection pool statistics.
expvar.Publish("database", expvar.Func(func() interface{} {
return db.Stats()
}))
// Publish the current Unix timestamp.
expvar.Publish("timestamp", expvar.Func(func() interface{} {
return time.Now().Unix()
}))
app := &application{
config: cfg,
logger: logger,
models: data.NewModels(db),
mailer: mailer.New(cfg.smtp.host, cfg.smtp.port, cfg.smtp.username, cfg.smtp.password, cfg.smtp.sender),
}
if err = app.serve(); err != nil {
logger.PrintFatal(err, nil)
}
}
func openDB(cfg config) (*sql.DB, error) {
db, err := sql.Open("postgres", cfg.db.dsn)
if err != nil {
return nil, err
}
db.SetMaxOpenConns(cfg.db.maxOpenConns)
db.SetMaxIdleConns(cfg.db.maxIdleConns)
duration, err := time.ParseDuration(cfg.db.maxIdleTime)
if err != nil {
return nil, err
}
db.SetConnMaxIdleTime(duration)
// We will wait for 5 seconds for a response from the DB.
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err = db.PingContext(ctx)
if err != nil {
return nil, err
}
return db, nil
}