-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
main.go
90 lines (85 loc) · 2.5 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
// Copyright 2016 - 2021 The aurora Authors. All rights reserved. Use of this
// source code is governed by a MIT license that can be found in the LICENSE
// file.
//
// The aurora is a web-based beanstalkd queue server console written in Go
// and works on macOS, Linux and Windows machines. Main idea behind using Go
// for backend development is to utilize ability of the compiler to produce
// zero-dependency binaries for multiple platforms. aurora was created as an
// attempt to build very simple and portable application to work with local or
// remote beanstalkd server.
//
// See https://xuri.me/aurora for more information about aurora.
package main
import (
"embed"
"fmt"
"io/fs"
"net/http"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
)
//go:embed public
var staticFiles embed.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()
err := readConf()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
public, _ := fs.Sub(staticFiles, "public")
// handle static files include HTML, CSS and JavaScripts.
http.Handle("/", http.FileServer(http.FS(public)))
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 beanstalkd console open", url, "in browser")
if !pubConf.OpenPage.Enabled {
return
}
var err error
switch runtime.GOOS {
case "linux", "freebsd", "openbsd", "netbsd":
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, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)
<-c
}