go-urlsh/internal/web/root.go

48 lines
1.2 KiB
Go
Raw Permalink Normal View History

2023-04-27 12:34:24 +02:00
package web
import (
"codeberg.org/lauralani/go-urlsh/internal/db"
"codeberg.org/lauralani/go-urlsh/internal/misc"
2023-04-28 18:24:09 +02:00
"codeberg.org/lauralani/go-urlsh/models"
"context"
"database/sql"
2023-04-27 12:34:24 +02:00
"github.com/gofiber/fiber/v2"
2023-04-28 18:24:09 +02:00
"log"
2023-04-27 12:34:24 +02:00
)
2023-04-28 18:24:09 +02:00
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
}
2023-04-27 12:34:24 +02:00
} else {
2023-04-28 18:24:09 +02:00
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)
2023-04-27 12:34:24 +02:00
c.Status(fiber.StatusSeeOther)
return nil
}
}