-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
146 lines (112 loc) · 3.53 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"backend/service"
"backend/view"
"backend/viewmodel"
"context"
"encoding/json"
"flag"
"fmt"
"log"
"math"
"net/http"
"os"
)
type APIServer struct {
listenAddr string
vm *viewmodel.TableViewModel
}
func (as *APIServer) ServeTempl(w http.ResponseWriter, r *http.Request) error {
entity := r.URL.Query().Get("entity")
sortKey := r.URL.Query().Get("sortKey")
page := r.URL.Query().Get("page")
as.vm.UpdateKinds(r.Context())
if entity != "" {
as.vm.SelectKind(entity)
}
if as.vm.Selected == "" {
view.Show(as.vm).Render(r.Context(), w)
return nil
}
if sortKey != "" {
as.vm.ToggleSortDirection()
as.vm.SortKey = sortKey
}
if page == "prev" && as.vm.HasPrevPage {
as.vm.CurrentPage -= 1
as.vm.HasPrevPage = as.vm.CurrentPage > 1
} else if page == "next" {
if as.vm.CurrentPage == as.vm.Pages {
as.vm.GetNewPage(r.Context())
} else {
as.vm.CurrentPage += 1
}
} else if len(as.vm.Entities) == 0 {
as.vm.GetNewPage(r.Context())
}
View := as.vm.Entities
if len(View) > 0 {
start := (as.vm.CurrentPage - 1) * as.vm.PageSize
end := int(math.Min(float64(as.vm.CurrentPage*as.vm.PageSize), float64(len(as.vm.Entities))))
View = View[start:end]
as.vm.Headers = service.GetTableHeaders(View)
as.vm.View = View
}
as.vm.DebugInfo()
view.Show(as.vm).Render(r.Context(), w)
return nil
}
type ApiFunc func(w http.ResponseWriter, r *http.Request) error
type HttpError struct {
Message string `json:"message"`
}
func WriteJSON(w http.ResponseWriter, status int, value any) {
w.WriteHeader(status)
w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(value)
}
func makeHttpHandler(f ApiFunc) http.HandlerFunc {
// log the endpoint
return func(w http.ResponseWriter, r *http.Request) {
fmt.Println("\n\n---------------------------------------------")
fmt.Println(r.Method, r.URL.Path)
fmt.Println("---------------------------------------------]\n")
if err := f(w, r); err != nil {
WriteJSON(w, http.StatusBadRequest, HttpError{
Message: err.Error(),
})
}
}
}
func usage() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
func main() {
port := flag.String("port", "8080", "Port for the emulator")
projectId := flag.String("project", "my-project", "Project ID for the emulator")
emulatorHost := flag.String("emuHost", "localhost:8081", "Host for the emulator")
emulatorHostPath := flag.String("emuHostPath", "localhost:8081/datastore", "Host path for the emulator")
datastoreHost := flag.String("dsHost", "http://localhost:8081", "Host for the datastore")
flag.Usage = usage
flag.Parse()
os.Setenv("DATASTORE_DATASET", *projectId)
os.Setenv("DATASTORE_EMULATOR_HOST", *emulatorHost)
os.Setenv("DATASTORE_EMULATOR_HOST_PATH", *emulatorHostPath)
os.Setenv("DATASTORE_HOST", *datastoreHost)
os.Setenv("DATASTORE_PROJECT_ID", *projectId)
fmt.Println("Starting server on port:", port)
ctx := context.Background()
client, err := service.NewDatastoreClient(ctx)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
vm := viewmodel.NewTableViewModel(client)
as := APIServer{vm: vm}
router := http.NewServeMux()
router.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("./public"))))
router.Handle("/vendor-js/", http.StripPrefix("/vendor-js/", http.FileServer(http.Dir("./vendor-js"))))
router.Handle("/js/", http.StripPrefix("/js/", http.FileServer(http.Dir("./js"))))
router.HandleFunc("/", makeHttpHandler(as.ServeTempl))
http.ListenAndServe("localhost:8080", router)
}