From 8e0bfc327f9f016aada9ffa50f03e6c83a1fa926 Mon Sep 17 00:00:00 2001 From: VnPower Date: Tue, 20 Jun 2023 21:21:27 +0700 Subject: [PATCH] Testing out the settings page --- .gitignore | 1 + configs/session.go | 10 ++++ main.go | 12 +---- template/layout.jet.html | 2 +- template/settings.jet.html | 104 ++++++++++++++----------------------- views/routes.go | 35 +++++++++++++ 6 files changed, 86 insertions(+), 78 deletions(-) create mode 100644 configs/session.go diff --git a/.gitignore b/.gitignore index 03e9b50..3d20e16 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ config.yml tmp/ .air.toml +/.dir-locals.el diff --git a/configs/session.go b/configs/session.go new file mode 100644 index 0000000..ffbda30 --- /dev/null +++ b/configs/session.go @@ -0,0 +1,10 @@ +package configs + +import "github.com/gofiber/fiber/v2/middleware/session" + +var Store *session.Store + +func SetupStorage() { + Store = session.New() + Store.RegisterType("") +} diff --git a/main.go b/main.go index 149f1b7..86503ca 100644 --- a/main.go +++ b/main.go @@ -6,13 +6,10 @@ import ( "pixivfe/handler" "pixivfe/views" "strings" - "time" "github.com/goccy/go-json" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/cache" - "github.com/gofiber/fiber/v2/middleware/csrf" - // "github.com/gofiber/fiber/v2/middleware/helmet" "github.com/gofiber/fiber/v2/middleware/logger" "github.com/gofiber/fiber/v2/utils" "github.com/gofiber/template/jet/v2" @@ -42,14 +39,6 @@ func setupRouter() *fiber.App { }, }, )) - // server.Use(helmet.New()) - server.Use(csrf.New(csrf.Config{ - KeyLookup: "header:X-Csrf-Token", // string in the form of ':' that is used to extract token from the request - CookieName: "my_csrf_", // name of the session cookie - CookieSameSite: "Strict", // indicates if CSRF cookie is requested by SameSite - Expiration: 3 * time.Hour, // expiration is the duration before CSRF token will expire - KeyGenerator: utils.UUID, // creates a new CSRF token - })) // Static files server.Static("/favicon.ico", "./template/favicon.ico") @@ -67,6 +56,7 @@ func setupRouter() *fiber.App { func main() { err := configs.ParseConfig() + configs.SetupStorage() if err != nil { panic(err) diff --git a/template/layout.jet.html b/template/layout.jet.html index 8ee6945..a0e3da7 100644 --- a/template/layout.jet.html +++ b/template/layout.jet.html @@ -52,7 +52,7 @@ Search - + Settings diff --git a/template/settings.jet.html b/template/settings.jet.html index 528be6f..70d3b08 100644 --- a/template/settings.jet.html +++ b/template/settings.jet.html @@ -1,68 +1,40 @@
-

Settings

-
-
- Proxy server {{ Settings.Proxy }} - - -
- - -
- - -
- - -
- - -
- - -
-
- -
- -
- -
-
+

Settings

+
+
+ Custom token + + +
+
+
+
+ Proxy server + + +
+ + +
+ + +
+ + +
+ + +
+ + +
+
+ +
+ +
+ +
+
diff --git a/views/routes.go b/views/routes.go index 310312f..93ff830 100644 --- a/views/routes.go +++ b/views/routes.go @@ -52,6 +52,16 @@ func index_page(c *fiber.Ctx) error { // "Spotlights": spotlight, // "Newest": newest, // }) + sess, err := configs.Store.Get(c) + if err != nil { + panic(err) + } + token := sess.Get("token") + + if token != nil { + println(token.(string)) + } + return c.Render("temp", fiber.Map{"Title": "Landing"}) } @@ -165,6 +175,28 @@ func discovery_page(c *fiber.Ctx) error { return c.Render("discovery", fiber.Map{"Title": "Discovery", "Artworks": artworks}) } +func settings_page(c *fiber.Ctx) error { + return c.Render("settings", fiber.Map{}) +} + +func settings_handler(c *fiber.Ctx) error { + sess, err := configs.Store.Get(c) + if err != nil { + panic(err) + } + + token := c.FormValue("token") + if token != "" { + sess.Set("token", token) + + if err := sess.Save(); err != nil { + panic(err) + } + return c.Redirect("/settings/", http.StatusAccepted) + } + return c.Redirect("/settings/", http.StatusNoContent) +} + // func not_found_page(c *fiber.Ctx) { // return c.Render(http.StatusNotFound, "error.html", fiber.Map{ // "Title": "Not found", @@ -204,6 +236,9 @@ func SetupRoutes(r *fiber.App) { r.Get("discovery", discovery_page) r.Post("tags", search) + r.Get("settings", settings_page) + r.Post("setting", settings_handler) + // 404 page // r.NoRoute(not_found_page) }