diff --git a/examples/docker-compose.yml b/examples/docker-compose.yml new file mode 100644 index 0000000..9ff3970 --- /dev/null +++ b/examples/docker-compose.yml @@ -0,0 +1,9 @@ +services: + + db: + image: postgres + restart: always + environment: + POSTGRES_PASSWORD: example + POSTGRES_USER: go-urlsh + POSTGRES_DB: go-urlsh \ No newline at end of file diff --git a/internal/app/fiber.go b/internal/app/fiber.go index af55561..8067ea1 100644 --- a/internal/app/fiber.go +++ b/internal/app/fiber.go @@ -58,6 +58,7 @@ func SetupFiber() error { fiberapp.Get("/admin/", web.HandleAdminLinkIndexGet) fiberapp.Get("/admin/links/new", web.HandleAdminLinkNewGet) + fiberapp.Get("/admin/links/edit/:id", web.HandleAdminLinkEditGet) fiberapp.Static("/admin/", "./web") diff --git a/internal/web/link.go b/internal/web/link.go index 2333205..0a4178b 100644 --- a/internal/web/link.go +++ b/internal/web/link.go @@ -1,22 +1,62 @@ 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" "fmt" "github.com/gofiber/fiber/v2" + "log" ) func HandleAdminLinkNewGet(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()) +func HandleAdminLinkEditGet(c *fiber.Ctx) error { + if !db.IsCookieValid(c.Cookies(misc.CookieName, "")) { + c.Location("/admin/") + c.Status(fiber.StatusSeeOther) + return nil + } + + var link = new(models.Link) + var id = c.Params("id", "") + + if id == "" { + log.Println("[HandleAdminLinkEditGet] no id param given") + return fiber.NewError(fiber.StatusBadRequest, "400 Bad Request") + } + + err := models.DB.NewSelect().Model(link).Where("id = ?", id).Scan(context.Background()) + + if err != nil { + if err == sql.ErrNoRows { + log.Printf("[HandleAdminLinkEditGet] Shortlink %v not found\n", id) + return fiber.NewError(fiber.StatusNotFound, "404 Not Found") + } else { + log.Printf("[HandleAdminLinkEditGet] Error querying Shortlink %v from database: %v\n", id, err) + return fiber.NewError(fiber.StatusInternalServerError, "500 Internal Server Error") + } + } + + return c.Render("edit_link", link) +} + +func HandleAdminLinkIndexGet(c *fiber.Ctx) error { + if !db.IsCookieValid(c.Cookies(misc.CookieName, "")) { + c.Location("/admin/login") + c.Status(fiber.StatusSeeOther) + return nil + } else { + 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) } - return c.Render("links", links) } diff --git a/internal/web/login.go b/internal/web/login.go index cddd255..4b7d70e 100644 --- a/internal/web/login.go +++ b/internal/web/login.go @@ -1,6 +1,7 @@ package web import ( + "codeberg.org/lauralani/go-urlsh/internal/db" "codeberg.org/lauralani/go-urlsh/internal/misc" "codeberg.org/lauralani/go-urlsh/models" "context" @@ -31,8 +32,14 @@ func HandleAdminLoginPost(c *fiber.Ctx) error { if passwordsum == user.PasswordHash { // Passwords match + var expires time.Time + + if login.Remember == "on" { + expires = time.Now().Add(30 * 24 * time.Hour) + } else { + expires = time.Now().Add(24 * time.Hour) + } - expires := time.Now().Add(30 * 24 * time.Hour) key := uuid.New().String() dblogin := new(models.Login) @@ -68,5 +75,11 @@ func HandleAdminLoginPost(c *fiber.Ctx) error { } func HandleAdminLoginGet(c *fiber.Ctx) error { - return c.Render("login", nil) + if db.IsCookieValid(c.Cookies(misc.CookieName, "")) { + c.Location("/admin/") + c.Status(fiber.StatusSeeOther) + return nil + } else { + return c.Render("login", nil) + } } diff --git a/models/login.go b/models/login.go index 932b657..76c3cc3 100644 --- a/models/login.go +++ b/models/login.go @@ -15,4 +15,5 @@ type Login struct { type LoginRequest struct { Username string `json:"username"` Password string `json:"password"` + Remember string `json:"remember"` } diff --git a/views/add_link.tmpl b/views/add_link.tmpl index a509e3a..62cc100 100644 --- a/views/add_link.tmpl +++ b/views/add_link.tmpl @@ -1,53 +1,74 @@ +