-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·141 lines (118 loc) · 2.67 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
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/novacloudcz/graphql-orm-changelog/gen"
"github.com/novacloudcz/graphql-orm-changelog/src"
"github.com/novacloudcz/graphql-orm/events"
"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,
}
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: "migrate schema database",
Action: func(ctx *cli.Context) error {
fmt.Println("starting migration")
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().Error
}
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 := events.NewEventController()
if err != nil {
return err
}
mux := gen.GetHTTPServeMux(src.New(db, &eventController), 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())
}()
go func(){
src.StartCloudEventsServer(db)
}()
<-stop
log.Println("\nShutting down the server...")
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
err = db.Close()
if err != nil {
return cli.NewExitError(err, 1)
}
log.Println("Database connection closed")
err = h.Shutdown(ctx)
if err != nil {
return cli.NewExitError(err, 1)
}
log.Println("Server gracefully stopped")
return nil
}