-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (82 loc) · 2.49 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"embed"
"fmt"
"html/template"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/jritsema/gotoolbox"
"github.com/jritsema/gotoolbox/web"
)
var (
//go:embed all:templates/*
templateFS embed.FS
//parsed templates
html *template.Template
)
func startHTTPServer(dbConnection *Database, creds []Credential, hosts []Host) {
var err error
html, err = web.TemplateParseFSRecursive(templateFS, ".html", true, nil)
if err != nil {
panic(err)
}
router := http.NewServeMux()
router.Handle("/admin", authMiddleware(adminMiddleware(web.Action(adminHandler))))
router.Handle("/xml/", authMiddleware(http.StripPrefix("/xml/", http.FileServer(http.Dir("./xml")))))
router.Handle("/login", web.Action(loginHandler))
router.Handle("/logout", authMiddleware(web.Action(logoutHandler)))
router.Handle("/hosts", authMiddleware(web.Action(hostsHandler)))
router.Handle("/hosts/add", authMiddleware(web.Action(addHostsHandler)))
router.Handle("/creds", authMiddleware(web.Action(credsHandler)))
router.Handle("/creds/add", authMiddleware(web.Action(addCredsHandler)))
router.Handle("/scans", authMiddleware(web.Action(scansHandler)))
router.Handle("/scan/", authMiddleware(web.Action(scanHandler)))
router.Handle("/upload", authMiddleware(web.Action(uploadHandler)))
router.Handle("/", authMiddleware(web.Action(indexHandler)))
nextRequestID := func() string {
return fmt.Sprintf("%d", time.Now().UnixNano())
}
logger := log.New(os.Stdout, "http: ", log.LstdFlags)
middleware := tracing(nextRequestID)(logging(logger)(router))
port := gotoolbox.GetEnvWithDefault("PORT", "42069")
logger.Println("listening on http://localhost:" + port)
if err := http.ListenAndServe(":"+port, middleware); err != nil {
logger.Println("http.ListenAndServe():", err)
os.Exit(1)
}
}
func handleSigTerms() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
fmt.Println("received SIGTERM, exiting")
os.Exit(1)
}()
}
func main() {
handleSigTerms()
dbConnection, err := createDbConnection()
if err != nil {
log.Fatal(err)
}
createDbSchema(dbConnection)
creds, err := getCredentials(dbConnection)
if err != nil {
log.Print(err)
}
hosts, err := getHosts(dbConnection)
if err != nil {
log.Print(err)
}
password, err := createAdmin(dbConnection)
if err != nil {
log.Print(err)
}
fmt.Println("Please login with the following credentials:", "admin", password)
startHTTPServer(dbConnection, creds, hosts)
}