mirror of
https://github.com/1f349/violet.git
synced 2024-11-24 04:11:32 +00:00
Remove locks and add get requests for routes
This commit is contained in:
parent
949dcd298a
commit
ce8c421187
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user