-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·113 lines (93 loc) · 2.26 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
package main
import (
"context"
"embed"
"fmt"
"io/fs"
"os"
"os/exec"
"os/signal"
"sync"
"syscall"
"time"
"github.com/yerTools/ResMon/src/go/database"
"github.com/yerTools/ResMon/src/go/web"
)
//go:embed webroot
var webroot embed.FS
//go:embed src/sql/migrations
var migrations embed.FS
func createContext(killTimeout time.Duration) (
ctx context.Context, killCtx context.Context,
cancelCtx context.CancelFunc,
cancelKillCtx context.CancelFunc,
) {
killCtx, cancelKillCtx = context.WithCancel(context.Background())
ctx, cancelCtx = context.WithCancel(killCtx)
go func() {
sigChan := make(chan os.Signal, 3)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
<-sigChan
cancelCtx()
}()
go func() {
<-ctx.Done()
sigChan := make(chan os.Signal, 3)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
select {
case <-sigChan:
case <-time.After(killTimeout):
}
cancelKillCtx()
}()
return
}
func main() {
ctx, killCtx, shutdown, _ := createContext(time.Second * 10)
var devMode bool
for _, a := range os.Args {
if a == "--dev" {
devMode = true
}
}
err := os.MkdirAll("data", os.ModePerm)
if err != nil {
panic(fmt.Sprintf("could not create data directory: %v", err))
}
migrationsFS, err := fs.Sub(migrations, "src/sql/migrations")
if err != nil {
panic(fmt.Sprintf("could not get embedded migrations: %v", err))
}
fmt.Println("opening database ...")
db, err := database.OpenDB(ctx, "./data/database.db", migrationsFS)
if err != nil {
panic(fmt.Sprintf("could not open database: %v", err))
}
defer db.Close()
fmt.Println("starting web server at http://127.0.0.1:8321/ ...")
svr := web.NewServer(8321, "webroot", webroot, devMode)
var wg sync.WaitGroup
wg.Add(1)
go (func() {
err := svr.Run(ctx, killCtx, shutdown, db)
if err != nil {
panic(fmt.Sprintf("an error occurred: %v", err))
}
wg.Done()
})()
if len(os.Args) == 2 && os.Args[1] == "--generate-elm-graphql-schema" {
cmdStruct := exec.Command(
"npx", "elm-graphql",
"http://127.0.0.1:8321/api",
"--base", "Graph",
"--output", "generated/elm",
"--scalar-codecs", "Api.ScalarCodecs")
out, err := cmdStruct.Output()
fmt.Println(string(out))
if err != nil {
panic(err)
}
shutdown()
}
wg.Wait()
}