-
Notifications
You must be signed in to change notification settings - Fork 164
/
main.go
91 lines (78 loc) · 1.87 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
package main
import (
"encoding/json"
"log"
"net/http"
"os"
"path/filepath"
"runtime"
"./proxy"
"github.com/goji/httpauth"
"github.com/gorilla/mux"
"github.com/yvasiyarov/gorelic"
)
var cfg proxy.Config
func startProxy() {
if cfg.Threads > 0 {
runtime.GOMAXPROCS(cfg.Threads)
log.Printf("Running with %v threads", cfg.Threads)
} else {
n := runtime.NumCPU()
runtime.GOMAXPROCS(n)
log.Printf("Running with default %v threads", n)
}
r := mux.NewRouter()
s := proxy.NewEndpoint(&cfg)
go startFrontend(&cfg, s)
r.Handle("/miner/{diff:.+}/{id:.+}", s)
err := http.ListenAndServe(cfg.Proxy.Listen, r)
if err != nil {
log.Fatal(err)
}
}
func startFrontend(cfg *proxy.Config, s *proxy.ProxyServer) {
r := mux.NewRouter()
r.HandleFunc("/stats", s.StatsIndex)
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./www/")))
var err error
if len(cfg.Frontend.Password) > 0 {
auth := httpauth.SimpleBasicAuth(cfg.Frontend.Login, cfg.Frontend.Password)
err = http.ListenAndServe(cfg.Frontend.Listen, auth(r))
} else {
err = http.ListenAndServe(cfg.Frontend.Listen, r)
}
if err != nil {
log.Fatal(err)
}
}
func startNewrelic() {
if cfg.NewrelicEnabled {
nr := gorelic.NewAgent()
nr.Verbose = cfg.NewrelicVerbose
nr.NewrelicLicense = cfg.NewrelicKey
nr.NewrelicName = cfg.NewrelicName
nr.Run()
}
}
func readConfig(cfg *proxy.Config) {
configFileName := "config.json"
if len(os.Args) > 1 {
configFileName = os.Args[1]
}
configFileName, _ = filepath.Abs(configFileName)
log.Printf("Loading config: %v", configFileName)
configFile, err := os.Open(configFileName)
if err != nil {
log.Fatal("File error: ", err.Error())
}
defer configFile.Close()
jsonParser := json.NewDecoder(configFile)
if err = jsonParser.Decode(&cfg); err != nil {
log.Fatal("Config error: ", err.Error())
}
}
func main() {
readConfig(&cfg)
startNewrelic()
startProxy()
}