-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
48 lines (38 loc) · 1.02 KB
/
api.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
package main
import (
"fmt"
"github.com/gorilla/mux"
"log"
"net/http"
"os"
)
// getMetrics processes data for the target file specified in configs.json
func getMetrics(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, processFile())
}
// spin server spins up the api endpoint
func spinServer() {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/getMetrics", getMetrics)
log.Fatal(http.ListenAndServe(":8080", router))
}
// parseArgs checks the flag at runtime to determine the course of action
func parseArgs() string {
args := os.Args
if len(os.Args) == 1 {
return "NULL"
}
return args[1]
}
func main() {
switch parseArgs() {
case "-t": // test output without spinnning up server
fmt.Println(processFile())
case "-r": // runs the api and spins up the server
spinServer()
fmt.Println("Server initialized on port 8080...")
default:
fmt.Printf("Usage:\n\t-r: (run) - spins up the server exposing to port 8080" +
"\n\t-t: (test) - test outputJSON without spinning up the server\n")
}
}