Add active state to get request output

This commit is contained in:
Melon 2023-07-13 00:15:00 +01:00
parent ce8c421187
commit eb8e4c7ed7
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
5 changed files with 90 additions and 102 deletions

View File

@ -139,26 +139,23 @@ func (m *Manager) internalCompile(router *Router) error {
return rows.Err()
}
func (m *Manager) GetAllRoutes() ([]target.Route, []bool, error) {
rSlice := make([]target.Route, 0)
aSlice := make([]bool, 0)
func (m *Manager) GetAllRoutes() ([]target.RouteWithActive, error) {
s := make([]target.RouteWithActive, 0)
query, err := m.db.Query(`SELECT source, destination, flags, active FROM routes`)
if err != nil {
return nil, nil, err
return nil, err
}
for query.Next() {
var a target.Route
var active bool
if query.Scan(&a.Src, &a.Dst, &a.Flags, &active) != nil {
return nil, nil, err
var a target.RouteWithActive
if query.Scan(&a.Src, &a.Dst, &a.Flags, &a.Active) != nil {
return nil, err
}
rSlice = append(rSlice, a)
aSlice = append(aSlice, active)
s = append(s, a)
}
return rSlice, aSlice, nil
return s, nil
}
func (m *Manager) InsertRoute(route target.Route) error {
@ -171,26 +168,23 @@ func (m *Manager) DeleteRoute(source string) error {
return err
}
func (m *Manager) GetAllRedirects() ([]target.Redirect, []bool, error) {
rSlice := make([]target.Redirect, 0)
aSlice := make([]bool, 0)
func (m *Manager) GetAllRedirects() ([]target.RedirectWithActive, error) {
s := make([]target.RedirectWithActive, 0)
query, err := m.db.Query(`SELECT source, destination, flags, code, active FROM redirects`)
if err != nil {
return nil, nil, err
return nil, err
}
for query.Next() {
var a target.Redirect
var active bool
if query.Scan(&a.Src, &a.Dst, &a.Flags, &a.Code, &active) != nil {
return nil, nil, err
var a target.RedirectWithActive
if query.Scan(&a.Src, &a.Dst, &a.Flags, &a.Code, &a.Active) != nil {
return nil, err
}
rSlice = append(rSlice, a)
aSlice = append(aSlice, active)
s = append(s, a)
}
return rSlice, aSlice, nil
return s, nil
}
func (m *Manager) InsertRedirect(redirect target.Redirect) error {

View File

@ -30,40 +30,7 @@ func NewApiServer(conf *conf.Conf, compileTarget utils.MultiCompilable) *http.Se
r.PUT("/domain/:domain", domainFunc)
r.DELETE("/domain/:domain", domainFunc)
// Endpoint code for target routes/redirects
targetApis := SetupTargetApis(conf.Signer, conf.Router)
// Endpoint for routes
r.GET("/route", checkAuthWithPerm(conf.Signer, "violet:route", func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, b AuthClaims) {
routes, active, err := conf.Router.GetAllRoutes()
if err != nil {
apiError(rw, http.StatusInternalServerError, "Failed to get routes from database")
return
}
rw.WriteHeader(http.StatusOK)
_ = json.NewEncoder(rw).Encode(map[string]any{
"routes": routes,
"active": active,
})
}))
r.POST("/route", targetApis.CreateRoute)
r.DELETE("/route", targetApis.DeleteRoute)
// Endpoint for redirects
r.GET("/redirect", checkAuthWithPerm(conf.Signer, "violet:redirect", func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, b AuthClaims) {
redirects, active, err := conf.Router.GetAllRedirects()
if err != nil {
apiError(rw, http.StatusInternalServerError, "Failed to get redirects from database")
return
}
rw.WriteHeader(http.StatusOK)
_ = json.NewEncoder(rw).Encode(map[string]any{
"redirects": redirects,
"active": active,
})
}))
r.POST("/redirect", targetApis.CreateRedirect)
r.DELETE("/redirect", targetApis.DeleteRedirect)
SetupTargetApis(r, conf.Signer, conf.Router)
// Endpoint for acme-challenge
acmeChallengeFunc := acmeChallengeManage(conf.Signer, conf.Domains, conf.Acme)

View File

@ -12,16 +12,21 @@ import (
"strings"
)
type TargetApis struct {
CreateRoute httprouter.Handle
DeleteRoute httprouter.Handle
CreateRedirect httprouter.Handle
DeleteRedirect httprouter.Handle
}
func SetupTargetApis(verify mjwt.Verifier, manager *router.Manager) *TargetApis {
r := &TargetApis{
CreateRoute: parseJsonAndCheckOwnership[routeSource](verify, "route", func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, b AuthClaims, t routeSource) {
func SetupTargetApis(r *httprouter.Router, verify mjwt.Verifier, manager *router.Manager) {
// Endpoint for routes
r.GET("/route", checkAuthWithPerm(verify, "violet:route", func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, b AuthClaims) {
routes, active, err := manager.GetAllRoutes()
if err != nil {
apiError(rw, http.StatusInternalServerError, "Failed to get routes from database")
return
}
rw.WriteHeader(http.StatusOK)
_ = json.NewEncoder(rw).Encode(map[string]any{
"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) {
err := manager.InsertRoute(target.Route(t))
if err != nil {
log.Printf("[Violet] Failed to insert route into database: %s\n", err)
@ -29,8 +34,8 @@ func SetupTargetApis(verify mjwt.Verifier, manager *router.Manager) *TargetApis
return
}
manager.Compile()
}),
DeleteRoute: parseJsonAndCheckOwnership[sourceJson](verify, "route", func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, b AuthClaims, t sourceJson) {
}))
r.DELETE("/route", parseJsonAndCheckOwnership[sourceJson](verify, "route", func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, b AuthClaims, t sourceJson) {
err := manager.DeleteRoute(t.Src)
if err != nil {
log.Printf("[Violet] Failed to delete route from database: %s\n", err)
@ -38,8 +43,22 @@ func SetupTargetApis(verify mjwt.Verifier, manager *router.Manager) *TargetApis
return
}
manager.Compile()
}),
CreateRedirect: parseJsonAndCheckOwnership[redirectSource](verify, "redirect", func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, b AuthClaims, t redirectSource) {
}))
// Endpoint for redirects
r.GET("/redirect", checkAuthWithPerm(verify, "violet:redirect", func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, b AuthClaims) {
redirects, active, err := manager.GetAllRedirects()
if err != nil {
apiError(rw, http.StatusInternalServerError, "Failed to get redirects from database")
return
}
rw.WriteHeader(http.StatusOK)
_ = json.NewEncoder(rw).Encode(map[string]any{
"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) {
err := manager.InsertRedirect(target.Redirect(t))
if err != nil {
log.Printf("[Violet] Failed to insert redirect into database: %s\n", err)
@ -47,8 +66,8 @@ func SetupTargetApis(verify mjwt.Verifier, manager *router.Manager) *TargetApis
return
}
manager.Compile()
}),
DeleteRedirect: parseJsonAndCheckOwnership[sourceJson](verify, "redirect", func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, b AuthClaims, t sourceJson) {
}))
r.DELETE("/redirect", parseJsonAndCheckOwnership[sourceJson](verify, "redirect", func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, b AuthClaims, t sourceJson) {
err := manager.DeleteRedirect(t.Src)
if err != nil {
log.Printf("[Violet] Failed to delete redirect from database: %s\n", err)
@ -56,9 +75,7 @@ func SetupTargetApis(verify mjwt.Verifier, manager *router.Manager) *TargetApis
return
}
manager.Compile()
}),
}
return r
}))
}
type AuthWithJsonCallback[T any] func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, b AuthClaims, t T)

View File

@ -18,6 +18,11 @@ type Redirect struct {
Code int `json:"code"` // status code used to redirect
}
type RedirectWithActive struct {
Redirect
Active bool `json:"active"`
}
func (r Route) HasFlag(flag Flags) bool {
return r.Flags&flag != 0
}

View File

@ -43,6 +43,11 @@ type Route struct {
Proxy *proxy.HybridTransport `json:"-"` // reverse proxy handler
}
type RouteWithActive struct {
Route
Active bool `json:"active"`
}
// UpdateHeaders takes an existing set of headers and overwrites them with the
// extra headers.
func (r Route) UpdateHeaders(header http.Header) {