-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-server.go
57 lines (47 loc) · 1.29 KB
/
http-server.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
49
50
51
52
53
54
55
56
57
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"strings"
"sync"
"time"
)
type endPoint struct {
LastImport time.Time `json:"lastImport"`
NextImport time.Time `json:"nextImport"`
ImportRunning bool `json:"importRunning"`
}
var statusEndPoint endPoint
func serveStatusEndPoint(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
statusEndPoint.NextImport = Cron.Entries()[0].Schedule.Next(time.Now())
if err := json.NewEncoder(w).Encode(statusEndPoint); err != nil {
fmt.Println(err.Error())
}
}
func serveLog(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text") //Check text is valid content type
logID := strings.TrimPrefix(r.URL.Path, "/logs/")
logFileBytes, err := os.ReadFile(fmt.Sprintf("/logs/%s.log", logID))
if err != nil {
w.Write([]byte(err.Error()))
return
}
w.Write(logFileBytes)
}
func startHttpServer(waitGroup *sync.WaitGroup) {
http.HandleFunc("/status", serveStatusEndPoint)
http.HandleFunc("/logs/", serveLog)
fmt.Printf("json endpoint available at %s/status\n", httpBaseURL)
waitGroup.Done()
err := http.ListenAndServe(fmt.Sprintf(":%d", httpListenPort), nil)
switch {
case errors.Is(err, http.ErrServerClosed):
os.Exit(1)
case err != nil:
os.Exit(1)
}
}