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
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
}

View file

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

View file

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

View file

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

65
main.go
View file

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

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