-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
44 lines (34 loc) · 1.31 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
package main
import (
"flag"
"time"
"github.com/cixtor/middleware"
)
// app is an instance of the Application, the core struct.
var app Application
// router is an instance of Middleware, a lightweight HTTP router.
var router = middleware.New()
// init functions are usually discouraged because they are called “randomly” so
// if there is more than one in the whole project, there is no guarantee that
// they will be executed in the correct order. However, when they are correctly
// used —like in this project— they allow us to provide extensibility.
func init() {
flag.StringVar(&app.host, "host", "0.0.0.0", "Host or IP to run the server")
flag.StringVar(&app.port, "port", "3000", "Port number to run the server")
flag.StringVar(&app.database, "database", "database.db", "Path to the SQLite database")
flag.StringVar(&app.migration, "migration", "migration.sql", "SQL to initialize the database")
}
func main() {
app.Initialize()
defer app.db.Close()
router.Host = app.host
router.Port = app.port
// sane timeouts, because default Go values are crazy.
router.IdleTimeout = 10 * time.Second
router.ReadTimeout = 10 * time.Second
router.WriteTimeout = 10 * time.Second
router.ShutdownTimeout = 10 * time.Second
router.ReadHeaderTimeout = 10 * time.Second
router.Use(app.Auth)
router.ListenAndServe()
}