added logout endpoint

This commit is contained in:
yusing 2024-10-31 00:58:35 +08:00
parent 669d13b89a
commit 4ee93a1351
3 changed files with 27 additions and 0 deletions

View file

@ -29,6 +29,8 @@ func NewHandler() http.Handler {
// mux.HandleFunc("GET", "/v1/checkhealth", v1.CheckHealth)
// mux.HandleFunc("HEAD", "/v1/checkhealth", v1.CheckHealth)
mux.HandleFunc("POST", "/v1/login", auth.LoginHandler)
mux.HandleFunc("GET", "/v1/logout", auth.LogoutHandler)
mux.HandleFunc("POST", "/v1/logout", auth.LogoutHandler)
mux.HandleFunc("POST", "/v1/reload", v1.Reload)
mux.HandleFunc("GET", "/v1/list", auth.RequireAuth(v1.List))
mux.HandleFunc("GET", "/v1/list/{what}", auth.RequireAuth(v1.List))

View file

@ -80,6 +80,19 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
func LogoutHandler(w http.ResponseWriter, r *http.Request) {
http.SetCookie(w, &http.Cookie{
Name: "token",
Value: "",
Expires: time.Unix(0, 0),
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
Path: "/",
})
w.Header().Set("location", "/login")
w.WriteHeader(http.StatusTemporaryRedirect)
}
func RequireAuth(next http.HandlerFunc) http.HandlerFunc {
if common.IsDebugSkipAuth {
return next

View file

@ -0,0 +1,12 @@
package middleware
type (
rateLimiter struct {
*rateLimiterOpts
m *Middleware
}
rateLimiterOpts struct {
Count int `json:"count"`
}
)