-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
84 lines (78 loc) · 2.39 KB
/
main.go
File metadata and controls
84 lines (78 loc) · 2.39 KB
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
package main
import (
"context"
"flag"
"fmt"
"github.com/san-lab/toolsmith/httphandler"
"log"
"net/http"
"os"
"os/signal"
"strconv"
"sync"
)
//Parsing flags "ethport" and "host"
//Initializing EthRPC client
//TODO: Take care of the favicon.ico location
func main() {
ethRPCAddress := flag.String("ethRPCAddress", "localhost:8545", "default RPC access point")
httpPortF := flag.String("httpPort", "8090", "http port")
mockMode := flag.Bool("mockMode", false, "should mock http RPC client")
dumpRPC := flag.Bool("dumpRPC", false, "should dump RPC responses to files")
startWatchdog := flag.Bool("startWatchdog", false, "should a watchdog be started")
withBasicAuth := flag.Bool("withAuth", true, "should Basic Authentication be enabled")
httpsPortF := flag.Int("httpsPort", 0, "https port. tls not started if not provided. requires server.crt & server.key")
flag.Parse()
c := httphandler.Config{}
c.RPCFirstEntry = *ethRPCAddress
httpPort := *httpPortF
httpsPort := *httpsPortF
c.MockMode = *mockMode
c.DumpRPC = *dumpRPC
c.StartWatchdog = *startWatchdog
c.BasicAuth = *withBasicAuth
fmt.Println("Here")
interruptChan := make(chan os.Signal)
wg := &sync.WaitGroup{}
signal.Notify(interruptChan, os.Interrupt)
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
ctx = context.WithValue(ctx, "WaitGroup", wg)
handler, err := httphandler.NewHttpHandler(c, ctx)
if err != nil {
panic(err)
}
http.HandleFunc("/favicon.ico", favIconHandler)
//Beware! This config means that all the static images - also the ones called from the templates -
// have to be addressed as "/static/*", regardless of the location of the template
fs := http.FileServer(http.Dir("static"))
http.HandleFunc("/static/", http.StripPrefix("/"+
"static", fs).ServeHTTP)
http.HandleFunc("/", handler.GetHandler(*withBasicAuth))
srv := http.Server{Addr: ":" + httpPort}
var tlsSrv http.Server
if httpsPort > 0 {
tlsSrv = http.Server{Addr: ":" + strconv.Itoa(httpsPort)}
}
go func() {
select {
case <-interruptChan:
cancel()
srv.Shutdown(context.TODO())
if httpsPort > 0 {
tlsSrv.Shutdown(context.TODO())
}
return
}
}()
if httpsPort > 0 {
go func() {
log.Println(tlsSrv.ListenAndServeTLS("server.crt", "server.key"))
}()
}
log.Println(srv.ListenAndServe())
wg.Wait()
}
func favIconHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/build_32x32.png")
}