mirror of
https://github.com/1f349/violet.git
synced 2024-11-22 03:11:44 +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()
|
return rows.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Manager) GetAllRoutes() ([]target.Route, []bool, error) {
|
func (m *Manager) GetAllRoutes() ([]target.RouteWithActive, error) {
|
||||||
rSlice := make([]target.Route, 0)
|
s := make([]target.RouteWithActive, 0)
|
||||||
aSlice := make([]bool, 0)
|
|
||||||
|
|
||||||
query, err := m.db.Query(`SELECT source, destination, flags, active FROM routes`)
|
query, err := m.db.Query(`SELECT source, destination, flags, active FROM routes`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for query.Next() {
|
for query.Next() {
|
||||||
var a target.Route
|
var a target.RouteWithActive
|
||||||
var active bool
|
if query.Scan(&a.Src, &a.Dst, &a.Flags, &a.Active) != nil {
|
||||||
if query.Scan(&a.Src, &a.Dst, &a.Flags, &active) != nil {
|
return nil, err
|
||||||
return nil, nil, err
|
|
||||||
}
|
}
|
||||||
rSlice = append(rSlice, a)
|
s = append(s, a)
|
||||||
aSlice = append(aSlice, active)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return rSlice, aSlice, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Manager) InsertRoute(route target.Route) error {
|
func (m *Manager) InsertRoute(route target.Route) error {
|
||||||
@ -171,26 +168,23 @@ func (m *Manager) DeleteRoute(source string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Manager) GetAllRedirects() ([]target.Redirect, []bool, error) {
|
func (m *Manager) GetAllRedirects() ([]target.RedirectWithActive, error) {
|
||||||
rSlice := make([]target.Redirect, 0)
|
s := make([]target.RedirectWithActive, 0)
|
||||||
aSlice := make([]bool, 0)
|
|
||||||
|
|
||||||
query, err := m.db.Query(`SELECT source, destination, flags, code, active FROM redirects`)
|
query, err := m.db.Query(`SELECT source, destination, flags, code, active FROM redirects`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for query.Next() {
|
for query.Next() {
|
||||||
var a target.Redirect
|
var a target.RedirectWithActive
|
||||||
var active bool
|
if query.Scan(&a.Src, &a.Dst, &a.Flags, &a.Code, &a.Active) != nil {
|
||||||
if query.Scan(&a.Src, &a.Dst, &a.Flags, &a.Code, &active) != nil {
|
return nil, err
|
||||||
return nil, nil, err
|
|
||||||
}
|
}
|
||||||
rSlice = append(rSlice, a)
|
s = append(s, a)
|
||||||
aSlice = append(aSlice, active)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return rSlice, aSlice, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Manager) InsertRedirect(redirect target.Redirect) error {
|
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.PUT("/domain/:domain", domainFunc)
|
||||||
r.DELETE("/domain/:domain", domainFunc)
|
r.DELETE("/domain/:domain", domainFunc)
|
||||||
|
|
||||||
// Endpoint code for target routes/redirects
|
SetupTargetApis(r, conf.Signer, conf.Router)
|
||||||
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)
|
|
||||||
|
|
||||||
// Endpoint for acme-challenge
|
// Endpoint for acme-challenge
|
||||||
acmeChallengeFunc := acmeChallengeManage(conf.Signer, conf.Domains, conf.Acme)
|
acmeChallengeFunc := acmeChallengeManage(conf.Signer, conf.Domains, conf.Acme)
|
||||||
|
@ -12,16 +12,21 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TargetApis struct {
|
func SetupTargetApis(r *httprouter.Router, verify mjwt.Verifier, manager *router.Manager) {
|
||||||
CreateRoute httprouter.Handle
|
// Endpoint for routes
|
||||||
DeleteRoute httprouter.Handle
|
r.GET("/route", checkAuthWithPerm(verify, "violet:route", func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, b AuthClaims) {
|
||||||
CreateRedirect httprouter.Handle
|
routes, active, err := manager.GetAllRoutes()
|
||||||
DeleteRedirect httprouter.Handle
|
if err != nil {
|
||||||
}
|
apiError(rw, http.StatusInternalServerError, "Failed to get routes from database")
|
||||||
|
return
|
||||||
func SetupTargetApis(verify mjwt.Verifier, manager *router.Manager) *TargetApis {
|
}
|
||||||
r := &TargetApis{
|
rw.WriteHeader(http.StatusOK)
|
||||||
CreateRoute: parseJsonAndCheckOwnership[routeSource](verify, "route", func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, b AuthClaims, t routeSource) {
|
_ = 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))
|
err := manager.InsertRoute(target.Route(t))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[Violet] Failed to insert route into database: %s\n", err)
|
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
|
return
|
||||||
}
|
}
|
||||||
manager.Compile()
|
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)
|
err := manager.DeleteRoute(t.Src)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[Violet] Failed to delete route from database: %s\n", err)
|
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
|
return
|
||||||
}
|
}
|
||||||
manager.Compile()
|
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))
|
err := manager.InsertRedirect(target.Redirect(t))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[Violet] Failed to insert redirect into database: %s\n", err)
|
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
|
return
|
||||||
}
|
}
|
||||||
manager.Compile()
|
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)
|
err := manager.DeleteRedirect(t.Src)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[Violet] Failed to delete redirect from database: %s\n", err)
|
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
|
return
|
||||||
}
|
}
|
||||||
manager.Compile()
|
manager.Compile()
|
||||||
}),
|
}))
|
||||||
}
|
|
||||||
return r
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type AuthWithJsonCallback[T any] func(rw http.ResponseWriter, req *http.Request, params httprouter.Params, b AuthClaims, t T)
|
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
|
Code int `json:"code"` // status code used to redirect
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type RedirectWithActive struct {
|
||||||
|
Redirect
|
||||||
|
Active bool `json:"active"`
|
||||||
|
}
|
||||||
|
|
||||||
func (r Route) HasFlag(flag Flags) bool {
|
func (r Route) HasFlag(flag Flags) bool {
|
||||||
return r.Flags&flag != 0
|
return r.Flags&flag != 0
|
||||||
}
|
}
|
||||||
|
@ -43,6 +43,11 @@ type Route struct {
|
|||||||
Proxy *proxy.HybridTransport `json:"-"` // reverse proxy handler
|
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
|
// UpdateHeaders takes an existing set of headers and overwrites them with the
|
||||||
// extra headers.
|
// extra headers.
|
||||||
func (r Route) UpdateHeaders(header http.Header) {
|
func (r Route) UpdateHeaders(header http.Header) {
|
||||||
|
Loading…
Reference in New Issue
Block a user