2020-04-23 07:42:16 +07:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2020-05-16 12:04:47 +07:00
|
|
|
var (
|
|
|
|
validTarget = make(map[string]bool)
|
|
|
|
)
|
|
|
|
|
2020-04-23 07:42:16 +07:00
|
|
|
func apiEndpoints() {
|
|
|
|
http.HandleFunc("/api/latest", allStatus)
|
2020-04-23 12:55:30 +07:00
|
|
|
http.HandleFunc("/api/history/day", dayHistory)
|
2020-04-23 13:44:23 +07:00
|
|
|
http.HandleFunc("/api/history/week", weekHistory)
|
2020-04-23 13:49:23 +07:00
|
|
|
http.HandleFunc("/api/history/month", monthHistory)
|
|
|
|
http.HandleFunc("/api/history/year", yearHistory)
|
2020-04-23 07:42:16 +07:00
|
|
|
}
|
|
|
|
|
2020-05-16 12:04:47 +07:00
|
|
|
func StartServer(port int, validTargets []string) {
|
|
|
|
for _, v := range validTargets {
|
|
|
|
validTarget[v] = true
|
|
|
|
}
|
2020-04-23 07:42:16 +07:00
|
|
|
apiEndpoints()
|
|
|
|
err := http.ListenAndServe(":"+strconv.Itoa(port), nil)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ListenAndServe returned an error: %v", err)
|
|
|
|
}
|
|
|
|
}
|