switch to Go 1.22-style routing
This commit is contained in:
parent
354e6610fb
commit
63cd339f5e
1 changed files with 39 additions and 41 deletions
|
|
@ -46,50 +46,47 @@ func (s *visitsStore) visit(ending string) int {
|
||||||
return newVisits
|
return newVisits
|
||||||
}
|
}
|
||||||
|
|
||||||
type visitsHandler struct {
|
func commonHeaders(w http.ResponseWriter) {
|
||||||
store *visitsStore
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *visitsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
||||||
// CORS: this is expected to be called from a different domain,
|
// CORS: this is expected to be called from a different domain,
|
||||||
// hoops require jumping
|
// hoops require jumping
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
w.Header().Set("Access-Control-Max-Age", "86400")
|
w.Header().Set("Access-Control-Max-Age", "86400")
|
||||||
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
|
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("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
|
||||||
|
|
||||||
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)
|
|
||||||
w.Write([]byte("cannot parse data"))
|
|
||||||
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()
|
|
||||||
if err != nil {
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
w.Write([]byte("cannot serialize"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
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.Header().Set("Allow", "POST, GET, OPTIONS")
|
||||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
var s string
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&s); err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
w.Write([]byte("cannot parse data"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
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"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write(jsonBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
@ -104,11 +101,12 @@ func main() {
|
||||||
} else {
|
} else {
|
||||||
log.Println("initial file", *initialFile, "could not be read, defaulting to empty data")
|
log.Println("initial file", *initialFile, "could not be read, defaulting to empty data")
|
||||||
}
|
}
|
||||||
|
|
||||||
store := visitsStore{m: initialVisits, RWMutex: &sync.RWMutex{}}
|
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)
|
http.ListenAndServe(*addr, nil)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue