big bug fixes
This commit is contained in:
63
server.go
63
server.go
@@ -5,10 +5,11 @@ package main
|
||||
|
||||
import (
|
||||
"filething/ui"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/gofiber/fiber/v3/middleware/static"
|
||||
@@ -22,59 +23,57 @@ type embeddedFS struct {
|
||||
func (fs *embeddedFS) Open(name string) (fs.File, error) {
|
||||
// Prepend the prefix to the requested file name
|
||||
publicPath := filepath.Join(fs.prefix, name)
|
||||
fmt.Println("Reading file:", publicPath)
|
||||
file, err := fs.baseFS.Open(publicPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("file not found: %s", publicPath)
|
||||
return nil, fiber.ErrNotFound
|
||||
}
|
||||
|
||||
fmt.Println("File found:", publicPath, file)
|
||||
|
||||
return file, err
|
||||
}
|
||||
|
||||
var publicFS = &embeddedFS{
|
||||
baseFS: ui.DistDir,
|
||||
prefix: "public/",
|
||||
prefix: ".output/public/",
|
||||
}
|
||||
|
||||
func init() {
|
||||
initUi = func(app *fiber.App) {
|
||||
app.Get("/*", static.New("", static.Config{
|
||||
FS: publicFS,
|
||||
app.Use("/", static.New("", static.Config{
|
||||
FS: publicFS,
|
||||
CacheDuration: 10 * time.Hour,
|
||||
}))
|
||||
|
||||
// 404 handler
|
||||
app.Use(func(c fiber.Ctx) error {
|
||||
err := c.Next()
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if fiber.ErrNotFound == err {
|
||||
path := c.Path()
|
||||
if !strings.HasPrefix(path, "/api") {
|
||||
file, err := publicFS.Open("404.html")
|
||||
if err != nil {
|
||||
c.App().Server().Logger.Printf("Error opening 404.html: %s", err)
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
fileInfo, err := file.Stat()
|
||||
if err != nil {
|
||||
c.App().Server().Logger.Printf("An error occurred while getting the file info: %s", err)
|
||||
return err
|
||||
}
|
||||
|
||||
fileBuf := make([]byte, fileInfo.Size())
|
||||
_, err = file.Read(fileBuf)
|
||||
if err != nil {
|
||||
c.App().Server().Logger.Printf("An error occurred while reading the file: %s", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusNotFound).SendString(string(fileBuf))
|
||||
path := c.Path()
|
||||
if !strings.HasPrefix(path, "/api") {
|
||||
file, err := publicFS.Open("404.html")
|
||||
if err != nil {
|
||||
c.App().Server().Logger.Printf("Error opening 404.html: %s", err)
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
fileInfo, err := file.Stat()
|
||||
if err != nil {
|
||||
c.App().Server().Logger.Printf("An error occurred while getting the file info: %s", err)
|
||||
return err
|
||||
}
|
||||
|
||||
fileBuf := make([]byte, fileInfo.Size())
|
||||
_, err = file.Read(fileBuf)
|
||||
if err != nil {
|
||||
c.App().Server().Logger.Printf("An error occurred while reading the file: %s", err)
|
||||
return err
|
||||
}
|
||||
|
||||
c.Context().SetContentType("text/html")
|
||||
return c.Status(http.StatusOK).SendString(string(fileBuf))
|
||||
}
|
||||
return err
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user