-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
145 lines (123 loc) · 3.17 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"errors"
"github.com/voyagegroup/popuko/queue"
"github.com/voyagegroup/popuko/setting"
)
var config *setting.Settings
var (
revision string
builddate string
)
func main() {
var configDir string
{
c := "Specify the config dir as absolute path. default: $" + setting.XdgConfigHomeEnvKey + "/" + setting.HomeDirName
flag.StringVar(&configDir, "config-base-dir", "", c)
}
var useTLS bool
{
c := "whether the server uses TLS (https://) or not (default: false)"
flag.BoolVar(&useTLS, "tls", false, c)
}
var certFile string
{
c := "Specify the absolute path to the cert file. (default: none)"
flag.StringVar(&certFile, "cert", "", c)
}
var keyFile string
{
c := "Specify the absolute path to the key file. (default: none)"
flag.StringVar(&keyFile, "key", "", c)
}
flag.Parse()
ok, root := setting.HomeDir(configDir)
if !ok {
log.Println("info: cannot find the config dir fot this.")
return
}
// Check whether the dir exists. If there is none, create it.
if _, err := os.Stat(root); err != nil {
if err := os.MkdirAll(root, os.ModePerm); err != nil {
log.Println("error: cannot create the config home dir.")
return
}
}
var certPath string
var keyPath string
if useTLS {
var err error
certPath, err = checkPath(certFile)
if err != nil {
log.Printf("error: use TLS, but `--cert` is invalid path: %v\n", err)
return
}
keyPath, err = checkPath(keyFile)
if err != nil {
log.Printf("error: use TLS, but `--key` is invalid path: %v\n", err)
return
}
}
config = setting.LoadSettings(root)
if config == nil {
log.Println("Cannot find $XDG_CONFIG_HOME/popuko" + setting.RootConfigFile)
return
}
log.Println("===== popuko =====")
log.Printf("version (git revision): %s\n", revision)
log.Printf("builddate: %s\n", builddate)
log.Printf("use TLS: %v\n", useTLS)
if useTLS {
log.Printf("cert: %v\n", certPath)
log.Printf("key: %v\n", keyPath)
}
log.Printf("listen http on port: %v\n", config.PortStr())
log.Printf("botname for GitHub: %v\n", "@"+config.BotNameForGithub())
log.Printf("config dir: %v\n", root)
log.Println("==================")
github, err := createGithubClient(config)
if err != nil {
log.Printf("error: %s\n", err.Error())
return
}
if err == nil && github == nil {
log.Println("error: Cannot create the github client")
return
}
q := queue.NewAutoMergeQRepo(root)
if q == nil {
log.Println("Fail to initialize the merge queue")
return
}
server := AppServer{
githubClient: github,
autoMergeRepo: q,
setting: config,
}
http.HandleFunc(prefixWebHookPath, server.handleGithubHook)
http.HandleFunc("/", server.handleRESTApiRequest)
if useTLS {
http.ListenAndServeTLS(config.PortStr(), certPath, keyPath, nil)
} else {
http.ListenAndServe(config.PortStr(), nil)
}
}
func checkPath(path string) (fullpath string, err error) {
if path == "" {
return "", errors.New("Not empty string")
}
p, err := filepath.Abs(path)
if err != nil {
return "", errors.New("Fail to parse the path")
}
if _, err := os.Stat(p); err != nil {
return "", fmt.Errorf("Not exist the file: %v", p)
}
return p, nil
}