-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.go
81 lines (76 loc) · 2.68 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
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"sync"
"wunderbase/pkg/api"
"wunderbase/pkg/migrate"
"wunderbase/pkg/queryengine"
"github.com/caarlos0/env/v6"
)
type config struct {
Production bool `env:"PRODUCTION" envDefault:"false"`
PrismaSchemaFilePath string `env:"PRISMA_SCHEMA_FILE" envDefault:"./schema.prisma"`
MigrationLockFilePath string `env:"MIGRATION_LOCK_FILE" envDefault:"migration.lock"`
EnableSleepMode bool `env:"ENABLE_SLEEP_MODE" envDefault:"true"`
SleepAfterSeconds int `env:"SLEEP_AFTER_SECONDS" envDefault:"10"`
// I think that we should discard `EnablePlayground`, when we add `Production` flag.
// EnablePlayground bool `env:"ENABLE_PLAYGROUND" envDefault:"true"`
MigrationEnginePath string `env:"MIGRATION_ENGINE_PATH" envDefault:"./migration-engine"`
QueryEnginePath string `env:"QUERY_ENGINE_PATH" envDefault:"./query-engine"`
QueryEnginePort string `env:"QUERY_ENGINE_PORT" envDefault:"4467"`
ListenAddr string `env:"LISTEN_ADDR" envDefault:"0.0.0.0:4466"`
GraphiQLApiURL string `env:"GRAPHIQL_API_URL" envDefault:"http://localhost:4466"`
ReadLimitSeconds int `env:"READ_LIMIT_SECONDS" envDefault:"10000"`
WriteLimitSeconds int `env:"WRITE_LIMIT_SECONDS" envDefault:"2000"`
HealthEndpoint string `env:"HEALTH_ENDPOINT" envDefault:"/health"`
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
wg := &sync.WaitGroup{}
wg.Add(2)
config := &config{}
if err := env.Parse(config); err != nil {
log.Fatalln("parse env", err)
}
schema, err := ioutil.ReadFile(config.PrismaSchemaFilePath)
if err != nil {
log.Fatalln("load prisma schema", err)
}
migrate.Database(config.MigrationEnginePath, config.MigrationLockFilePath, string(schema), config.PrismaSchemaFilePath)
go queryengine.Run(ctx, wg, config.QueryEnginePath, config.QueryEnginePort, config.PrismaSchemaFilePath, config.Production)
log.Printf("Server Listening on: http://%s", config.ListenAddr)
handler := api.NewHandler(config.EnableSleepMode,
config.Production,
fmt.Sprintf("http://localhost:%s/", config.QueryEnginePort),
fmt.Sprintf("http://localhost:%s/sdl", config.QueryEnginePort),
config.HealthEndpoint,
config.SleepAfterSeconds,
config.ReadLimitSeconds,
config.WriteLimitSeconds,
cancel)
srv := http.Server{
Addr: config.ListenAddr,
Handler: handler,
}
go func() {
err = srv.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
log.Fatalln("listen and serve", err)
}
}()
<-ctx.Done()
err = srv.Close()
if err != nil {
log.Fatalln("close server", err)
}
log.Println("Server stopped")
wg.Done()
wg.Wait()
os.Exit(0)
}