diff --git a/internal/app/fiber.go b/internal/app/fiber.go index 7d76015..b13947c 100644 --- a/internal/app/fiber.go +++ b/internal/app/fiber.go @@ -38,11 +38,12 @@ func SetupFiber() error { } fiberapp := fiber.New(fiber.Config{ - AppName: "go-urlsh", - ProxyHeader: proxyheader, - Prefork: prod, - ErrorHandler: HandleError, - Views: template_engine, + AppName: "go-urlsh", + ProxyHeader: proxyheader, + Prefork: prod, + ErrorHandler: HandleError, + Views: template_engine, + CompressedFileSuffix: ".gz", }) fiberapp.Use(logger.New(logger.Config{Format: "[${ip}]:${port} ${status} ${method} ${path}\n"})) @@ -50,14 +51,19 @@ func SetupFiber() error { fiberapp.Use(compress.New()) fiberapp.Use(recover.New()) - fiberapp.Get("/admin/", web.HandleAdminRootGet) + fiberapp.Get("/admin/", web.HandleAdminLinkIndexGet) + fiberapp.Get("/admin/login", web.HandleAdminLoginGet) fiberapp.Post("/admin/login", web.HandleAdminLoginPost) - fiberapp.Get("/admin/link/add", web.HandleAdminLinkAddGet) + fiberapp.Get("/admin/", web.HandleAdminLinkIndexGet) + fiberapp.Get("/admin/links/add", web.HandleAdminLinkAddGet) fiberapp.Static("/admin/", "./web") + fiberapp.Get("/", web.HandleRootGet) + fiberapp.Get("/:id", web.HandleRootGet) + v1 := fiberapp.Group("/api/v1") v1.Use(cors.New(cors.Config{AllowOrigins: "*"})) diff --git a/internal/web/link.go b/internal/web/link.go index 4b9aad1..6c102ee 100644 --- a/internal/web/link.go +++ b/internal/web/link.go @@ -1,7 +1,22 @@ package web -import "github.com/gofiber/fiber/v2" +import ( + "codeberg.org/lauralani/go-urlsh/models" + "context" + "fmt" + "github.com/gofiber/fiber/v2" +) func HandleAdminLinkAddGet(c *fiber.Ctx) error { return c.Render("add_link", nil) } + +func HandleAdminLinkIndexGet(c *fiber.Ctx) error { + var links []models.Link + + err := models.DB.NewSelect().Model(&links).Scan(context.Background()) + if err != nil { + return fmt.Errorf("error querying links: %v", err.Error()) + } + return c.Render("links", links) +} diff --git a/internal/web/root.go b/internal/web/root.go index 3afa4e2..9738e39 100644 --- a/internal/web/root.go +++ b/internal/web/root.go @@ -3,26 +3,44 @@ package web import ( "codeberg.org/lauralani/go-urlsh/internal/db" "codeberg.org/lauralani/go-urlsh/internal/misc" + "codeberg.org/lauralani/go-urlsh/models" + "context" + "database/sql" "github.com/gofiber/fiber/v2" + "log" ) -func HandleAdminRootGet(c *fiber.Ctx) error { - cookie := c.Cookies(misc.CookieName) - if cookie == "" { - c.Location("/admin/login") - c.Status(fiber.StatusSeeOther) - return nil - } - - if db.IsCookieValid(cookie) { - // Render index template - return c.Render("index", fiber.Map{ - "Title": "It works", - "Plat": "almost", - }) +func HandleRootGet(c *fiber.Ctx) error { + if c.Params("id", "") == "" { + if db.IsCookieValid(c.Cookies(misc.CookieName, "")) { + c.Location("/admin/") + c.Status(fiber.StatusSeeOther) + return nil + } else { + err := c.SendString("This is the index page of go-urlsh.\n " + + "See https://codeberg.org/lauralani/go-urlsh for more info.") + if err != nil { + log.Println(err) + return err + } + c.Status(fiber.StatusOK) + return nil + } } else { - c.ClearCookie(misc.CookieName) - c.Location("/admin/login") + link := new(models.Link) + + err := models.DB.NewSelect().Model(link).Where("id = ?", c.Params("id")).Scan(context.Background()) + if err != nil { + if err == sql.ErrNoRows { + log.Printf("Shortlink %v not found\n", c.Params("id")) + return fiber.NewError(fiber.StatusNotFound, "404 Not Found") + } else { + log.Printf("Error querying Link %v from database: %v\n", c.Params("id"), err) + return fiber.NewError(fiber.StatusInternalServerError, "500 Internal Server Error") + } + } + + c.Location(link.URL) c.Status(fiber.StatusSeeOther) return nil } diff --git a/views/add_link.tmpl b/views/add_link.tmpl index 2b1e757..a509e3a 100644 --- a/views/add_link.tmpl +++ b/views/add_link.tmpl @@ -5,7 +5,7 @@