package main
import (
"encoding/json"
"net/http"
)
var users map[string]string = map[string]string{}
func main() {
http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
server := r.URL.Query().Get("server")
if len(name) == 0 || len(server) == 0 {
w.WriteHeader(http.StatusBadRequest)
return
}
if _, isExists := users[name]; isExists {
w.WriteHeader(http.StatusConflict)
return
}
users[name] = server
})
http.HandleFunc("/logout", func(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
server := r.URL.Query().Get("server")
if len(name) == 0 || len(server) == 0 {
w.WriteHeader(http.StatusBadRequest)
return
}
user, userExists := users[name]
if !userExists || user != server {
w.WriteHeader(http.StatusNotFound)
return
}
delete(users, name)
w.WriteHeader(http.StatusOK)
})
http.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) {
bytes, err := json.Marshal(users)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write(bytes)
})
http.ListenAndServe(":1121", nil)
}