mirror of
https://github.com/idanoo/GoScrobble
synced 2025-07-01 13:42:20 +00:00
0.1.0 Split repos, Add API docs, new env vars
This commit is contained in:
parent
cf4498e8bc
commit
306bab997b
55 changed files with 19278 additions and 11 deletions
|
@ -25,6 +25,7 @@ var ReverseProxies []string
|
|||
// Directories
|
||||
var FrontendDirectory string
|
||||
var DataDirectory string
|
||||
var ApiDocsDirectory string
|
||||
|
||||
// RequestRequest - Incoming JSON!
|
||||
type RequestRequest struct {
|
||||
|
@ -96,27 +97,33 @@ func HandleRequests(port string) {
|
|||
// This just prevents it serving frontend stuff over /api
|
||||
r.PathPrefix("/api")
|
||||
|
||||
// SERVE STATIC FILES - NO AUTH
|
||||
// Serve Images
|
||||
spaStatic := spaStaticHandler{staticPath: DataDirectory}
|
||||
r.PathPrefix("/img").Handler(spaStatic)
|
||||
|
||||
apiDocs := spaStaticHandler{staticPath: "docs/api/build"}
|
||||
r.PathPrefix("/docs").Handler(apiDocs)
|
||||
// Serve API Docs
|
||||
apiDocs := apiDocHandler{staticPath: ApiDocsDirectory, indexPath: "index.html"}
|
||||
r.PathPrefix("/docs/").Handler(apiDocs)
|
||||
|
||||
// SERVE FRONTEND - NO AUTH
|
||||
// This is a really terrible work around to Slate using relative paths and not
|
||||
// picking up the css/img files when you don't have a trailing slash\
|
||||
apiDocRedirect := apiDocRedirectHandler{}
|
||||
r.PathPrefix("/docs").Handler(apiDocRedirect)
|
||||
|
||||
// Serve Frontend
|
||||
spa := spaHandler{staticPath: FrontendDirectory + string(os.PathSeparator) + "build", indexPath: "index.html"}
|
||||
r.PathPrefix("/").Handler(spa)
|
||||
|
||||
// Setup CORS rules
|
||||
c := cors.New(cors.Options{
|
||||
AllowedOrigins: []string{"*"},
|
||||
AllowCredentials: true,
|
||||
AllowedMethods: []string{"GET", "POST", "PATCH", "DELETE"},
|
||||
AllowedHeaders: []string{"*"},
|
||||
})
|
||||
|
||||
handler := c.Handler(r)
|
||||
|
||||
// Serve it up!
|
||||
// Serve it!
|
||||
fmt.Printf("Goscrobble listening on port %s", port)
|
||||
fmt.Println("")
|
||||
|
||||
|
@ -781,7 +788,7 @@ func getServerInfo(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
info := ServerInfo{
|
||||
Version: "0.0.33",
|
||||
Version: "0.1.0",
|
||||
RegistrationEnabled: cachedRegistrationEnabled,
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// spaStaticHandler - Handles static imges
|
||||
|
@ -18,7 +19,17 @@ type spaHandler struct {
|
|||
indexPath string
|
||||
}
|
||||
|
||||
// ServerHTTP - Frontend React server
|
||||
// apiDocHandler - Handles API Docs
|
||||
type apiDocHandler struct {
|
||||
staticPath string
|
||||
indexPath string
|
||||
}
|
||||
|
||||
// apiDocRedirectHandler - Handles redirect to add trailing slash (Fixes relative URLs...)
|
||||
type apiDocRedirectHandler struct {
|
||||
}
|
||||
|
||||
// ServeHTTP - Frontend React server
|
||||
func (h spaStaticHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
// Get the absolute path to prevent directory traversal
|
||||
path, err := filepath.Abs(r.URL.Path)
|
||||
|
@ -43,7 +54,7 @@ func (h spaStaticHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
http.FileServer(http.Dir(h.staticPath)).ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
// ServerHTTP - Frontend React server
|
||||
// ServeHTTP - Frontend React server
|
||||
func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
// Get the absolute path to prevent directory traversal
|
||||
path, err := filepath.Abs(r.URL.Path)
|
||||
|
@ -72,3 +83,42 @@ func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
// otherwise, use http.FileServer to serve the static dir
|
||||
http.FileServer(http.Dir(h.staticPath)).ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
// ServeHTTP - API Redirect to add trailing slash
|
||||
func (h apiDocRedirectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, "/docs/", 301)
|
||||
return
|
||||
}
|
||||
|
||||
// ServeHTTP - API Docs
|
||||
func (h apiDocHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
// Get the absolute path to prevent directory traversal
|
||||
path, err := filepath.Abs(r.URL.Path)
|
||||
if err != nil {
|
||||
// If we failed to get the absolute path respond with a 400 bad request and return
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
path = filepath.Join(h.staticPath, strings.Replace(path, "/docs", "", 1))
|
||||
|
||||
info, err := os.Stat(path)
|
||||
if info.IsDir() {
|
||||
// Root directory, server index.html
|
||||
http.ServeFile(w, r, filepath.Join(h.staticPath, h.indexPath))
|
||||
return
|
||||
} else if os.IsNotExist(err) {
|
||||
// file does not exist, serve index.html
|
||||
http.ServeFile(w, r, filepath.Join(h.staticPath, h.indexPath))
|
||||
return
|
||||
} else if err != nil {
|
||||
// if we got an error (that wasn't that the file doesn't exist) stating the
|
||||
// file, return a 500 internal server error and stop
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Strip /docs from request and serve folder
|
||||
handler := http.FileServer(http.Dir(h.staticPath))
|
||||
http.StripPrefix("/docs", handler).ServeHTTP(w, r)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue