Update HTTP route function to new syntax in Go 1.22+
This commit is contained in:
210
routes/routes.go
210
routes/routes.go
@ -25,194 +25,110 @@ import (
|
|||||||
func SetupRoutes() *http.ServeMux {
|
func SetupRoutes() *http.ServeMux {
|
||||||
handler := http.NewServeMux()
|
handler := http.NewServeMux()
|
||||||
|
|
||||||
handler.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
handler.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.RequestURI {
|
indexHandler.GET(w, r)
|
||||||
case "/":
|
|
||||||
switch r.Method {
|
|
||||||
case http.MethodGet:
|
|
||||||
indexHandler.GET(w, r)
|
|
||||||
default:
|
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
w.WriteHeader(http.StatusNotFound)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
authRouter := http.NewServeMux()
|
handler.HandleFunc("GET /auth/google", func(w http.ResponseWriter, r *http.Request) {
|
||||||
handler.Handle("/auth/", http.StripPrefix("/auth", authRouter))
|
middleware.Guest(googleOauthHandler.GET, w, r)
|
||||||
authRouter.HandleFunc("/google", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
switch r.Method {
|
|
||||||
case http.MethodGet:
|
|
||||||
middleware.Guest(googleOauthHandler.GET, w, r)
|
|
||||||
default:
|
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
authRouter.HandleFunc("/totp", func(w http.ResponseWriter, r *http.Request) {
|
handler.HandleFunc("GET /auth/totp", func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.Method {
|
middleware.Guest(totpHandler.GET, w, r)
|
||||||
case http.MethodGet:
|
|
||||||
middleware.Guest(totpHandler.GET, w, r)
|
|
||||||
case http.MethodPost:
|
|
||||||
middleware.Guest(totpHandler.POST, w, r)
|
|
||||||
default:
|
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
authRouter.HandleFunc("/google/callback", func(w http.ResponseWriter, r *http.Request) {
|
handler.HandleFunc("POST /auth/totp", func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.Method {
|
middleware.Guest(totpHandler.POST, w, r)
|
||||||
case http.MethodGet:
|
|
||||||
middleware.Guest(googleOauthCallbackHandler.GET, w, r)
|
|
||||||
default:
|
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
authRouter.HandleFunc("/google/setup/{code}", func(w http.ResponseWriter, r *http.Request) {
|
handler.HandleFunc("GET /auth/google/callback", func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.Method {
|
middleware.Guest(googleOauthCallbackHandler.GET, w, r)
|
||||||
case http.MethodGet:
|
|
||||||
middleware.Guest(googleOauthSetupHandler.GET, w, r)
|
|
||||||
case http.MethodPost:
|
|
||||||
middleware.Guest(googleOauthSetupHandler.POST, w, r)
|
|
||||||
default:
|
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
handler.HandleFunc("/signin", func(w http.ResponseWriter, r *http.Request) {
|
handler.HandleFunc("GET /auth/google/setup/{code}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.Method {
|
middleware.Guest(googleOauthSetupHandler.GET, w, r)
|
||||||
case http.MethodGet:
|
})
|
||||||
middleware.Guest(signinHandler.GET, w, r)
|
handler.HandleFunc("POST /auth/google/setup/{code}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
case http.MethodPost:
|
middleware.Guest(googleOauthSetupHandler.POST, w, r)
|
||||||
middleware.Guest(signinHandler.POST, w, r)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
signupRouter := http.NewServeMux()
|
handler.HandleFunc("GET /signin", func(w http.ResponseWriter, r *http.Request) {
|
||||||
handler.Handle("/signup/", http.StripPrefix("/signup", signupRouter))
|
middleware.Guest(signinHandler.GET, w, r)
|
||||||
|
|
||||||
signupRouter.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
switch r.Method {
|
|
||||||
case http.MethodGet:
|
|
||||||
middleware.Guest(signupHandler.GET, w, r)
|
|
||||||
case http.MethodPost:
|
|
||||||
middleware.Guest(signupHandler.POST, w, r)
|
|
||||||
default:
|
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
signupRouter.HandleFunc("/verify/{code}", func(w http.ResponseWriter, r *http.Request) {
|
handler.HandleFunc("POST /signin", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
middleware.Guest(signinHandler.POST, w, r)
|
||||||
|
})
|
||||||
|
|
||||||
|
handler.HandleFunc("GET /signup", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
middleware.Guest(signupHandler.GET, w, r)
|
||||||
|
})
|
||||||
|
|
||||||
|
handler.HandleFunc("POST /signup", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
middleware.Guest(signupHandler.POST, w, r)
|
||||||
|
})
|
||||||
|
|
||||||
|
handler.HandleFunc("GET /signup/verify/{code}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
middleware.Guest(signupVerifyHandler.GET, w, r)
|
middleware.Guest(signupVerifyHandler.GET, w, r)
|
||||||
})
|
})
|
||||||
|
|
||||||
forgotPasswordRouter := http.NewServeMux()
|
handler.HandleFunc("GET /forgot-password", func(w http.ResponseWriter, r *http.Request) {
|
||||||
handler.Handle("/forgot-password/", http.StripPrefix("/forgot-password", forgotPasswordRouter))
|
middleware.Guest(forgotPasswordHandler.GET, w, r)
|
||||||
forgotPasswordRouter.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
switch r.Method {
|
|
||||||
case http.MethodGet:
|
|
||||||
middleware.Guest(forgotPasswordHandler.GET, w, r)
|
|
||||||
case http.MethodPost:
|
|
||||||
middleware.Guest(forgotPasswordHandler.POST, w, r)
|
|
||||||
default:
|
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
forgotPasswordRouter.HandleFunc("/verify/{code}", func(w http.ResponseWriter, r *http.Request) {
|
handler.HandleFunc("POST /forgot-password", func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.Method {
|
middleware.Guest(forgotPasswordHandler.POST, w, r)
|
||||||
case http.MethodGet:
|
|
||||||
middleware.Guest(forgotPasswordVerifyHandler.GET, w, r)
|
|
||||||
case http.MethodPost:
|
|
||||||
middleware.Guest(forgotPasswordVerifyHandler.POST, w, r)
|
|
||||||
default:
|
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
handler.HandleFunc("/user", func(w http.ResponseWriter, r *http.Request) {
|
handler.HandleFunc("GET /forgot-password/verify/{code}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.Method {
|
middleware.Guest(forgotPasswordVerifyHandler.GET, w, r)
|
||||||
case http.MethodGet:
|
|
||||||
middleware.Auth(userHandler.GET, w, r)
|
|
||||||
default:
|
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
handler.HandleFunc("/user/totp/setup", func(w http.ResponseWriter, r *http.Request) {
|
handler.HandleFunc("POST /forgot-password/verify/{code}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.Method {
|
middleware.Guest(forgotPasswordVerifyHandler.POST, w, r)
|
||||||
case http.MethodGet:
|
|
||||||
middleware.Auth(userHandlerTotpSetup.GET, w, r)
|
|
||||||
case http.MethodPost:
|
|
||||||
middleware.Auth(userHandlerTotpSetup.POST, w, r)
|
|
||||||
default:
|
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Upload router
|
handler.HandleFunc("GET /user", func(w http.ResponseWriter, r *http.Request) {
|
||||||
uploadRouter := http.NewServeMux()
|
middleware.Auth(userHandler.GET, w, r)
|
||||||
handler.Handle("/upload/", http.StripPrefix("/upload", uploadRouter))
|
|
||||||
|
|
||||||
uploadRouter.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
switch r.Method {
|
|
||||||
case http.MethodGet:
|
|
||||||
middleware.Auth(uploadHandler.GET, w, r)
|
|
||||||
default:
|
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
uploadRouter.HandleFunc("/{id}", func(w http.ResponseWriter, r *http.Request) {
|
handler.HandleFunc("GET /user/totp/setup", func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.Method {
|
middleware.Auth(userHandlerTotpSetup.GET, w, r)
|
||||||
case http.MethodPost:
|
|
||||||
middleware.Auth(uploadHandler.POST, w, r)
|
|
||||||
default:
|
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
uploadRouter.HandleFunc("/init", func(w http.ResponseWriter, r *http.Request) {
|
handler.HandleFunc("POST /user/totp/setup", func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.Method {
|
middleware.Auth(userHandlerTotpSetup.POST, w, r)
|
||||||
case http.MethodPost:
|
|
||||||
middleware.Auth(initialisation.POST, w, r)
|
|
||||||
default:
|
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Download router
|
handler.HandleFunc("GET /upload", func(w http.ResponseWriter, r *http.Request) {
|
||||||
downloadRouter := http.NewServeMux()
|
middleware.Auth(uploadHandler.GET, w, r)
|
||||||
handler.Handle("/download/", http.StripPrefix("/download", downloadRouter))
|
|
||||||
downloadRouter.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
switch r.Method {
|
|
||||||
case http.MethodGet:
|
|
||||||
middleware.Auth(downloadHandler.GET, w, r)
|
|
||||||
default:
|
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
downloadRouter.HandleFunc("/{id}", func(w http.ResponseWriter, r *http.Request) {
|
handler.HandleFunc("POST /upload/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.Method {
|
middleware.Auth(uploadHandler.POST, w, r)
|
||||||
case http.MethodGet:
|
|
||||||
downloadFileHandler.GET(w, r)
|
|
||||||
default:
|
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
handler.HandleFunc("/logout", func(w http.ResponseWriter, r *http.Request) {
|
handler.HandleFunc("POST /upload/init", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
middleware.Auth(initialisation.POST, w, r)
|
||||||
|
})
|
||||||
|
|
||||||
|
handler.HandleFunc("GET /download", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
middleware.Auth(downloadHandler.GET, w, r)
|
||||||
|
})
|
||||||
|
|
||||||
|
handler.HandleFunc("GET /download/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
downloadFileHandler.GET(w, r)
|
||||||
|
})
|
||||||
|
|
||||||
|
handler.HandleFunc("GET /logout", func(w http.ResponseWriter, r *http.Request) {
|
||||||
middleware.Auth(logoutHandler.GET, w, r)
|
middleware.Auth(logoutHandler.GET, w, r)
|
||||||
})
|
})
|
||||||
|
|
||||||
handler.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
|
handler.HandleFunc("GET /robots.txt", func(w http.ResponseWriter, r *http.Request) {
|
||||||
http.ServeFile(w, r, "public/robots.txt")
|
http.ServeFile(w, r, "public/robots.txt")
|
||||||
})
|
})
|
||||||
|
|
||||||
handler.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
|
handler.HandleFunc("GET /favicon.ico", func(w http.ResponseWriter, r *http.Request) {
|
||||||
http.ServeFile(w, r, "public/favicon.ico")
|
http.ServeFile(w, r, "public/favicon.ico")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user