|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "github.com/LReg/time-tracker/internal/timeslot" |
| 6 | + "github.com/LReg/time-tracker/internal/ui" |
| 7 | + "github.com/LReg/time-tracker/internal/util" |
| 8 | + log "github.com/sirupsen/logrus" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + "strconv" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +func setCorsHeaders(writer http.ResponseWriter) { |
| 16 | + writer.Header().Set("Access-Control-Allow-Origin", ui.WebserverUrl) |
| 17 | + writer.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS") |
| 18 | + writer.Header().Set("Access-Control-Allow-Headers", "Content-Type") |
| 19 | +} |
| 20 | + |
| 21 | +func notFound(writer http.ResponseWriter) { |
| 22 | + writer.Header().Set("Content-Type", "text/plain; charset=utf-8") |
| 23 | + writer.WriteHeader(http.StatusNotFound) |
| 24 | + writer.Write([]byte("Not Found")) |
| 25 | +} |
| 26 | + |
| 27 | +func internalServerError(writer http.ResponseWriter) { |
| 28 | + writer.Header().Set("Content-Type", "text/plain; charset=utf-8") |
| 29 | + writer.WriteHeader(http.StatusInternalServerError) |
| 30 | + writer.Write([]byte("Error converting timeslot to json")) |
| 31 | +} |
| 32 | + |
| 33 | +func writeJson(writer http.ResponseWriter, err error, jsonTimeslots []byte) error { |
| 34 | + writer.Header().Set("Content-Type", "application/json; charset=utf-8") |
| 35 | + _, err = writer.Write(jsonTimeslots) |
| 36 | + return err |
| 37 | +} |
| 38 | + |
| 39 | +func StartTimeSlotApi() { |
| 40 | + mux := http.NewServeMux() |
| 41 | + |
| 42 | + mux.HandleFunc("/quit", func(writer http.ResponseWriter, request *http.Request) { |
| 43 | + setCorsHeaders(writer) |
| 44 | + writer.Header().Set("Content-Type", "text/plain; charset=utf-8") |
| 45 | + writer.WriteHeader(http.StatusOK) |
| 46 | + _, err := writer.Write([]byte("OK")) |
| 47 | + if err != nil { |
| 48 | + log.Error("Error writing response", err) |
| 49 | + } |
| 50 | + go func() { |
| 51 | + time.Sleep(1 * time.Second) |
| 52 | + os.Exit(0) |
| 53 | + }() |
| 54 | + }) |
| 55 | + |
| 56 | + mux.HandleFunc("/tt/{ms}", func(writer http.ResponseWriter, request *http.Request) { |
| 57 | + from, to := parseMS(request) |
| 58 | + timeslots := timeslot.GetTimeSlotsFromTo(from, to) |
| 59 | + |
| 60 | + setCorsHeaders(writer) |
| 61 | + |
| 62 | + if len(timeslots) == 0 { |
| 63 | + notFound(writer) |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + jsonTimeslots, err := json.Marshal(timeslots) |
| 68 | + if err != nil { |
| 69 | + log.Error("Error converting timeslot to json", err) |
| 70 | + internalServerError(writer) |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + err = writeJson(writer, err, jsonTimeslots) |
| 75 | + if err != nil { |
| 76 | + log.Error("Error writing response", err) |
| 77 | + } |
| 78 | + }) |
| 79 | + err := http.ListenAndServe(":8888", mux) |
| 80 | + if err != nil { |
| 81 | + log.Fatal("Could not start the webserver", err) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func parseMS(request *http.Request) (time.Time, time.Time) { |
| 86 | + ms := request.PathValue("ms") |
| 87 | + millis, err := strconv.ParseInt(ms, 10, 64) |
| 88 | + if err != nil { |
| 89 | + log.Error("Error converting ms to int", err) |
| 90 | + } |
| 91 | + |
| 92 | + date := time.UnixMilli(millis) |
| 93 | + from := util.GetLastMidnightFrom(date) |
| 94 | + to := util.GetLastMidnightFrom(date.Add(24 * time.Hour)) |
| 95 | + return from, to |
| 96 | +} |
0 commit comments