-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_server.go
55 lines (43 loc) · 1.31 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
// HTTP server code
package main
import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"net/http"
)
func initHttpServer() {
// Define a handler function to handle HTTP requests
http.HandleFunc("/api/v1", entriesApiHandler)
// avatars.go
http.HandleFunc("/avatars/", avatarHandler)
// statuses.go
http.HandleFunc("/statusCheck/", statusCheckHandler)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Serve the file using the file server
log.Info("Filesystem request: ", r.Method, " ", r.URL.Path)
http.FileServer(http.Dir(compiledVuePath)).ServeHTTP(w, r)
})
// Specify the port to listen on
port := 8080
addr := fmt.Sprintf(":%d", port)
// Start the HTTP server
log.Info("Server is running on ", addr)
err := http.ListenAndServe(addr, nil)
if err != nil {
log.Fatal("Error starting server: ", err)
}
}
func entriesApiHandler(w http.ResponseWriter, r *http.Request) {
log.Info("Api request: ", r.Method, " ", r.URL.Path)
// Set the content type to JSON
w.Header().Set("Content-Type", "application/json")
// Marshal the dashboardItems to JSON
jsonData, err := json.Marshal(dashboardItems.get())
if err != nil {
http.Error(w, "Error marshalling JSON", http.StatusInternalServerError)
return
}
// Write the JSON data to the response writer
w.Write(jsonData)
}