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

This commit is contained in:
Данил 2022-10-23 15:26:51 +03:00
parent 42f568cf85
commit 16301a3428
8 changed files with 102 additions and 47 deletions

View file

@ -1,16 +1,16 @@
package controllers package controllers
import ( import (
"net/http"
"os" "os"
"os/user" "os/user"
"strings" "strings"
"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"
) )
var CreateLabel = func(w http.ResponseWriter, r *http.Request) { var CreateLabel = 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())
@ -28,5 +28,6 @@ var CreateLabel = func(w http.ResponseWriter, r *http.Request) {
functions.Logger(err.Error()) 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
} }

View file

@ -1,27 +1,28 @@
package controllers package controllers
import ( import (
"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/reujab/wallpaper" "github.com/reujab/wallpaper"
) )
var WallpaperUpdate = func(w http.ResponseWriter, r *http.Request) { var WallpaperUpdate = func(c *fiber.Ctx) error {
var url string var url string
url = r.FormValue("url") url = c.FormValue("url")
if url == "" { if url == "" {
utils.Respond(w, utils.Message(false, "URL is required")) utils.Respond(c, utils.Message(false, "URL is required"))
return return nil
} }
err := wallpaper.SetFromURL(url) err := wallpaper.SetFromURL(url)
if err != nil { if err != nil {
functions.Logger(err.Error()) functions.Logger(err.Error())
utils.Respond(w, utils.Message(false, "An error occurred while setting the wallpaper")) utils.Respond(c, utils.Message(false, "An error occurred while setting the wallpaper"))
return return nil
} }
utils.Respond(w, utils.Message(true, "Wallpaper successfully updated")) utils.Respond(c, utils.Message(true, "Wallpaper successfully updated"))
return nil
} }

View file

@ -2,17 +2,18 @@ package utils
import ( import (
"encoding/json" "encoding/json"
"github.com/Redume/EveryNasa/functions" "github.com/Redume/EveryNasa/functions"
"net/http" "github.com/gofiber/fiber/v2"
) )
func Message(status bool, message string) map[string]interface{} { func Message(status bool, message string) map[string]interface{} {
return map[string]interface{}{"status": status, "message": message} return map[string]interface{}{"status": status, "message": message}
} }
func Respond(w http.ResponseWriter, data map[string]interface{}) { func Respond(c *fiber.Ctx, data map[string]interface{}) {
w.Header().Add("Content-Type", "application/json") c.Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(data) err := json.NewEncoder(c).Encode(data)
if err != nil { if err != nil {
functions.Logger(err.Error()) functions.Logger(err.Error())
} }

View file

@ -19,7 +19,7 @@ func Tray() {
for { for {
select { select {
case <-ui.ClickedCh: case <-ui.ClickedCh:
err := open.Run("http://localhost:4662") err := open.Run("http://localhost:3000")
if err != nil { if err != nil {
Logger(err.Error()) Logger(err.Error())
} }

65
main.go
View file

@ -1,54 +1,61 @@
package main package main
import ( import (
"net/http"
"github.com/Redume/EveryNasa/api/controllers" "github.com/Redume/EveryNasa/api/controllers"
"github.com/Redume/EveryNasa/functions" "github.com/Redume/EveryNasa/functions"
"github.com/Redume/EveryNasa/web/pages" "github.com/Redume/EveryNasa/web/page"
"github.com/getlantern/systray" "github.com/getlantern/systray"
"github.com/gorilla/mux" "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
) )
func main() { func main() {
go functions.Logger("EveryNasa started")
go functions.Database() go functions.Database()
go systray.Run(functions.Tray, functions.Quit) go systray.Run(functions.Tray, functions.Quit)
go functions.StartWallpaper() go functions.StartWallpaper()
router := mux.NewRouter() app := fiber.New()
router.Use(func(next http.Handler) http.Handler { app.Static("/static", "./web/static")
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { app.Use(cors.New())
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET") app.Get("/", func(c *fiber.Ctx) error {
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization") return page.Gallery(c)
next.ServeHTTP(w, r)
}) })
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) app.Get("/api/get/settings", func(c *fiber.Ctx) error {
http.HandleFunc("/settings", page.SettingsHandler) return controllers.SettingsGet(c)
http.HandleFunc("/about", page.AboutHandler) })
router.HandleFunc("/api/get/settings", controllers.SettingsGet).Methods("GET") app.Use(func(c *fiber.Ctx) error {
router.HandleFunc("/api/get/version", controllers.Version).Methods("GET") err := c.SendStatus(404)
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)
if err != nil { if err != nil {
functions.Logger(err.Error()) functions.Logger(err.Error())
} }
}() return c.SendFile("./web/errors/404.html")
})
err := http.ListenAndServe(":8080", router) err := app.Listen(":3000")
if err != nil { if err != nil {
functions.Logger(err.Error()) return
} }
} }

15
web/page/about.go Normal file
View file

@ -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")
}

15
web/page/gallery.go Normal file
View file

@ -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")
}

15
web/page/settings.go Normal file
View file

@ -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")
}