-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
88 lines (75 loc) · 1.65 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
package main
import (
"fmt"
"github.com/gorilla/mux"
"log"
"net/http"
"os"
"path"
)
// Check that DEPOT_ROOT is defined and can be accessed
func checkRoot(root string) {
if root == "" {
log.Fatalf("DEPOT_ROOT not defined")
}
info, err := os.Stat(root)
if err != nil {
log.Fatalf("DEPOT_ROOT error: %s", err)
} else if !info.IsDir() {
log.Fatalf("DEPOT_ROOT is not a directory")
}
tmpDir := path.Join(root, "tmp")
info, err = os.Stat(tmpDir);
if err != nil {
err = os.Mkdir(tmpDir, 0777)
if err != nil {
log.Fatalf("Couldn't create tmp dir")
}
} else if !info.IsDir() {
log.Fatalf("tmp dir is not a directory")
}
}
// Get the mux router
func getRouter(handlers *Handlers) *mux.Router {
r := mux.NewRouter()
r.HandleFunc("/", handlers.RootHandler)
r.HandleFunc("/assets/{filepath:.*}", handlers.AssetsHandler)
r.HandleFunc("/jobs/{job}/{filepath:.*}", handlers.JobsHandler)
r.HandleFunc("/zip", handlers.ZipHandler)
return r
}
// Server setup
func Server() {
// Check setup
root := os.Getenv("DEPOT_ROOT")
checkRoot(root)
user := os.Getenv("DEPOT_USER")
pass := os.Getenv("DEPOT_PASS")
deletion := os.Getenv("DEPOT_DISABLE_DELETION")
// Create handlers with root
handlers := &(Handlers{
root: root,
deletion: deletion == "",
})
// Define auth
auth := &(Auth{
user: user,
pass: pass,
})
// Routing
r := getRouter(handlers)
// Auth
r.Use(auth.Middleware)
// Port
addr := ":8080"
DepotPort := os.Getenv("DEPOT_PORT")
if DepotPort != "" {
addr = fmt.Sprintf(":%s", DepotPort)
}
// Serve
log.Printf("running at https://127.0.0.1%s", addr)
log.Fatal(http.ListenAndServe(addr, r))
}
func main() {
Server()
}