/
main.go
86 lines (72 loc) · 1.45 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"github.com/groob/ape/api"
)
func init() {
initFlag()
checkRepo()
}
func main() {
var opts api.ServerOptions
// add jwt auth
if *flJWT {
jwtOpt := api.JWTAuth(*flJWTSecret)
opts = append(opts, jwtOpt)
}
// add basic auth
if *flBasic {
opts = append(opts, api.BasicAuth())
}
// configure repo
repo := api.SimpleRepo(*flRepo)
opts = append(opts, repo)
// create handler
apiHandler := api.NewServer(opts...)
http.Handle("/", apiHandler)
// serve http or https
serve()
}
func serve() {
if *flTLS {
serveTLS()
}
serveHTTP()
}
func serveHTTP() {
port := fmt.Sprintf(":%v", *flPort)
log.Fatal(http.ListenAndServe(port, nil))
}
func serveTLS() {
port := fmt.Sprintf(":%v", *flPort)
log.Fatal(http.ListenAndServeTLS(port, *flTLSCert, *flTLSKey, nil))
}
func checkRepo() {
pkgsinfoPath := fmt.Sprintf("%v/pkgsinfo/", *flRepo)
createDir(pkgsinfoPath)
manifestPath := fmt.Sprintf("%v/manifests/", *flRepo)
createDir(manifestPath)
pkgsPath := fmt.Sprintf("%v/pkgs/", *flRepo)
createDir(pkgsPath)
catalogsPath := fmt.Sprintf("%v/catalogs/", *flRepo)
createDir(catalogsPath)
}
func createDir(path string) {
if !dirExists(path) {
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0755); err != nil {
log.Fatalf("%v must exits", path)
}
}
}
func dirExists(path string) bool {
_, err := os.Stat(path)
if os.IsNotExist(err) {
return false
}
return true
}