package main import ( "errors" "fmt" _ "github.com/go-sql-driver/mysql" "github.com/gorilla/mux" "log" "net/http" "strconv" ) const DEBUG = true func main() { err := setup() defer teardown() if err == nil { startServer() } } func setup() error { err := InitDb() if err != nil { return err } return nil } func teardown() { CloseDb() } func startServer() { port := "8001" r := mux.NewRouter() r.HandleFunc("/", showCharts).Methods("GET") r.HandleFunc("/since/{mins}", showCharts).Methods("GET") r.HandleFunc("/data/", sendData).Methods("GET") r.HandleFunc("/data/since/{mins}", sendData).Methods("GET") r.HandleFunc("/data/last/", sendLastSnapshot).Methods("GET") r.HandleFunc("/", saveSnapshot).Methods("POST") r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static/")))) http.Handle("/", r) fmt.Printf("Listening on port %s...\n", port) log.Fatal(http.ListenAndServe(":"+port, nil)) } func showCharts(w http.ResponseWriter, r *http.Request) { if countStr := mux.Vars(r)["mins"]; countStr != "" { if _, err := strconv.ParseInt(countStr, 10, 0); err != nil { http.Redirect(w, r, "/", 303) } } http.ServeFile(w, r, "charts.html") } func sendData(w http.ResponseWriter, r *http.Request) { var count int64 = 60 if vars := mux.Vars(r); vars["mins"] != "" { newCount, err := strconv.ParseInt(vars["mins"], 10, 0) if err != nil { sendInternalError(errors.New("bad snapshot count request"), w, r) return } count = newCount } records, err := getSnapshotRecordsFromDb(int(count)) if err != nil { sendInternalError(fmt.Errorf("couldn't read rows from the database: %w", err), w, r) return } json, err := createJsonFromSnapshotRecords(records) if err != nil { sendInternalError(fmt.Errorf("couldn't create a json from the records: %w", err), w, r) return } w.Header().Set("Content-Type", "application/json") _, err = fmt.Fprintf(w, string(json)) if err != nil { sendInternalError(err, w, r) return } } func sendLastSnapshot(w http.ResponseWriter, r *http.Request) { record, err := getLastSnapshotRecordFromDb() if err != nil { sendInternalError(fmt.Errorf("couldn't read last snapshot from the database: %w", err), w, r) return } json, err := createJsonFromSnapshotRecords([]*SnapshotRecord{record}) if err != nil { sendInternalError(fmt.Errorf("couldn't create a json from the last record: %w", err), w, r) return } w.Header().Set("Content-Type", "application/json") _, err = fmt.Fprintf(w, string(json)) if err != nil { sendInternalError(err, w, r) return } } func saveSnapshot(w http.ResponseWriter, r *http.Request) { snapshotSub, err := createSnapshotSubFromJsonBodyStream(r.Body) if err != nil { sendInternalError(fmt.Errorf("couldn't create snapshot from JSON: %w", err), w, r) } err = writeSnapshotToDb(snapshotSub) if err != nil { sendInternalError(fmt.Errorf("couldn't submit snapshot into the database: %w", err), w, r) } } func sendInternalError(err error, w http.ResponseWriter, r *http.Request) { errorMessage := "Internal Server Error!" if DEBUG { errorMessage += fmt.Sprintf(" Happened during %s request for pattern '%s': %s", r.Method, r.URL, err.Error()) } fmt.Println(errorMessage) http.Error(w, errorMessage, 500) }