rewritten from net/http to fiber, changed port from 8080, 4662 to one 3000

This commit is contained in:
Данил 2022-10-23 15:27:36 +03:00
parent bdab0c6194
commit 3afc5c2c00
2 changed files with 34 additions and 29 deletions

View file

@ -2,21 +2,20 @@ package controllers
import ( import (
"database/sql" "database/sql"
"fmt"
"net/http"
"github.com/Redume/EveryNasa/api/utils" "github.com/Redume/EveryNasa/api/utils"
"github.com/Redume/EveryNasa/functions" "github.com/Redume/EveryNasa/functions"
"github.com/gofiber/fiber/v2"
_ "github.com/mattn/go-sqlite3" _ "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") db, err := sql.Open("sqlite3", "EveryNasa.db")
if err != nil { if err != nil {
functions.Logger(err.Error()) functions.Logger(err.Error())
} }
query, err := db.Query("SELECT * FROM settings") querySettings, err := db.Query("SELECT * FROM settings")
if err != nil { if err != nil {
functions.Logger(err.Error()) functions.Logger(err.Error())
} }
@ -26,32 +25,34 @@ var SettingsGet = func(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
functions.Logger(err.Error()) functions.Logger(err.Error())
} }
}(query) }(querySettings)
var startup, wallpaper int var startup, wallpaper int
for query.Next() { for querySettings.Next() {
err := query.Scan(&startup, &wallpaper) err := querySettings.Scan(&startup, &wallpaper)
if err != nil { if err != nil {
functions.Logger(err.Error()) functions.Logger(err.Error())
} }
var data = map[string]interface{}{"startup": startup, "wallpaper": wallpaper} 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") db, err := sql.Open("sqlite3", "EveryNasa.db")
if err != nil { if err != nil {
functions.Logger(err.Error()) functions.Logger(err.Error())
} }
wallpaper := r.FormValue("wallpaper") startup := c.FormValue("startup")
startup := r.FormValue("startup") wallpaper := c.FormValue("wallpaper")
if startup == "" && wallpaper == "" { if startup == "" && wallpaper == "" {
utils.Respond(w, utils.Message(false, "All fields are required.")) utils.Respond(c, utils.Message(false, "All fields are required."))
return return nil
} }
if wallpaper != "" { if wallpaper != "" {
@ -61,7 +62,6 @@ var SettingsUpdate = func(w http.ResponseWriter, r *http.Request) {
} }
if wallpaper == "1" { if wallpaper == "1" {
fmt.Println("Wallpaper enabled")
go functions.StartWallpaper() 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
} }

View file

@ -1,7 +1,7 @@
package controllers package controllers
import ( import (
"net/http" "github.com/gofiber/fiber/v2"
"os" "os"
"os/user" "os/user"
"strings" "strings"
@ -10,24 +10,26 @@ import (
"github.com/Redume/EveryNasa/functions" "github.com/Redume/EveryNasa/functions"
) )
var Startup = func(w http.ResponseWriter, r *http.Request) { var Startup = func(c *fiber.Ctx) error {
startup := r.FormValue("startup") startup := c.FormValue("startup")
if startup == "" { if startup == "" {
utils.Respond(w, utils.Message(false, "All fields are required.")) utils.Respond(c, utils.Message(false, "All fields are required."))
return return nil
} }
if startup == "1" { if startup == "1" {
SetStartup(w, r) SetStartup(c)
} else if startup == "0" { } else if startup == "0" {
RemoveStartup(w, r) RemoveStartup(c)
} else { } else {
utils.Respond(w, utils.Message(false, "Invalid field.")) utils.Respond(c, utils.Message(false, "Invalid field."))
return return nil
} }
return nil
} }
var SetStartup = func(w http.ResponseWriter, r *http.Request) { var SetStartup = func(c *fiber.Ctx) error {
u, err := user.Current() u, err := user.Current()
if err != nil { if err != nil {
functions.Logger(err.Error()) functions.Logger(err.Error())
@ -45,10 +47,11 @@ var SetStartup = func(w http.ResponseWriter, r *http.Request) {
functions.Logger(err.Error()) 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() u, err := user.Current()
if err != nil { if err != nil {
functions.Logger(err.Error()) functions.Logger(err.Error())
@ -59,5 +62,6 @@ var RemoveStartup = func(w http.ResponseWriter, r *http.Request) {
functions.Logger(err.Error()) 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
} }