-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
78 lines (64 loc) · 1.75 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
package main
import (
"flag"
"os"
"os/signal"
sys "syscall"
"github.com/Leantar/fimserver/modules/config"
"github.com/Leantar/fimserver/modules/preparation"
"github.com/Leantar/fimserver/repository"
"github.com/Leantar/fimserver/server"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
type Config struct {
Server server.Config `yaml:"server"`
Repository repository.Config `yaml:"repository"`
}
var (
configPath = flag.String("config", "config.yaml", "Specify a path to load the config from")
setupMode = flag.Bool("setup", false, "Prepare the database of the application")
)
func main() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog.SetGlobalLevel(zerolog.InfoLevel)
// Parse command line arguments
flag.Parse()
// Read config file
var conf Config
err := config.FromYamlFile(*configPath, &conf)
if err != nil {
log.Fatal().Caller().Err(err).Msg("failed to read config")
}
if *setupMode {
err := setup(conf)
if err != nil {
log.Fatal().Caller().Err(err).Msg("failed to run preparation")
}
} else {
err := run(conf)
if err != nil {
log.Fatal().Caller().Err(err).Msg("failed to run server")
}
}
}
// Start the normal execution mode
func run(conf Config) error {
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, sys.SIGINT, sys.SIGTERM)
repo := repository.New(conf.Repository)
srv := server.New(repo, conf.Server)
go func() {
if err := srv.Run(); err != nil {
log.Fatal().Caller().Err(err).Msg("server failed to run")
}
}()
<-quit
srv.Stop()
return nil
}
// Run the setup mode. This creates all required casbin rules, an admin user and all relations inside the database.
func setup(conf Config) error {
repo := repository.New(conf.Repository)
return preparation.Setup(repo)
}