-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathserver.go
117 lines (97 loc) · 2.12 KB
/
server.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
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"flag"
"fmt"
"io"
"log"
"net/http"
_ "github.com/go-sql-driver/mysql"
"github.com/sarchlab/akita/v3/daisen/static"
"github.com/sarchlab/akita/v3/tracing"
)
var (
httpFlag = flag.String("http",
"0.0.0.0:3001",
"HTTP service address (e.g., ':6060')")
mySQLDBName = flag.String("mysql",
"",
"Name of the MySQL database to connect to.")
csvFileName = flag.String("csv",
"",
"Name of the CSV file to read from.")
sqliteFileName = flag.String("sqlite",
"",
"Name of the SQLite file to read from.")
traceReader tracing.TraceReader
fs http.FileSystem
)
func main() {
parseArgs()
fs = static.GetAssets()
startServer()
}
func parseArgs() {
flag.Parse()
mustBeOneAndOnlyOneSource()
}
func mustBeOneAndOnlyOneSource() {
numSources := 0
if *mySQLDBName != "" {
numSources++
}
if *csvFileName != "" {
numSources++
}
if *sqliteFileName != "" {
numSources++
}
if numSources != 1 {
flag.PrintDefaults()
panic("Must specify one and only one of -mysql, -csv, or -sqlite")
}
}
func startServer() {
connectToDB()
startAPIServer()
}
func connectToDB() {
switch {
case *mySQLDBName != "":
db := tracing.NewMySQLTraceReader(*mySQLDBName)
db.Init()
traceReader = db
case *csvFileName != "":
db := tracing.NewCSVTraceReader(*csvFileName)
traceReader = db
case *sqliteFileName != "":
db := tracing.NewSQLiteTraceReader(*sqliteFileName)
db.Init()
traceReader = db
}
}
func startAPIServer() {
http.Handle("/", http.FileServer(fs))
http.HandleFunc("/dashboard", serveIndex)
http.HandleFunc("/component", serveIndex)
http.HandleFunc("/task", serveIndex)
http.HandleFunc("/api/trace", httpTrace)
http.HandleFunc("/api/compnames", httpComponentNames)
http.HandleFunc("/api/compinfo", httpComponentInfo)
fmt.Printf("Listening %s\n", *httpFlag)
err := http.ListenAndServe(*httpFlag, nil)
dieOnErr(err)
}
func serveIndex(w http.ResponseWriter, r *http.Request) {
var err error
f, err := fs.Open("index.html")
dieOnErr(err)
p, err := io.ReadAll(f)
dieOnErr(err)
_, err = w.Write(p)
dieOnErr(err)
}
func dieOnErr(err error) {
if err != nil {
log.Panic(err)
}
}