-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.go
137 lines (110 loc) · 3.91 KB
/
mod.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
package main
import (
"fmt"
"net/http"
"os"
"os/signal"
"path/filepath"
"sync"
"time"
"github.com/jessevdk/go-flags"
"github.com/nkcr/hodor/config"
"github.com/nkcr/hodor/deployer"
"github.com/nkcr/hodor/server"
"github.com/rs/zerolog"
"github.com/tidwall/buntdb"
)
// Version contains the current or build version. This variable can be changed
// at build time with:
//
// go build -ldflags="-X 'main.Version=v1.0.0'"
//
// Version should be fetched from git: `git describe --tags`
var Version = "unknown"
// BuildTime indicates the time at which the binary has been built. Must be set
// as with Version.
var BuildTime = "unknown"
var logout = zerolog.ConsoleWriter{
Out: os.Stdout,
TimeFormat: time.RFC3339,
}
// args defines the CLI arguments. You can always use -h to see the help.
type args struct {
Config string `short:"c" long:"config" default:"config.json" description:"File path of the configuration."`
DBFilePath string `short:"d" long:"dbfilepath" default:"hodor.db" description:"File path of the database."`
HTTPListen string `short:"l" long:"listen" default:"0.0.0.0:3333" description:"The listen address of the HTTP server that servers the API."`
Version bool `short:"v" long:"version" description:"Displays the version."`
}
func main() {
var args args
parser := flags.NewParser(&args, flags.Default)
remaining, err := parser.Parse()
if err != nil {
flagsErr, ok := err.(*flags.Error)
if ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
}
fmt.Println("failed to parse arguments:", err.Error())
os.Exit(1)
}
if len(remaining) != 0 {
fmt.Printf("unknown flags: %v\n", remaining)
os.Exit(1)
}
if args.Version {
fmt.Println("Hodor", Version, "-", BuildTime)
os.Exit(0)
}
var logger = zerolog.New(logout).Level(zerolog.InfoLevel).
With().Timestamp().Logger().
With().Caller().Logger()
logger.Info().Msgf("hi,\n"+
"┌───────────────────────────────────────────────┐\n"+
"│ ** Hookable Deployment of Releases **\t│\n"+
"├───────────────────────────────────────────────┤\n"+
"│ Version %s │ Build time %s\t│\n"+
"├───────────────────────────────────────────────┤\n"+
"│ Config %s\t│\n"+
"├───────────────────────────────────────────────┤\n"+
"│ DBFilePath %s\t│\n"+
"├───────────────────────────────────────────────┤\n"+
"│ HTTPListen %s\t│\n"+
"└───────────────────────────────────────────────┘\n",
Version, BuildTime, args.Config, args.DBFilePath, args.HTTPListen)
var conf config.Config
err = conf.LoadFromJSON(args.Config)
if err != nil {
logger.Panic().Msgf("failed to load config: %v", err)
}
err = os.MkdirAll(filepath.Dir(args.DBFilePath), 0744)
if err != nil {
panic(fmt.Sprintf("failed to create db dir: %v", err))
}
db, err := buntdb.Open(args.DBFilePath)
if err != nil {
panic(err)
}
defer db.Close()
deployer := deployer.NewFileDeployer(db, conf, http.DefaultClient, logger)
server := server.NewHookHTTP(args.HTTPListen, deployer, logger)
wait := sync.WaitGroup{}
wait.Add(1)
go func() {
defer wait.Done()
server.Start()
logger.Info().Msg("http server done")
}()
wait.Add(1)
go func() {
defer wait.Done()
deployer.Start()
logger.Info().Msg("deployer done")
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
server.Stop()
deployer.Stop()
wait.Wait()
logger.Info().Msg("done")
}