Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
tkdeng committed Dec 26, 2024
1 parent c267faf commit daa2f57
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package server

import (
"github.com/tkdeng/simplewebserver/render"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/tkdeng/simplewebserver/render"

"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/static"
"github.com/tkdeng/goutil"
Expand Down Expand Up @@ -110,25 +112,32 @@ func (app *App) Listen() error {
if url == "/" || url == "" {
url = "index"
}
url = strings.Trim(url, "/")

if path, err := goutil.JoinPath(Config.Root, "pages.dist", url+".html"); err == nil {
if stat, err := os.Stat(path); err == nil && !stat.IsDir() {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
c.SendStatus(fiber.StatusOK)

if i, err := strconv.Atoi(url); err == nil && i >= 100 && i <= 599 {
c.SendStatus(i)
} else {
c.SendStatus(200)
}

return c.Render(url, fiber.Map{})
}
}

if path, err := goutil.JoinPath(Config.Root, "pages.dist", "404.html"); err == nil {
if stat, err := os.Stat(path); err == nil && !stat.IsDir() {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
c.SendStatus(fiber.StatusNotFound)
c.SendStatus(404)
return c.Render("404", fiber.Map{})
}
}

c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
c.SendStatus(fiber.StatusNotFound)
c.SendStatus(404)
return c.Send([]byte("<h1>Error 404</h1><h2>Page Not Found!</h2>"))
})

Expand Down

0 comments on commit daa2f57

Please sign in to comment.