-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
46 lines (39 loc) · 1007 Bytes
/
main.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
package main
import (
"fmt"
"net/http"
"strconv"
"time"
"go.uber.org/zap"
"github.com/castus/lights-api/api"
)
func main() {
logger, _ := zap.NewProduction()
log := logger.Sugar()
defer logger.Sync()
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
queryParameters := r.URL.Query()
timestamp := queryParameters.Get("timestamp")
parsedTime, err := strconv.ParseInt(timestamp, 10, 64)
fmt.Println(parsedTime)
if err != nil {
panic(err)
}
unixTime := time.Unix(parsedTime, 0)
lightValue := api.Get(unixTime)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Fatal(err)
} else {
w.WriteHeader(http.StatusOK)
}
w.Header().Set("Content-Type", "plain/text")
_, _ = fmt.Fprintf(w, lightValue)
})
port := "8080"
log.Infow("API server is running", "port", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}