Remove locks and add get requests for routes

This commit is contained in:
Melon 2023-07-12 18:38:26 +01:00
parent 949dcd298a
commit ce8c421187
Signed by: melon
GPG Key ID: 6C9D970C50D26A25
2 changed files with 68 additions and 8 deletions

View File

@ -139,30 +139,66 @@ func (m *Manager) internalCompile(router *Router) error {
return rows.Err() 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 { 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) _, 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 return err
} }
func (m *Manager) DeleteRoute(source string) error { 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) _, err := m.db.Exec(`UPDATE routes SET active = 0 WHERE source = ?`, source)
return err 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 { 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) _, 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 return err
} }
func (m *Manager) DeleteRedirect(source string) error { 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) _, err := m.db.Exec(`UPDATE redirects SET active = 0 WHERE source = ?`, source)
return err return err
} }

View File

@ -34,10 +34,34 @@ func NewApiServer(conf *conf.Conf, compileTarget utils.MultiCompilable) *http.Se
targetApis := SetupTargetApis(conf.Signer, conf.Router) targetApis := SetupTargetApis(conf.Signer, conf.Router)
// Endpoint for routes // 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.POST("/route", targetApis.CreateRoute)
r.DELETE("/route", targetApis.DeleteRoute) r.DELETE("/route", targetApis.DeleteRoute)
// Endpoint for redirects // 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.POST("/redirect", targetApis.CreateRedirect)
r.DELETE("/redirect", targetApis.DeleteRedirect) r.DELETE("/redirect", targetApis.DeleteRedirect)