-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·156 lines (132 loc) · 3.06 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
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/novacloudcz/graphql-orm-example/gen"
"github.com/novacloudcz/graphql-orm-example/src"
"github.com/rs/cors"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Name = "graphql-orm"
app.Usage = "This tool is for generating "
app.Version = "0.0.0"
app.Commands = []cli.Command{
startCmd,
migrateCmd,
automigrateCmd,
}
err := app.Run(os.Args)
if err != nil {
panic(err)
}
}
var startCmd = cli.Command{
Name: "start",
Usage: "start api server",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "cors",
Usage: "Enable cors",
},
cli.StringFlag{
Name: "p,port",
Usage: "Port to listen to",
Value: "80",
EnvVar: "PORT",
},
},
Action: func(ctx *cli.Context) error {
cors := ctx.Bool("cors")
port := ctx.String("port")
if err := startServer(cors, port); err != nil {
return cli.NewExitError(err.Error(), 1)
}
return nil
},
}
var migrateCmd = cli.Command{
Name: "migrate",
Usage: "run migration sequence (using gomigrate)",
Action: func(ctx *cli.Context) error {
fmt.Println("starting migration")
if err := migrate(); err != nil {
return cli.NewExitError(err.Error(), 1)
}
fmt.Println("migration complete")
return nil
},
}
var automigrateCmd = cli.Command{
Name: "automigrate",
Usage: "migrate schema database using basic gorm migration",
Action: func(ctx *cli.Context) error {
fmt.Println("starting automigration")
if err := automigrate(); err != nil {
return cli.NewExitError(err.Error(), 1)
}
fmt.Println("migration complete")
return nil
},
}
func automigrate() error {
db := gen.NewDBFromEnvVars()
defer db.Close()
return db.AutoMigrate()
}
func migrate() error {
db := gen.NewDBFromEnvVars()
defer db.Close()
return db.Migrate(src.GetMigrations(db))
}
func startServer(enableCors bool, port string) error {
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
db := gen.NewDBFromEnvVars()
defer db.Close()
eventController, err := gen.NewEventController()
if err != nil {
return err
}
mux := gen.GetHTTPServeMux(src.New(db, &eventController), db, src.GetMigrations(db))
mux.HandleFunc("/healthcheck", func(res http.ResponseWriter, req *http.Request) {
if err := db.Ping(); err != nil {
res.WriteHeader(400)
res.Write([]byte("ERROR"))
return
}
res.WriteHeader(200)
res.Write([]byte("OK"))
})
var handler http.Handler
if enableCors {
handler = cors.AllowAll().Handler(mux)
} else {
handler = mux
}
h := &http.Server{Addr: ":" + port, Handler: handler}
go func() {
log.Printf("connect to http://localhost:%s/graphql for GraphQL playground", port)
log.Fatal(h.ListenAndServe())
}()
<-stop
log.Println("\nShutting down the server...")
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
err = h.Shutdown(ctx)
if err != nil {
return cli.NewExitError(err, 1)
}
log.Println("Server gracefully stopped")
err = db.Close()
if err != nil {
return cli.NewExitError(err, 1)
}
log.Println("Database connection closed")
return nil
}