🐛 Fixes routes errors and specific log reading

This commit is contained in:
Daniel Svitan 2025-04-11 22:39:01 +02:00
parent 6754872cfe
commit f64f02cfd9

View File

@ -73,7 +73,7 @@ func idparam(next func(echo.Context, uuid.UUID) error) echo.HandlerFunc {
} }
func filename(dataDir string, id string, date string) string { func filename(dataDir string, id string, date string) string {
return fmt.Sprintf("%s/%s_%d.txt", dataDir, date, id) return fmt.Sprintf("%s/%s_%s.txt", dataDir, date, id)
} }
func main() { func main() {
@ -157,9 +157,20 @@ func main() {
return c.JSONPretty(http.StatusOK, clients, indent) return c.JSONPretty(http.StatusOK, clients, indent)
})) }))
app.GET("/admin/clients/:id", authed(idparam(func(c echo.Context, id uuid.UUID) error {
for _, client := range clients {
if client.ID == id {
return c.JSONPretty(http.StatusOK, client, indent)
}
}
return c.NoContent(http.StatusNotFound)
})))
app.GET("/admin/logs", authed(func(c echo.Context) error { app.GET("/admin/logs", authed(func(c echo.Context) error {
files, err := os.ReadDir(dataDir) files, err := os.ReadDir(dataDir)
if err != nil { if err != nil {
log.Error(err)
return c.NoContent(http.StatusInternalServerError) return c.NoContent(http.StatusInternalServerError)
} }
@ -173,17 +184,7 @@ func main() {
return c.JSONPretty(http.StatusOK, filenames, indent) return c.JSONPretty(http.StatusOK, filenames, indent)
})) }))
app.GET("/admin/:id", authed(idparam(func(c echo.Context, id uuid.UUID) error { app.GET("/admin/logs/:id", authed(idparam(func(c echo.Context, id uuid.UUID) error {
for _, client := range clients {
if client.ID == id {
return c.JSONPretty(http.StatusOK, client, indent)
}
}
return c.NoContent(http.StatusNotFound)
})))
app.GET("/admin/:id/logs", authed(idparam(func(c echo.Context, id uuid.UUID) error {
date := c.Param("date") date := c.Param("date")
if date == "" { if date == "" {
date = time.Now().Format(DateFormat) date = time.Now().Format(DateFormat)
@ -207,7 +208,7 @@ func main() {
} }
} }
f, err := os.OpenFile(filename(dataDir, id.String(), date), syscall.O_CREAT|syscall.O_APPEND|syscall.O_WRONLY, 0644) f, err := os.OpenFile(filename(dataDir, id.String(), date), syscall.O_RDONLY, 0644)
if err != nil { if err != nil {
return c.JSON(http.StatusNotFound, echo.Map{"message": "file not found"}) return c.JSON(http.StatusNotFound, echo.Map{"message": "file not found"})
} }
@ -215,20 +216,22 @@ func main() {
if skip != 0 { if skip != 0 {
_, err = f.Seek(int64(skip), io.SeekStart) _, err = f.Seek(int64(skip), io.SeekStart)
if err != nil { if err != nil {
log.Error(err)
return c.NoContent(http.StatusInternalServerError) return c.NoContent(http.StatusInternalServerError)
} }
} }
bytes := make([]byte, take) bytes := make([]byte, take)
_, err = f.Read(bytes) n, err := f.Read(bytes)
if err != nil { if err != nil {
log.Error(err)
return c.NoContent(http.StatusInternalServerError) return c.NoContent(http.StatusInternalServerError)
} }
return c.JSONPretty(http.StatusOK, bytes, indent) return c.JSONPretty(http.StatusOK, string(bytes[:n]), indent)
}))) })))
app.POST("/admin/:id/name", authed(idparam(func(c echo.Context, id uuid.UUID) error { app.POST("/admin/name/:id", authed(idparam(func(c echo.Context, id uuid.UUID) error {
name := c.Param("name") name := c.Param("name")
if name == "" { if name == "" {
return c.JSON(http.StatusBadRequest, "missing field 'name'") return c.JSON(http.StatusBadRequest, "missing field 'name'")
@ -245,7 +248,7 @@ func main() {
return c.JSON(http.StatusNotFound, echo.Map{"message": "not found"}) return c.JSON(http.StatusNotFound, echo.Map{"message": "not found"})
}))) })))
app.POST("/admin/:id/exit", authed(idparam(func(c echo.Context, id uuid.UUID) error { app.POST("/admin/exit/:id", authed(idparam(func(c echo.Context, id uuid.UUID) error {
for _, client := range clients { for _, client := range clients {
if client.ID == id { if client.ID == id {
client.ExitWanted = true client.ExitWanted = true