/
root.go
74 lines (62 loc) · 1.69 KB
/
root.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
package commands
import (
"context"
"fmt"
"github.com/iReflect/reflect-app/config"
"github.com/iReflect/reflect-app/db"
"github.com/iReflect/reflect-app/servers"
"github.com/iReflect/reflect-app/workers"
"github.com/spf13/cobra"
"log"
"os"
"os/signal"
"time"
)
var rootCmd = &cobra.Command{
Use: "reflect-app",
Short: "reflect-app is a retrospective and feedback tool",
Long: `reflect-app is a retrospective and feedback tool
Complete documentation is available at https://ireflect.github.io`,
Run: func(cmd *cobra.Command, args []string) {
runCommand(cmd, args)
},
}
// Execute ...
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func runCommand(cmd *cobra.Command, args []string) {
configuration := config.GetConfig()
//Run migrations - Need to see how this would be possible with new goose.
gormDB := db.Initialize(configuration)
db.Migrate(configuration, gormDB)
app := &server.App{}
app.Initialize(configuration)
app.SetRoutes()
app.SetAdminRoutes()
srv := app.Server(":3000")
go func() {
// service connections
if err := srv.ListenAndServe(); err != nil {
log.Printf("listen: %s\n", err)
}
}()
asyncWorkers := &workers.Workers{}
asyncWorkers.Initialize(configuration)
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 5 seconds.
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
asyncWorkers.Shutdown()
log.Println("Shutdown Server ...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("App Server Shutdown:", err)
}
log.Println("Server exiting")
}