diff --git a/router/manager.go b/router/manager.go index 85208f1..4b04279 100644 --- a/router/manager.go +++ b/router/manager.go @@ -139,30 +139,66 @@ 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) + + query, err := m.db.Query(`SELECT source, destination, flags, active FROM routes`) + if err != nil { + return nil, 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 + } + rSlice = append(rSlice, a) + aSlice = append(aSlice, active) + } + + return rSlice, aSlice, nil +} + func (m *Manager) InsertRoute(route target.Route) error { - m.s.Lock() - defer m.s.Unlock() _, err := m.db.Exec(`INSERT INTO routes (source, destination, flags) VALUES (?, ?, ?) ON CONFLICT(source) DO UPDATE SET destination = excluded.destination, flags = excluded.flags, active = 1`, route.Src, route.Dst, route.Flags) return err } func (m *Manager) DeleteRoute(source string) error { - m.s.Lock() - defer m.s.Unlock() _, err := m.db.Exec(`UPDATE routes SET active = 0 WHERE source = ?`, source) return err } +func (m *Manager) GetAllRedirects() ([]target.Redirect, []bool, error) { + rSlice := make([]target.Redirect, 0) + aSlice := make([]bool, 0) + + query, err := m.db.Query(`SELECT source, destination, flags, code, active FROM redirects`) + if err != nil { + return nil, 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 + } + rSlice = append(rSlice, a) + aSlice = append(aSlice, active) + } + + return rSlice, aSlice, nil +} + func (m *Manager) InsertRedirect(redirect target.Redirect) error { - m.s.Lock() - defer m.s.Unlock() _, err := m.db.Exec(`INSERT INTO redirects (source, destination, flags, code) VALUES (?, ?, ?, ?) ON CONFLICT(source) DO UPDATE SET destination = excluded.destination, flags = excluded.flags, code = excluded.code, active = 1`, redirect.Src, redirect.Dst, redirect.Flags, redirect.Code) return err } func (m *Manager) DeleteRedirect(source string) error { - m.s.Lock() - defer m.s.Unlock() _, err := m.db.Exec(`UPDATE redirects SET active = 0 WHERE source = ?`, source) return err } diff --git a/servers/api/api.go b/servers/api/api.go index 7aa5e20..5f21a51 100644 --- a/servers/api/api.go +++ b/servers/api/api.go @@ -34,10 +34,34 @@ func NewApiServer(conf *conf.Conf, compileTarget utils.MultiCompilable) *http.Se 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)