go-urlsh/internal/web/index.go

49 lines
1.3 KiB
Go
Raw Permalink Normal View History

2023-04-27 12:34:24 +02:00
package web
import (
2023-04-28 18:24:09 +02:00
"context"
"database/sql"
"log"
2024-05-04 17:06:01 +02:00
"code.lila.network/adoralaura/go-urlsh/internal/constants"
"code.lila.network/adoralaura/go-urlsh/internal/db"
"code.lila.network/adoralaura/go-urlsh/models"
"github.com/gofiber/fiber/v2"
2023-04-27 12:34:24 +02:00
)
2024-05-04 17:06:01 +02:00
func HandleIndexGet(c *fiber.Ctx) error {
2023-04-28 18:24:09 +02:00
if c.Params("id", "") == "" {
2024-05-04 17:06:01 +02:00
if db.IsCookieValid(c.Cookies(constants.LoginCookieName, "")) {
2023-04-28 18:24:09 +02:00
c.Location("/admin/")
c.Status(fiber.StatusSeeOther)
return nil
} else {
err := c.SendString("This is the index page of go-urlsh.\n " +
2024-05-04 17:06:01 +02:00
"See https://code.lila.network/adoralaura/go-urlsh for more info.")
2023-04-28 18:24:09 +02:00
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
}
}