forked from joshsoftware/golang-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
89 lines (76 loc) · 1.73 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
package main
// @APITitle Main
// @APIDescription Main API for Microservices in Go!
import (
"fmt"
"joshsoftware/golang-boilerplate/config"
"joshsoftware/golang-boilerplate/db"
"joshsoftware/golang-boilerplate/service"
"os"
"strconv"
logger "github.com/sirupsen/logrus"
"github.com/urfave/cli"
"github.com/urfave/negroni"
)
func main() {
logger.SetFormatter(&logger.TextFormatter{
FullTimestamp: true,
TimestampFormat: "02-01-2006 15:04:05",
})
config.Load()
cliApp := cli.NewApp()
cliApp.Name = config.AppName()
cliApp.Version = "1.0.0"
cliApp.Commands = []cli.Command{
{
Name: "start",
Usage: "start server",
Action: func(c *cli.Context) error {
return startApp()
},
},
{
Name: "create_migration",
Usage: "create migration file",
Action: func(c *cli.Context) error {
return db.CreateMigrationFile(c.Args().Get(0))
},
},
{
Name: "migrate",
Usage: "run db migrations",
Action: func(c *cli.Context) error {
return db.RunMigrations()
},
},
{
Name: "rollback",
Usage: "rollback migrations",
Action: func(c *cli.Context) error {
return db.RollbackMigrations(c.Args().Get(0))
},
},
}
if err := cliApp.Run(os.Args); err != nil {
panic(err)
}
}
func startApp() (err error) {
store, err := db.Init()
if err != nil {
logger.WithField("err", err.Error()).Error("Database init failed")
return
}
deps := service.Dependencies{
Store: store,
}
// mux router
router := service.InitRouter(deps)
// init web server
server := negroni.Classic()
server.UseHandler(router)
port := config.AppPort() // This can be changed to the service port number via environment variable.
addr := fmt.Sprintf(":%s", strconv.Itoa(port))
server.Run(addr)
return
}