-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.go
More file actions
136 lines (110 loc) · 2.82 KB
/
main.go
File metadata and controls
136 lines (110 loc) · 2.82 KB
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
package main
import (
"basicexample/helper"
v20230401 "basicexample/v20230401"
v20230501 "basicexample/v20230501"
"encoding/json"
"log"
"math/rand"
"net/http"
"os"
"os/signal"
"time"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
rms "github.com/subomi/requestmigrations"
)
func main() {
rm, err := rms.NewRequestMigration(
&rms.RequestMigrationOptions{
VersionHeader: "X-Example-Version",
CurrentVersion: "2023-05-01",
VersionFormat: rms.DateFormat,
})
if err != nil {
log.Fatal(err)
}
rm.RegisterMigrations(rms.MigrationStore{
"2023-05-01": []rms.Migration{
&v20230501.ListUserResponseMigration{},
},
"2023-04-01": []rms.Migration{
&v20230401.ListUserResponseMigration{},
},
})
api := &API{rm: rm, store: userStore}
backend := http.Server{
Addr: ":9000",
Handler: buildMux(api),
}
err = backend.ListenAndServe()
if err != nil {
log.Fatal(err)
}
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
}
func buildMux(api *API) http.Handler {
m := mux.NewRouter()
m.HandleFunc("/users", api.ListUser).Methods("GET")
m.HandleFunc("/users/{id}", api.GetUser).Methods("GET")
m.HandleFunc("/changelog", api.handleChangelog).Methods(http.MethodGet)
reg := prometheus.NewRegistry()
api.rm.RegisterMetrics(reg)
promHandler := promhttp.HandlerFor(reg, promhttp.HandlerOpts{Registry: reg})
m.Handle("/metrics", promHandler)
return m
}
// api models
// define api
type API struct {
store *Store
rm *rms.RequestMigration
}
func (a *API) ListUser(w http.ResponseWriter, r *http.Request) {
err, vw, rollback := a.rm.Migrate(r, "ListUser")
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
defer rollback(w)
// Generate a random Int type number between 1 and 10
randNum := rand.Intn(2-1+1) + 1
time.Sleep(time.Duration(randNum) * time.Second)
users, err := a.store.GetAll()
if err != nil {
w.WriteHeader(http.StatusBadGateway)
return
}
res, err := helper.GenerateSuccessResponse(users, "users retrieved successfully")
if err != nil {
w.WriteHeader(http.StatusBadGateway)
return
}
vw.Write(res)
}
func (a *API) GetUser(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
user, err := a.store.Get(vars["id"])
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
res, err := helper.GenerateSuccessResponse(user, "user retrieved successfully")
if err != nil {
w.WriteHeader(http.StatusBadGateway)
return
}
w.Write(res)
}
func (a *API) handleChangelog(w http.ResponseWriter, r *http.Request) {
changelog, err := a.rm.GenerateChangelog()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(changelog)
}