1
//go:generate statik -src=./public
2

3
// Copyright 2016 - 2020 The aurora Authors. All rights reserved. Use of this
4
// source code is governed by a MIT license that can be found in the LICENSE
5
// file.
6
//
7
// The aurora is a web-based beanstalkd queue server console written in Go
8
// and works on macOS, Linux and Windows machines. Main idea behind using Go
9
// for backend development is to utilize ability of the compiler to produce
10
// zero-dependency binaries for multiple platforms. aurora was created as an
11
// attempt to build very simple and portable application to work with local or
12
// remote beanstalkd server.
13
//
14
// See https://xuri.me/aurora for more information about aurora.
15
package main
16

17
import (
18
	"fmt"
19
	"net/http"
20
	"os"
21
	"os/signal"
22
	"runtime"
23
	"strings"
24
	"syscall"
25

26
	"github.com/rakyll/statik/fs"
27
	_ "github.com/xuri/aurora/statik"
28
)
29

30
// main function defines the entry point for the program if read config file or
31
// init failed, the application will be exit.
32
func main() {
33 0
	parseFlags()
34 0
	err := readConf()
35 0
	if err != nil {
36 0
		fmt.Println(err)
37 0
		os.Exit(1)
38
	}
39

40 0
	statikFS, err := fs.New()
41 0
	if err != nil {
42 0
		fmt.Println(err)
43 0
		os.Exit(1)
44
	}
45 0
	http.FileServer(statikFS)
46

47
	// handle static files include HTML, CSS and JavaScripts.
48 0
	http.Handle("/", http.StripPrefix("/", http.FileServer(statikFS)))
49 0
	http.HandleFunc("/public", basicAuth(handlerMain))
50 0
	http.HandleFunc("/index", basicAuth(handlerServerList))
51 0
	http.HandleFunc("/serversRemove", basicAuth(serversRemove))
52 0
	http.HandleFunc("/server", basicAuth(handlerServer))
53 0
	http.HandleFunc("/tube", basicAuth(handlerTube))
54 0
	http.HandleFunc("/sample", basicAuth(handlerSample))
55 0
	http.HandleFunc("/statistics", basicAuth(handlerStatistics))
56 0
	go func() {
57 0
		err = http.ListenAndServe(pubConf.Listen, nil)
58 0
		if err != nil {
59 0
			fmt.Println("Cant start server:", err)
60 0
			os.Exit(1)
61
		}
62
	}()
63 0
	go statistic()
64 0
	openPage()
65 0
	handleSignals()
66
}
67

68
// openPage function can be open system default browser automatic.
69
func openPage() {
70 10
	url := fmt.Sprintf("http://%v", pubConf.Listen)
71 10
	fmt.Println("To view beanstalkd console open", url, "in browser")
72 10
	if !pubConf.OpenPage.Enabled {
73 10
		return
74
	}
75 10
	var err error
76 10
	switch runtime.GOOS {
77 10
	case "linux", "freebsd", "openbsd", "netbsd":
78 10
		err = runCmd("xdg-open", url)
79 0
	case "darwin":
80 0
		err = runCmd("open", url)
81 0
	case "windows":
82 0
		r := strings.NewReplacer("&", "^&")
83 0
		err = runCmd("cmd", "/c", "start", r.Replace(url))
84 0
	default:
85 0
		err = fmt.Errorf("unsupported platform")
86
	}
87 0
	if err != nil {
88 0
		fmt.Println(err)
89
	}
90
}
91

92
// handleSignals handle kill signal.
93
func handleSignals() {
94 0
	c := make(chan os.Signal, 1)
95 0
	signal.Notify(c, os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)
96 0
	<-c
97
}

Read our documentation on viewing source code .

Loading