🔨 Refactors filename generator function and logs listing
This commit is contained in:
parent
be9d929309
commit
9ec12d7325
@ -7,6 +7,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
@ -20,6 +21,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const TimeFormat = "2006-01-02 15:04:05"
|
const TimeFormat = "2006-01-02 15:04:05"
|
||||||
|
const DateFormat = "2006-01-02"
|
||||||
|
|
||||||
|
var LogRegex = regexp.MustCompile(`(?m)^.*/\d{4}-\d{2}-\d{2}_\d+\.txt$`)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
ID uuid.UUID `json:"id"`
|
ID uuid.UUID `json:"id"`
|
||||||
@ -68,8 +72,8 @@ func idparam(next func(echo.Context, uuid.UUID) error) echo.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func filename(dataDir string, id string) string {
|
func filename(dataDir string, id string, date string) string {
|
||||||
return fmt.Sprintf("%s/%s_%d.txt", dataDir, time.Now().Format("2006-01-02"), id)
|
return fmt.Sprintf("%s/%s_%d.txt", dataDir, date, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -161,8 +165,10 @@ func main() {
|
|||||||
|
|
||||||
filenames := []string{}
|
filenames := []string{}
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
|
if LogRegex.MatchString(file.Name()) {
|
||||||
filenames = append(filenames, file.Name())
|
filenames = append(filenames, file.Name())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return c.JSONPretty(http.StatusOK, filenames, indent)
|
return c.JSONPretty(http.StatusOK, filenames, indent)
|
||||||
}))
|
}))
|
||||||
@ -180,7 +186,7 @@ func main() {
|
|||||||
app.GET("/admin/:id/logs", authed(idparam(func(c echo.Context, id uuid.UUID) error {
|
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("2006-01-02")
|
date = time.Now().Format(DateFormat)
|
||||||
}
|
}
|
||||||
|
|
||||||
skipRaw := c.Param("skip")
|
skipRaw := c.Param("skip")
|
||||||
@ -201,7 +207,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
f, err := os.OpenFile(filename(dataDir, id.String()), syscall.O_CREAT|syscall.O_APPEND|syscall.O_WRONLY, 0644)
|
f, err := os.OpenFile(filename(dataDir, id.String(), date), syscall.O_CREAT|syscall.O_APPEND|syscall.O_WRONLY, 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"})
|
||||||
}
|
}
|
||||||
@ -281,7 +287,7 @@ func keys(c echo.Context) error {
|
|||||||
log.Error(err)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
f, err := os.OpenFile(filename(dataDir, client.ID.String()), syscall.O_CREAT|syscall.O_APPEND|syscall.O_WRONLY, 0644)
|
f, err := os.OpenFile(filename(dataDir, client.ID.String(), now.Format(DateFormat)), syscall.O_CREAT|syscall.O_APPEND|syscall.O_WRONLY, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
fmt.Printf("%s client %s crashed (couldn't open file)\n", time.Now().Format(TimeFormat), client.ID)
|
fmt.Printf("%s client %s crashed (couldn't open file)\n", time.Now().Format(TimeFormat), client.ID)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user