forked from xuri/aurora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (77 loc) · 1.92 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
//go:generate statik -src=./public
package main
import (
"fmt"
"net/http"
"os"
"os/signal"
"runtime"
"strings"
_ "github.com/Luxurioust/aurora/statik"
"github.com/rakyll/statik/fs"
)
// main function defines the entry point for the program if read config file or
// init failed, the application will be exit.
func main() {
parseFlags()
var err error
err = readConf()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
statikFS, err := fs.New()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
http.FileServer(statikFS)
// handle static files include HTML, CSS and JavaScripts.
http.Handle("/", http.StripPrefix("/", http.FileServer(statikFS)))
http.HandleFunc("/public", basicAuth(handlerMain))
http.HandleFunc("/index", basicAuth(handlerServerList))
http.HandleFunc("/serversRemove", basicAuth(serversRemove))
http.HandleFunc("/server", basicAuth(handlerServer))
http.HandleFunc("/tube", basicAuth(handlerTube))
http.HandleFunc("/sample", basicAuth(handlerSample))
http.HandleFunc("/statistics", basicAuth(handlerStatistics))
go func() {
err = http.ListenAndServe(pubConf.Listen, nil)
if err != nil {
fmt.Println("Cant start server:", err)
os.Exit(1)
}
}()
go statistic()
openPage()
handleSignals()
}
// openPage function can be open system default browser automatic.
func openPage() {
url := fmt.Sprintf("http://%v", pubConf.Listen)
fmt.Println("To view beanstalk console open", url, "in browser")
if !pubConf.OpenPage.Enabled {
return
}
var err error
switch runtime.GOOS {
case "linux":
err = runCmd("xdg-open", url)
case "darwin":
err = runCmd("open", url)
case "windows":
r := strings.NewReplacer("&", "^&")
err = runCmd("cmd", "/c", "start", r.Replace(url))
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
fmt.Println(err)
}
}
// handleSignals handle kill signal.
func handleSignals() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
<-c
}