Output slice of values

This commit is contained in:
Melon 2023-07-13 00:16:47 +01:00
parent eb8e4c7ed7
commit b2a53722e6
Signed by: melon
GPG Key ID: 6C9D970C50D26A25

View File

@ -15,16 +15,13 @@ import (
func SetupTargetApis(r *httprouter.Router, verify mjwt.Verifier, manager *router.Manager) { func SetupTargetApis(r *httprouter.Router, verify mjwt.Verifier, manager *router.Manager) {
// Endpoint for routes // Endpoint for routes
r.GET("/route", checkAuthWithPerm(verify, "violet:route", func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, b AuthClaims) { r.GET("/route", checkAuthWithPerm(verify, "violet:route", func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, b AuthClaims) {
routes, active, err := manager.GetAllRoutes() routes, err := manager.GetAllRoutes()
if err != nil { if err != nil {
apiError(rw, http.StatusInternalServerError, "Failed to get routes from database") apiError(rw, http.StatusInternalServerError, "Failed to get routes from database")
return return
} }
rw.WriteHeader(http.StatusOK) rw.WriteHeader(http.StatusOK)
_ = json.NewEncoder(rw).Encode(map[string]any{ _ = json.NewEncoder(rw).Encode(routes)
"routes": routes,
"active": active,
})
})) }))
r.POST("/route", parseJsonAndCheckOwnership[routeSource](verify, "route", func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, b AuthClaims, t routeSource) { r.POST("/route", parseJsonAndCheckOwnership[routeSource](verify, "route", func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, b AuthClaims, t routeSource) {
err := manager.InsertRoute(target.Route(t)) err := manager.InsertRoute(target.Route(t))
@ -47,16 +44,13 @@ func SetupTargetApis(r *httprouter.Router, verify mjwt.Verifier, manager *router
// Endpoint for redirects // Endpoint for redirects
r.GET("/redirect", checkAuthWithPerm(verify, "violet:redirect", func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, b AuthClaims) { r.GET("/redirect", checkAuthWithPerm(verify, "violet:redirect", func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, b AuthClaims) {
redirects, active, err := manager.GetAllRedirects() redirects, err := manager.GetAllRedirects()
if err != nil { if err != nil {
apiError(rw, http.StatusInternalServerError, "Failed to get redirects from database") apiError(rw, http.StatusInternalServerError, "Failed to get redirects from database")
return return
} }
rw.WriteHeader(http.StatusOK) rw.WriteHeader(http.StatusOK)
_ = json.NewEncoder(rw).Encode(map[string]any{ _ = json.NewEncoder(rw).Encode(redirects)
"redirects": redirects,
"active": active,
})
})) }))
r.POST("/redirect", parseJsonAndCheckOwnership[redirectSource](verify, "redirect", func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, b AuthClaims, t redirectSource) { r.POST("/redirect", parseJsonAndCheckOwnership[redirectSource](verify, "redirect", func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, b AuthClaims, t redirectSource) {
err := manager.InsertRedirect(target.Redirect(t)) err := manager.InsertRedirect(target.Redirect(t))