mirror of
https://github.com/1f349/violet.git
synced 2024-11-21 19:01:39 +00:00
Add active state to get request output
This commit is contained in:
parent
ce8c421187
commit
eb8e4c7ed7
@ -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 {
|
||||
|
@ -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)
|
||||
|
@ -12,53 +12,70 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type TargetApis struct {
|
||||
CreateRoute httprouter.Handle
|
||||
DeleteRoute httprouter.Handle
|
||||
CreateRedirect httprouter.Handle
|
||||
DeleteRedirect httprouter.Handle
|
||||
}
|
||||
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)
|
||||
apiError(rw, http.StatusInternalServerError, "Failed to insert route into database")
|
||||
return
|
||||
}
|
||||
manager.Compile()
|
||||
}))
|
||||
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)
|
||||
apiError(rw, http.StatusInternalServerError, "Failed to delete route from database")
|
||||
return
|
||||
}
|
||||
manager.Compile()
|
||||
}))
|
||||
|
||||
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) {
|
||||
err := manager.InsertRoute(target.Route(t))
|
||||
if err != nil {
|
||||
log.Printf("[Violet] Failed to insert route into database: %s\n", err)
|
||||
apiError(rw, http.StatusInternalServerError, "Failed to insert route into database")
|
||||
return
|
||||
}
|
||||
manager.Compile()
|
||||
}),
|
||||
DeleteRoute: 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)
|
||||
apiError(rw, http.StatusInternalServerError, "Failed to delete route from database")
|
||||
return
|
||||
}
|
||||
manager.Compile()
|
||||
}),
|
||||
CreateRedirect: 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)
|
||||
apiError(rw, http.StatusInternalServerError, "Failed to insert redirect into database")
|
||||
return
|
||||
}
|
||||
manager.Compile()
|
||||
}),
|
||||
DeleteRedirect: 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)
|
||||
apiError(rw, http.StatusInternalServerError, "Failed to delete redirect from database")
|
||||
return
|
||||
}
|
||||
manager.Compile()
|
||||
}),
|
||||
}
|
||||
return r
|
||||
// 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)
|
||||
apiError(rw, http.StatusInternalServerError, "Failed to insert redirect into database")
|
||||
return
|
||||
}
|
||||
manager.Compile()
|
||||
}))
|
||||
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)
|
||||
apiError(rw, http.StatusInternalServerError, "Failed to delete redirect from database")
|
||||
return
|
||||
}
|
||||
manager.Compile()
|
||||
}))
|
||||
}
|
||||
|
||||
type AuthWithJsonCallback[T any] func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, b AuthClaims, t T)
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user