🔨 Refactors filename generator function and logs listing
This commit is contained in:
parent
be9d929309
commit
9ec12d7325
@ -7,6 +7,7 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"syscall"
|
||||
"time"
|
||||
@ -20,6 +21,9 @@ import (
|
||||
)
|
||||
|
||||
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 {
|
||||
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 {
|
||||
return fmt.Sprintf("%s/%s_%d.txt", dataDir, time.Now().Format("2006-01-02"), id)
|
||||
func filename(dataDir string, id string, date string) string {
|
||||
return fmt.Sprintf("%s/%s_%d.txt", dataDir, date, id)
|
||||
}
|
||||
|
||||
func main() {
|
||||
@ -161,7 +165,9 @@ func main() {
|
||||
|
||||
filenames := []string{}
|
||||
for _, file := range files {
|
||||
filenames = append(filenames, file.Name())
|
||||
if LogRegex.MatchString(file.Name()) {
|
||||
filenames = append(filenames, file.Name())
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
date := c.Param("date")
|
||||
if date == "" {
|
||||
date = time.Now().Format("2006-01-02")
|
||||
date = time.Now().Format(DateFormat)
|
||||
}
|
||||
|
||||
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 {
|
||||
return c.JSON(http.StatusNotFound, echo.Map{"message": "file not found"})
|
||||
}
|
||||
@ -281,7 +287,7 @@ func keys(c echo.Context) error {
|
||||
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 {
|
||||
log.Error(err)
|
||||
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