switch to Go 1.22-style routing

This commit is contained in:
Alexander Khodyrev 2024-09-28 13:14:15 +03:00
parent 354e6610fb
commit 63cd339f5e

View file

@ -46,21 +46,24 @@ func (s *visitsStore) visit(ending string) int {
return newVisits
}
type visitsHandler struct {
store *visitsStore
}
func (h *visitsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func commonHeaders(w http.ResponseWriter) {
// CORS: this is expected to be called from a different domain,
// hoops require jumping
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Max-Age", "86400")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
w.Header().Set("Allow", "POST, GET, OPTIONS")
}
func (store *visitsStore) handleOptions(w http.ResponseWriter, r *http.Request) {
commonHeaders(w)
w.WriteHeader(http.StatusOK)
}
func (store *visitsStore) handlePost(w http.ResponseWriter, r *http.Request) {
commonHeaders(w)
w.Header().Set("content-type", "application/json")
if r.Method == http.MethodPost {
var s string
if err := json.NewDecoder(r.Body).Decode(&s); err != nil {
w.WriteHeader(http.StatusInternalServerError)
@ -68,13 +71,15 @@ func (h *visitsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
w.WriteHeader(http.StatusOK)
w.Write(([]byte)(strconv.Itoa(h.store.visit(s))))
return
}
if r.Method == http.MethodGet {
h.store.Lock()
jsonBytes, err := json.Marshal(h.store.m)
h.store.Unlock()
w.Write(([]byte)(strconv.Itoa(store.visit(s))))
}
func (store *visitsStore) handleGet(w http.ResponseWriter, r *http.Request) {
commonHeaders(w)
w.Header().Set("content-type", "application/json")
store.Lock()
jsonBytes, err := json.Marshal(store.m)
store.Unlock()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("cannot serialize"))
@ -82,14 +87,6 @@ func (h *visitsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
w.WriteHeader(http.StatusOK)
w.Write(jsonBytes)
return
}
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusOK)
return
}
w.Header().Set("Allow", "POST, GET, OPTIONS")
w.WriteHeader(http.StatusMethodNotAllowed)
}
func main() {
@ -104,11 +101,12 @@ func main() {
} else {
log.Println("initial file", *initialFile, "could not be read, defaulting to empty data")
}
store := visitsStore{m: initialVisits, RWMutex: &sync.RWMutex{}}
visitsH := &visitsHandler{store: &store}
http.Handle("/", visitsH)
log.Println("Starting web server...")
http.HandleFunc("GET /", store.handleGet)
http.HandleFunc("POST /", store.handlePost)
http.HandleFunc("OPTIONS /", store.handleOptions)
log.Println("Starting web server at ", *addr, "...")
http.ListenAndServe(*addr, nil)
}