From 16301a3428d3225e7f09cc2f3c65fa956120c4a1 Mon Sep 17 00:00:00 2001 From: Redume Date: Sun, 23 Oct 2022 15:26:51 +0300 Subject: [PATCH] rewritten from net/http to fiber, changed port from 8080, 4662 to one 3000 --- api/controllers/label.go | 7 ++-- api/controllers/wallpaper.go | 19 +++++----- api/utils/utils.go | 9 ++--- functions/tray.go | 2 +- main.go | 67 ++++++++++++++++++++---------------- web/page/about.go | 15 ++++++++ web/page/gallery.go | 15 ++++++++ web/page/settings.go | 15 ++++++++ 8 files changed, 102 insertions(+), 47 deletions(-) create mode 100644 web/page/about.go create mode 100644 web/page/gallery.go create mode 100644 web/page/settings.go diff --git a/api/controllers/label.go b/api/controllers/label.go index 90abaa7..4e1b18e 100644 --- a/api/controllers/label.go +++ b/api/controllers/label.go @@ -1,16 +1,16 @@ package controllers import ( - "net/http" "os" "os/user" "strings" "github.com/Redume/EveryNasa/api/utils" "github.com/Redume/EveryNasa/functions" + "github.com/gofiber/fiber/v2" ) -var CreateLabel = func(w http.ResponseWriter, r *http.Request) { +var CreateLabel = func(c *fiber.Ctx) error { u, err := user.Current() if err != nil { functions.Logger(err.Error()) @@ -28,5 +28,6 @@ var CreateLabel = func(w http.ResponseWriter, r *http.Request) { functions.Logger(err.Error()) } - utils.Respond(w, utils.Message(true, "The shortcut was successfully created")) + utils.Respond(c, utils.Message(true, "The shortcut was successfully created")) + return nil } diff --git a/api/controllers/wallpaper.go b/api/controllers/wallpaper.go index 1ba2fc8..4f4a73a 100644 --- a/api/controllers/wallpaper.go +++ b/api/controllers/wallpaper.go @@ -1,27 +1,28 @@ package controllers import ( - "net/http" - "github.com/Redume/EveryNasa/api/utils" "github.com/Redume/EveryNasa/functions" + "github.com/gofiber/fiber/v2" "github.com/reujab/wallpaper" ) -var WallpaperUpdate = func(w http.ResponseWriter, r *http.Request) { +var WallpaperUpdate = func(c *fiber.Ctx) error { var url string - url = r.FormValue("url") + url = c.FormValue("url") if url == "" { - utils.Respond(w, utils.Message(false, "URL is required")) - return + utils.Respond(c, utils.Message(false, "URL is required")) + return nil } err := wallpaper.SetFromURL(url) if err != nil { functions.Logger(err.Error()) - utils.Respond(w, utils.Message(false, "An error occurred while setting the wallpaper")) - return + utils.Respond(c, utils.Message(false, "An error occurred while setting the wallpaper")) + return nil } - utils.Respond(w, utils.Message(true, "Wallpaper successfully updated")) + utils.Respond(c, utils.Message(true, "Wallpaper successfully updated")) + + return nil } diff --git a/api/utils/utils.go b/api/utils/utils.go index eb9d1f7..aad7ac4 100644 --- a/api/utils/utils.go +++ b/api/utils/utils.go @@ -2,17 +2,18 @@ package utils import ( "encoding/json" + "github.com/Redume/EveryNasa/functions" - "net/http" + "github.com/gofiber/fiber/v2" ) func Message(status bool, message string) map[string]interface{} { return map[string]interface{}{"status": status, "message": message} } -func Respond(w http.ResponseWriter, data map[string]interface{}) { - w.Header().Add("Content-Type", "application/json") - err := json.NewEncoder(w).Encode(data) +func Respond(c *fiber.Ctx, data map[string]interface{}) { + c.Set("Content-Type", "application/json") + err := json.NewEncoder(c).Encode(data) if err != nil { functions.Logger(err.Error()) } diff --git a/functions/tray.go b/functions/tray.go index ebd0038..a0a27e6 100644 --- a/functions/tray.go +++ b/functions/tray.go @@ -19,7 +19,7 @@ func Tray() { for { select { case <-ui.ClickedCh: - err := open.Run("http://localhost:4662") + err := open.Run("http://localhost:3000") if err != nil { Logger(err.Error()) } diff --git a/main.go b/main.go index 0b2924a..0fdbf2d 100644 --- a/main.go +++ b/main.go @@ -1,54 +1,61 @@ package main import ( - "net/http" - "github.com/Redume/EveryNasa/api/controllers" "github.com/Redume/EveryNasa/functions" - "github.com/Redume/EveryNasa/web/pages" + "github.com/Redume/EveryNasa/web/page" + "github.com/getlantern/systray" - "github.com/gorilla/mux" + "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/cors" ) func main() { - go functions.Logger("EveryNasa started") go functions.Database() go systray.Run(functions.Tray, functions.Quit) go functions.StartWallpaper() - router := mux.NewRouter() - router.Use(func(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Access-Control-Allow-Origin", "*") - w.Header().Set("Access-Control-Allow-Methods", "POST, GET") - w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization") - next.ServeHTTP(w, r) - }) + app := fiber.New() + app.Static("/static", "./web/static") + app.Use(cors.New()) + + app.Get("/", func(c *fiber.Ctx) error { + return page.Gallery(c) + }) + app.Get("/settings", func(c *fiber.Ctx) error { + return page.Settings(c) + }) + app.Get("/about", func(c *fiber.Ctx) error { + return page.About(c) }) - http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./web/static")))) + app.Post("/api/update/settings", func(c *fiber.Ctx) error { + return controllers.SettingsUpdate(c) + }) + app.Post("/api/update/wallpaper", func(c *fiber.Ctx) error { + return controllers.WallpaperUpdate(c) + }) + app.Post("/api/update/startup", func(c *fiber.Ctx) error { + return controllers.Startup(c) + }) + app.Post("/api/create/label", func(c *fiber.Ctx) error { + return controllers.CreateLabel(c) + }) - http.HandleFunc("/", page.GalleryHandler) - http.HandleFunc("/settings", page.SettingsHandler) - http.HandleFunc("/about", page.AboutHandler) + app.Get("/api/get/settings", func(c *fiber.Ctx) error { + return controllers.SettingsGet(c) + }) - router.HandleFunc("/api/get/settings", controllers.SettingsGet).Methods("GET") - router.HandleFunc("/api/get/version", controllers.Version).Methods("GET") - - router.HandleFunc("/api/update/settings", controllers.SettingsUpdate).Methods("POST") - router.HandleFunc("/api/update/wallpaper", controllers.WallpaperUpdate).Methods("POST") - router.HandleFunc("/api/update/startup", controllers.Startup).Methods("POST") - router.HandleFunc("/api/create/label", controllers.CreateLabel).Methods("POST") - - go func() { - err := http.ListenAndServe(":4662", nil) + app.Use(func(c *fiber.Ctx) error { + err := c.SendStatus(404) if err != nil { functions.Logger(err.Error()) } - }() + return c.SendFile("./web/errors/404.html") + }) - err := http.ListenAndServe(":8080", router) + err := app.Listen(":3000") if err != nil { - functions.Logger(err.Error()) + return } } diff --git a/web/page/about.go b/web/page/about.go new file mode 100644 index 0000000..23b3058 --- /dev/null +++ b/web/page/about.go @@ -0,0 +1,15 @@ +package page + +import ( + "github.com/Redume/EveryNasa/functions" + "github.com/gofiber/fiber/v2" +) + +func About(c *fiber.Ctx) error { + con := functions.Connected() + if con == false { + return c.SendFile("./web/errors/500.html") + } + + return c.SendFile("./web/src/about.html") +} diff --git a/web/page/gallery.go b/web/page/gallery.go new file mode 100644 index 0000000..90e53bf --- /dev/null +++ b/web/page/gallery.go @@ -0,0 +1,15 @@ +package page + +import ( + "github.com/Redume/EveryNasa/functions" + "github.com/gofiber/fiber/v2" +) + +func Gallery(c *fiber.Ctx) error { + con := functions.Connected() + if con == false { + return c.SendFile("./web/errors/500.html") + } + + return c.SendFile("./web/src/gallery.html") +} diff --git a/web/page/settings.go b/web/page/settings.go new file mode 100644 index 0000000..d3c37dc --- /dev/null +++ b/web/page/settings.go @@ -0,0 +1,15 @@ +package page + +import ( + "github.com/Redume/EveryNasa/functions" + "github.com/gofiber/fiber/v2" +) + +func Settings(c *fiber.Ctx) error { + con := functions.Connected() + if con == false { + return c.SendFile("./web/errors/500.html") + } + + return c.SendFile("./web/src/settings.html") +}