Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor changes #4

Merged
merged 2 commits into from
Mar 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions fileserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ func (fs *FileServer) GetFiles(path string, skip, limit int, sortBy, order strin
}, nil
}

func (fo *FileServer) GetVideos(path string, skip, limit int, recursive bool, sortBy, order string) (*FilePiResonse, error) {
absPath, err := fo.getAbsolutePath(path)
func (fs *FileServer) GetVideos(path string, skip, limit int, recursive bool, sortBy, order string) (*FilePiResonse, error) {
absPath, err := fs.getAbsolutePath(path)
if err != nil {
return nil, err
}
Expand All @@ -153,7 +153,7 @@ func (fo *FileServer) GetVideos(path string, skip, limit int, recursive bool, so
CreatedTime: info.ModTime().UnixMilli(),
ModifiedTime: info.ModTime().UnixMilli(),
FileType: mimeType,
Owner: fo.getFileOwner(info),
Owner: fs.getFileOwner(info),
FullName: relPath,
})
}
Expand All @@ -163,7 +163,7 @@ func (fo *FileServer) GetVideos(path string, skip, limit int, recursive bool, so
if err != nil {
return nil, fmt.Errorf("error reading directory: %w", err)
}
if sortBy != "" && !contains(fo.ValidSortFields, sortBy) {
if sortBy != "" && !contains(fs.ValidSortFields, sortBy) {
return nil, fmt.Errorf("invalid sort field: %s", sortBy)
}
if sortBy != "" {
Expand Down Expand Up @@ -213,8 +213,8 @@ func (fo *FileServer) GetVideos(path string, skip, limit int, recursive bool, so
}, nil
}

func (fo *FileServer) Search(query, path string, skip, limit int, sortBy, order string) (*FilePiResonse, error) {
absPath, err := fo.getAbsolutePath(path)
func (fs *FileServer) Search(query, path string, skip, limit int, sortBy, order string) (*FilePiResonse, error) {
absPath, err := fs.getAbsolutePath(path)
if err != nil {
return nil, err
}
Expand All @@ -231,16 +231,16 @@ func (fo *FileServer) Search(query, path string, skip, limit int, sortBy, order
CreatedTime: info.ModTime().UnixMilli(),
ModifiedTime: info.ModTime().UnixMilli(),
FileType: mime.TypeByExtension(filepath.Ext(info.Name())),
Owner: fo.getFileOwner(info),
FullName: strings.TrimPrefix(path, fo.RootDir),
Owner: fs.getFileOwner(info),
FullName: strings.TrimPrefix(path, fs.RootDir),
})
}
return nil
})
if err != nil {
return nil, fmt.Errorf("error reading directory: %w", err)
}
if sortBy != "" && !contains(fo.ValidSortFields, sortBy) {
if sortBy != "" && !contains(fs.ValidSortFields, sortBy) {
return nil, fmt.Errorf("invalid sort field: %s", sortBy)
}
if sortBy != "" {
Expand Down Expand Up @@ -290,8 +290,8 @@ func (fo *FileServer) Search(query, path string, skip, limit int, sortBy, order
}, nil
}

func (fo *FileServer) ServeFile(path string) (string, error) {
absPath, err := fo.getAbsolutePath(path)
func (fs *FileServer) ServeFile(path string) (string, error) {
absPath, err := fs.getAbsolutePath(path)
if err != nil {
return "", err
}
Expand All @@ -301,8 +301,8 @@ func (fo *FileServer) ServeFile(path string) (string, error) {
return absPath, nil
}

func (fo *FileServer) StreamFile(path string) (string, error) {
absPath, err := fo.getAbsolutePath(path)
func (fs *FileServer) StreamFile(path string) (string, error) {
absPath, err := fs.getAbsolutePath(path)
if err != nil {
return "", err
}
Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ module FilePi

go 1.24.1

require (
github.com/gofiber/fiber/v2 v2.52.6
github.com/sirupsen/logrus v1.9.3
)

require (
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/gofiber/fiber/v2 v2.52.6 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.51.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gofiber/fiber/v2 v2.52.6 h1:Rfp+ILPiYSvvVuIPvxrBns+HJp8qGLDnLJawAu27XVI=
github.com/gofiber/fiber/v2 v2.52.6/go.mod h1:YEcBbO/FB+5M1IZNBP9FO3J9281zgPAreiI1oqg8nDw=
Expand All @@ -15,12 +16,14 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
Expand All @@ -34,4 +37,5 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
42 changes: 34 additions & 8 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,50 @@ package main
import (
"strconv"
"strings"
"time"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/requestid"
log "github.com/sirupsen/logrus"
)

func RequestLogger() fiber.Handler {
return func(c *fiber.Ctx) error {
// Record the start time
start := time.Now()

// Process the request
err := c.Next()

// Calculate the latency
latency := time.Since(start)

// Log the request details using Logrus
log.WithFields(log.Fields{
"status": c.Response().StatusCode(),
"method": c.Method(),
"path": c.Path(),
"latency": latency.String(),
"ip": c.IP(),
"query_params": c.Request().URI().QueryArgs().String(),
}).Info("HTTP request")

return err
}
}

func setupRoutes(fs *FileServer) {

app := fiber.New()
api := app.Group(API_PREFIX)

app.Use(requestid.New())
app.Use(logger.New(logger.Config{
Format: "${time} ${pid} ${locals:requestid} ${status} - ${method} ${path} ${queryParams}\n",
TimeFormat: "2006/01/02 15:04:05.000000",
TimeZone: "Local",
}))
//app.Use(requestid.New())
// app.Use(logger.New(logger.Config{
// Format: "${time} ${pid} ${locals:requestid} ${status} - ${method} ${path} ${queryParams}\n",
// TimeFormat: "2006/01/02 15:04:05.000000",
// TimeZone: "Local",
// }))

app.Use(RequestLogger())

api.Get("/files", func(c *fiber.Ctx) error {
path := c.Query("path", "")
Expand Down
Loading