add more templating
This commit is contained in:
parent
1aaa283396
commit
63dc9125c0
12 changed files with 1871 additions and 77 deletions
|
@ -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: "*"}))
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
|
||||
<title>Add new Shortlink - go-urlsh</title>
|
||||
<meta name='viewport' content='width=device-width, initial-scale=1'>
|
||||
<link rel="stylesheet" href="/admin/simple.min.css">
|
||||
<link rel="stylesheet" href="/admin/style.css">
|
||||
|
||||
</head>
|
||||
<body>
|
||||
|
@ -17,12 +17,16 @@
|
|||
<input type="text" name="linkname" id="linkname" placeholder="shortlink">
|
||||
|
||||
<label for="link">Link *</label>
|
||||
<input type="text" name="link" id="link" placeholder="https://" style="width: 100%" onchange="HandleChange()">
|
||||
<input type="text" name="link" id="link" placeholder="https://" style="width: 97%" onchange="HandleChange()">
|
||||
|
||||
<label for="description">Description</label>
|
||||
<input type="text" name="description" id="description" placeholder="" style="width: 100%">
|
||||
<input type="text" name="description" id="description" placeholder="" style="width: 97%">
|
||||
|
||||
<input type="submit" id="submit" value="Login" onclick="HandleSubmit()">
|
||||
<div>
|
||||
|
||||
<input type="submit" id="submit" value="Add" onclick="HandleSubmit()" style="margin-top: 1em;">
|
||||
|
||||
</div>
|
||||
</fieldset>
|
||||
|
||||
<dialog id="dialog-info">
|
||||
|
@ -43,7 +47,7 @@
|
|||
</p>
|
||||
</footer>
|
||||
|
||||
<script src="/admin/links.js"></script>
|
||||
<script src="/admin/link_add.js"></script>
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -1,10 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=Unicode">
|
||||
<title>{{.Title}}</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>{{.Title}}</h1>
|
||||
<p>{{.Plat}}</p>
|
||||
</body>
|
||||
</html>
|
55
views/links.tmpl
Normal file
55
views/links.tmpl
Normal file
|
@ -0,0 +1,55 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset='utf-8'>
|
||||
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
|
||||
<title>Shortlinks - go-urlsh</title>
|
||||
<meta name='viewport' content='width=device-width, initial-scale=1'>
|
||||
<link rel="stylesheet" href="/admin/style.css">
|
||||
<style>
|
||||
td { vertical-align: middle; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Shortlink</th>
|
||||
<th>URL</th>
|
||||
<th> </th>
|
||||
<th> </th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{range .}}
|
||||
<tr>
|
||||
<td>{{.ID}}</td>
|
||||
<td>{{.URL}}</td>
|
||||
<td style="text-align: right;">
|
||||
<button type="button" id="edit-button" onclick="HandleEdit('{{.ID}}')">
|
||||
Edit
|
||||
</button>
|
||||
</td>
|
||||
<td style="text-align: right;">
|
||||
<button type="button" id="delete-button" onclick="HandleDelete('{{.ID}}')">
|
||||
Delete
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
|
||||
<footer>
|
||||
<p>go-urlsh - <a href="/">Home</a> -
|
||||
<a href="https://codeberg.org/lauralani/go-urlsh" target="_blank">Source Code</a> -
|
||||
<a href="https://codeberg.org/lauralani/go-urlsh/src/branch/main/LICENSE" target="_blank">License</a>
|
||||
</p>
|
||||
</footer>
|
||||
|
||||
<script src="/admin/link_add.js"></script>
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -5,7 +5,7 @@
|
|||
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
|
||||
<title>Page Title</title>
|
||||
<meta name='viewport' content='width=device-width, initial-scale=1'>
|
||||
<link rel="stylesheet" href="/admin/simple.min/css">
|
||||
<link rel="stylesheet" href="/admin/style.css">
|
||||
</head>
|
||||
<body>
|
||||
<h3 id="form-elements">Form elements</h3>
|
||||
|
|
54
web/links.html
Normal file
54
web/links.html
Normal file
|
@ -0,0 +1,54 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset='utf-8'>
|
||||
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
|
||||
<title>Shortlinks - go-urlsh</title>
|
||||
<meta name='viewport' content='width=device-width, initial-scale=1'>
|
||||
<link rel="stylesheet" href="/admin/style.css">
|
||||
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Shortlink</th>
|
||||
<th>URL</th>
|
||||
<th> </th>
|
||||
<th> </th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{range .}}
|
||||
<tr>
|
||||
<td>{{.ID}}</td>
|
||||
<td>{{.URL}}</td>
|
||||
|
||||
<td>
|
||||
<button type="button" id="edit-button" onclick="HandleEdit('{{.ID}}')">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6"><path stroke-linecap="round" stroke-linejoin="round" d="M16.862 4.487l1.687-1.688a1.875 1.875 0 112.652 2.652L10.582 16.07a4.5 4.5 0 01-1.897 1.13L6 18l.8-2.685a4.5 4.5 0 011.13-1.897l8.932-8.931zm0 0L19.5 7.125M18 14v4.75A2.25 2.25 0 0115.75 21H5.25A2.25 2.25 0 013 18.75V8.25A2.25 2.25 0 015.25 6H10" /></svg>
|
||||
</button>
|
||||
</td>
|
||||
<td>
|
||||
<button type="button" id="delete-button" onclick="HandleDelete('{{.ID}}')">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6"><path stroke-linecap="round" stroke-linejoin="round" d="M14.74 9l-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 01-2.244 2.077H8.084a2.25 2.25 0 01-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 00-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 013.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 00-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 00-7.5 0" /></svg>
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
|
||||
<footer>
|
||||
<p>go-urlsh - <a href="/">Home</a> -
|
||||
<a href="https://codeberg.org/lauralani/go-urlsh" target="_blank">Source Code</a> -
|
||||
<a href="https://codeberg.org/lauralani/go-urlsh/src/branch/main/LICENSE" target="_blank">License</a>
|
||||
</p>
|
||||
</footer>
|
||||
|
||||
<script src="/admin/links.js"></script>
|
||||
|
||||
</body>
|
||||
</html>
|
36
web/links.js
36
web/links.js
|
@ -1,36 +0,0 @@
|
|||
async function HandleSubmit() {
|
||||
document.getElementById("submit").active = false
|
||||
let slug = document.getElementById("linkname").value
|
||||
let url = document.getElementById("link").value
|
||||
let description = document.getElementById("description").value
|
||||
let body = {
|
||||
"id" : slug,
|
||||
"url" : url,
|
||||
"description" : description
|
||||
}
|
||||
|
||||
let response = await fetch("/api/v1/links", {
|
||||
credentials: "include",
|
||||
body: JSON.stringify(body),
|
||||
mode: "same-origin",
|
||||
method: "POST"
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
document.getElementById("dialog-heading").textContent = "Error"
|
||||
document.getElementById("dialog-text").textContent = "The following error occured during the request: " + response.statusText
|
||||
document.getElementById('dialog-info').showModal()
|
||||
|
||||
document.getElementById("submit").active = true
|
||||
}
|
||||
}
|
||||
|
||||
async function HandleChange() {
|
||||
console.log("HandleChange")
|
||||
let buttonactive = true
|
||||
if (document.getElementById("link").value === "")
|
||||
{
|
||||
buttonactive = false
|
||||
}
|
||||
document.getElementById("submit").active = buttonactive
|
||||
}
|
1
web/simple.min.css
vendored
1
web/simple.min.css
vendored
File diff suppressed because one or more lines are too long
1689
web/style.css
Normal file
1689
web/style.css
Normal file
File diff suppressed because it is too large
Load diff
BIN
web/style.css.gz
Normal file
BIN
web/style.css.gz
Normal file
Binary file not shown.
Loading…
Reference in a new issue