mirror of
https://github.com/mgerb/go-discord-bot
synced 2026-01-10 17:12:48 +00:00
switched to gin - use logrus
This commit is contained in:
@@ -1,53 +1,32 @@
|
||||
package webserver
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/acme/autocert"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mgerb/go-discord-bot/server/config"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/handlers"
|
||||
"github.com/mgerb/go-discord-bot/server/webserver/pubg"
|
||||
)
|
||||
|
||||
func getRouter() *chi.Mux {
|
||||
func getRouter() *gin.Engine {
|
||||
router := gin.Default()
|
||||
|
||||
r := chi.NewRouter()
|
||||
router.Static("/static", "./dist/static")
|
||||
router.Static("/public/sounds", config.Config.SoundsPath)
|
||||
router.Static("/public/youtube", "./youtube")
|
||||
router.Static("/public/clips", config.Config.ClipsPath)
|
||||
|
||||
r.Use(middleware.RequestID)
|
||||
r.Use(middleware.DefaultCompress)
|
||||
|
||||
if !config.Flags.Prod {
|
||||
r.Use(middleware.Logger)
|
||||
}
|
||||
|
||||
workDir, _ := os.Getwd()
|
||||
|
||||
FileServer(r, "/static", http.Dir(filepath.Join(workDir, "./dist/static")))
|
||||
FileServer(r, "/public/sounds", http.Dir(filepath.Join(workDir, config.Config.SoundsPath)))
|
||||
FileServer(r, "/public/youtube", http.Dir(filepath.Join(workDir, "./youtube")))
|
||||
FileServer(r, "/public/clips", http.Dir(filepath.Join(workDir, config.Config.ClipsPath)))
|
||||
|
||||
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, "./dist/index.html")
|
||||
router.NoRoute(func(c *gin.Context) {
|
||||
c.File("./dist/index.html")
|
||||
})
|
||||
|
||||
r.Route("/api", func(r chi.Router) {
|
||||
// configure api end points
|
||||
r.Get("/soundlist", handlers.SoundList)
|
||||
r.Get("/cliplist", handlers.ClipList)
|
||||
r.Put("/upload", handlers.FileUpload)
|
||||
r.Get("/ytdownloader", handlers.Downloader)
|
||||
r.Get("/stats/pubg", pubg.Handler)
|
||||
})
|
||||
api := router.Group("/api")
|
||||
api.GET("/stats/pubg", pubg.Handler)
|
||||
api.GET("/ytdownloader", handlers.Downloader)
|
||||
api.GET("/soundlist", handlers.SoundList)
|
||||
api.GET("/cliplist", handlers.ClipList)
|
||||
api.POST("/upload", handlers.FileUpload)
|
||||
|
||||
return r
|
||||
return router
|
||||
}
|
||||
|
||||
// Start -
|
||||
@@ -59,49 +38,5 @@ func Start() {
|
||||
}
|
||||
|
||||
router := getRouter()
|
||||
|
||||
if config.Flags.TLS {
|
||||
|
||||
// start server on port 80 to redirect
|
||||
go http.ListenAndServe(":80", http.HandlerFunc(redirect))
|
||||
|
||||
// start TLS server
|
||||
log.Fatal(http.Serve(autocert.NewListener(), router))
|
||||
|
||||
} else {
|
||||
|
||||
// start basic server
|
||||
http.ListenAndServe(config.Config.ServerAddr, router)
|
||||
}
|
||||
}
|
||||
|
||||
// FileServer conveniently sets up a http.FileServer handler to serve
|
||||
// static files from a http.FileSystem.
|
||||
func FileServer(r chi.Router, path string, root http.FileSystem) {
|
||||
if strings.ContainsAny(path, "{}*") {
|
||||
panic("FileServer does not permit URL parameters.")
|
||||
}
|
||||
|
||||
fs := http.StripPrefix(path, http.FileServer(root))
|
||||
|
||||
if path != "/" && path[len(path)-1] != '/' {
|
||||
r.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP)
|
||||
path += "/"
|
||||
}
|
||||
path += "*"
|
||||
|
||||
r.Get(path, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
fs.ServeHTTP(w, r)
|
||||
}))
|
||||
}
|
||||
|
||||
// redirect to https
|
||||
func redirect(w http.ResponseWriter, req *http.Request) {
|
||||
// remove/add not default ports from req.Host
|
||||
target := "https://" + req.Host + req.URL.Path
|
||||
if len(req.URL.RawQuery) > 0 {
|
||||
target += "?" + req.URL.RawQuery
|
||||
}
|
||||
|
||||
http.Redirect(w, req, target, http.StatusTemporaryRedirect)
|
||||
router.Run(config.Config.ServerAddr)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user