This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
99 lines (74 loc) · 2.33 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
package main
import (
"encoding/json"
"flag"
"fmt"
"html/template"
"log"
"net/http"
"os"
"github.com/isotopsweden/docker-farmer/config"
"github.com/isotopsweden/docker-farmer/handlers"
"github.com/kardianos/osext"
)
var (
configFlag = flag.String("config", "config.json", "Path to config file")
publicFlag = flag.String("public", "public", "Path to public directory")
)
// path will return right path for file, looks at the
// given file first and then looks in the executable folder.
func realpath(file string) string {
if _, err := os.Stat(file); err == nil {
return file
}
path, _ := osext.ExecutableFolder()
if _, err := os.Stat(path + "/" + file); os.IsNotExist(err) {
return file
}
return path + "/" + file
}
func main() {
flag.Parse()
// Init config.
config.Init(realpath(*configFlag))
c := config.Get()
if c.Listen[0] == ':' {
c.Listen = "0.0.0.0" + c.Listen
}
// Index route.
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
links, err := json.Marshal(c.Links)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
templates := template.Must(template.ParseFiles(realpath(*publicFlag) + "/index.html"))
err = templates.ExecuteTemplate(w, "index.html", map[string]interface{}{
"Config": c,
"Links": template.JS(string(links)),
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
// Assets route.
http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir(realpath(*publicFlag)+"/assets"))))
// Config api route.
http.HandleFunc("/api/config", handlers.ConfigHandler)
// Containers api route.
http.HandleFunc("/api/containers", handlers.ContainersHandler)
// Database api route.
http.HandleFunc("/api/database", handlers.DatabaseHandler)
// BitBucket service route.
http.HandleFunc("/services/bitbucket", handlers.BitbucketHandler)
// GitHub service route.
http.HandleFunc("/services/github", handlers.GithubHandler)
// GitLab service route.
http.HandleFunc("/services/gitlab", handlers.GitlabHandler)
// Jira service route.
http.HandleFunc("/services/jira", handlers.JiraHandler)
// Test service route.
http.HandleFunc("/services/test", handlers.TestHandler)
fmt.Printf("Listening to http://%s\n", c.Listen)
// Listen to port.
log.Fatal(http.ListenAndServe(c.Listen, nil))
}