Better handling of react app
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"go-sjles-pta-vote/server/models"
|
||||
@@ -56,7 +57,6 @@ func voteIDHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func statsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "GET" {
|
||||
filePath := "./server/templates/stats.html"
|
||||
log.Printf("Serving stats.html from %s", filePath)
|
||||
http.ServeFile(w, r, filePath)
|
||||
} else if r.Method == "POST" {
|
||||
vars := mux.Vars(r)
|
||||
@@ -117,7 +117,6 @@ func adminMembersHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
if y := r.FormValue("year"); y != "" {
|
||||
year, err = strconv.Atoi(y)
|
||||
log.Printf("Parsed year: %d", year)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid year", http.StatusBadRequest)
|
||||
return
|
||||
@@ -150,51 +149,31 @@ func adminMembersHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// Add a new handler for the client's AdminMembersPage
|
||||
func adminMembersClientHandler(w http.ResponseWriter, r *http.Request) {
|
||||
filePath := "./client/build/admin-members.html"
|
||||
if _, err := os.Stat(filePath); err == nil {
|
||||
http.ServeFile(w, r, filePath)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
log.SetOutput(os.Stdout)
|
||||
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||
|
||||
log.Printf("Starting server on :8080")
|
||||
http.HandleFunc("/api/vote", voteHandler)
|
||||
http.HandleFunc("/api/vote/{id}", voteIDHandler)
|
||||
http.HandleFunc("/api/stats", statsHandler)
|
||||
http.HandleFunc("/api/stats/{id}", statsIDHandler)
|
||||
http.HandleFunc("/api/admin", adminHandler)
|
||||
http.HandleFunc("/api/admin/{id}", adminIDHandler)
|
||||
http.HandleFunc("/api/admin/members", adminMembersHandler)
|
||||
|
||||
http.HandleFunc("/vote", voteHandler)
|
||||
http.HandleFunc("/vote/{id}", voteIDHandler)
|
||||
http.HandleFunc("/stats", statsHandler)
|
||||
http.HandleFunc("/stats/{id}", statsIDHandler)
|
||||
http.HandleFunc("/admin", adminHandler)
|
||||
http.HandleFunc("/admin/{id}", adminIDHandler)
|
||||
http.HandleFunc("/admin/members", adminMembersHandler)
|
||||
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
|
||||
filePath := "./server/icons/favicon.ico"
|
||||
http.ServeFile(w, r, filePath)
|
||||
})
|
||||
http.Handle("/", http.FileServer(http.Dir("./client/build")))
|
||||
|
||||
// Add this function to handle static files during development
|
||||
http.HandleFunc("/static/", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/" {
|
||||
http.ServeFile(w, r, "./client/build/index.html")
|
||||
} else {
|
||||
file := "./client/build" + r.URL.Path
|
||||
if _, err := os.Stat(file); err == nil {
|
||||
http.ServeFile(w, r, file)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
buildPath := filepath.Join(".", "client", "build")
|
||||
fs := http.FileServer(http.Dir(buildPath))
|
||||
|
||||
http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// If the file exists on disk, let the file server handle it.
|
||||
if _, err := os.Stat(filepath.Join(buildPath, r.URL.Path)); err == nil {
|
||||
fs.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
// Add this line to handle client's AdminMembersPage
|
||||
http.HandleFunc("/admin-members", adminMembersClientHandler)
|
||||
// Otherwise serve index.html (so React Router can handle the route)
|
||||
http.ServeFile(w, r, filepath.Join(buildPath, "index.html"))
|
||||
}))
|
||||
|
||||
log.Printf("Starting server on :8080")
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user