From 3afc5c2c00e0dae7f85cee7a9f29c5fef6b4575c Mon Sep 17 00:00:00 2001 From: Redume Date: Sun, 23 Oct 2022 15:27:36 +0300 Subject: [PATCH] rewritten from net/http to fiber, changed port from 8080, 4662 to one 3000 --- api/controllers/settings.go | 33 +++++++++++++++++---------------- api/controllers/startup.go | 30 +++++++++++++++++------------- 2 files changed, 34 insertions(+), 29 deletions(-) diff --git a/api/controllers/settings.go b/api/controllers/settings.go index a2a6bbb..63270f2 100644 --- a/api/controllers/settings.go +++ b/api/controllers/settings.go @@ -2,21 +2,20 @@ package controllers import ( "database/sql" - "fmt" - "net/http" - "github.com/Redume/EveryNasa/api/utils" "github.com/Redume/EveryNasa/functions" + "github.com/gofiber/fiber/v2" _ "github.com/mattn/go-sqlite3" ) -var SettingsGet = func(w http.ResponseWriter, r *http.Request) { +var SettingsGet = func(c *fiber.Ctx) error { db, err := sql.Open("sqlite3", "EveryNasa.db") if err != nil { functions.Logger(err.Error()) } - query, err := db.Query("SELECT * FROM settings") + querySettings, err := db.Query("SELECT * FROM settings") + if err != nil { functions.Logger(err.Error()) } @@ -26,32 +25,34 @@ var SettingsGet = func(w http.ResponseWriter, r *http.Request) { if err != nil { functions.Logger(err.Error()) } - }(query) + }(querySettings) var startup, wallpaper int - for query.Next() { - err := query.Scan(&startup, &wallpaper) + for querySettings.Next() { + err := querySettings.Scan(&startup, &wallpaper) if err != nil { functions.Logger(err.Error()) } var data = map[string]interface{}{"startup": startup, "wallpaper": wallpaper} - utils.Respond(w, data) + utils.Respond(c, data) } + + return nil } -var SettingsUpdate = func(w http.ResponseWriter, r *http.Request) { +var SettingsUpdate = func(c *fiber.Ctx) error { db, err := sql.Open("sqlite3", "EveryNasa.db") if err != nil { functions.Logger(err.Error()) } - wallpaper := r.FormValue("wallpaper") - startup := r.FormValue("startup") + startup := c.FormValue("startup") + wallpaper := c.FormValue("wallpaper") if startup == "" && wallpaper == "" { - utils.Respond(w, utils.Message(false, "All fields are required.")) - return + utils.Respond(c, utils.Message(false, "All fields are required.")) + return nil } if wallpaper != "" { @@ -61,7 +62,6 @@ var SettingsUpdate = func(w http.ResponseWriter, r *http.Request) { } if wallpaper == "1" { - fmt.Println("Wallpaper enabled") go functions.StartWallpaper() } } @@ -73,5 +73,6 @@ var SettingsUpdate = func(w http.ResponseWriter, r *http.Request) { } } - utils.Respond(w, utils.Message(true, "The settings have been applied successfully.")) + utils.Respond(c, utils.Message(true, "The settings have been applied successfully.")) + return nil } diff --git a/api/controllers/startup.go b/api/controllers/startup.go index 31635f6..749dab7 100644 --- a/api/controllers/startup.go +++ b/api/controllers/startup.go @@ -1,7 +1,7 @@ package controllers import ( - "net/http" + "github.com/gofiber/fiber/v2" "os" "os/user" "strings" @@ -10,24 +10,26 @@ import ( "github.com/Redume/EveryNasa/functions" ) -var Startup = func(w http.ResponseWriter, r *http.Request) { - startup := r.FormValue("startup") +var Startup = func(c *fiber.Ctx) error { + startup := c.FormValue("startup") if startup == "" { - utils.Respond(w, utils.Message(false, "All fields are required.")) - return + utils.Respond(c, utils.Message(false, "All fields are required.")) + return nil } if startup == "1" { - SetStartup(w, r) + SetStartup(c) } else if startup == "0" { - RemoveStartup(w, r) + RemoveStartup(c) } else { - utils.Respond(w, utils.Message(false, "Invalid field.")) - return + utils.Respond(c, utils.Message(false, "Invalid field.")) + return nil } + + return nil } -var SetStartup = func(w http.ResponseWriter, r *http.Request) { +var SetStartup = func(c *fiber.Ctx) error { u, err := user.Current() if err != nil { functions.Logger(err.Error()) @@ -45,10 +47,11 @@ var SetStartup = func(w http.ResponseWriter, r *http.Request) { functions.Logger(err.Error()) } - utils.Respond(w, utils.Message(true, "The settings have been applied successfully.")) + utils.Respond(c, utils.Message(true, "The settings have been applied successfully.")) + return nil } -var RemoveStartup = func(w http.ResponseWriter, r *http.Request) { +var RemoveStartup = func(c *fiber.Ctx) error { u, err := user.Current() if err != nil { functions.Logger(err.Error()) @@ -59,5 +62,6 @@ var RemoveStartup = func(w http.ResponseWriter, r *http.Request) { functions.Logger(err.Error()) } - utils.Respond(w, utils.Message(true, "The settings have been applied successfully.")) + utils.Respond(c, utils.Message(true, "The settings have been applied successfully.")) + return nil }