First attempt at react interface
This commit is contained in:
@@ -102,15 +102,22 @@ func adminIDHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func adminMembersHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "GET" {
|
||||
filePath := "./server/templates/members.html"
|
||||
log.Printf("Serving members.html from %s", filePath)
|
||||
http.ServeFile(w, r, filePath)
|
||||
// Redirect to client's AdminMembersPage instead of serving members.html
|
||||
w.Header().Set("Location", "/admin-members")
|
||||
w.WriteHeader(http.StatusFound)
|
||||
return
|
||||
} else if r.Method == "POST" {
|
||||
var year int
|
||||
var err error
|
||||
r.ParseForm()
|
||||
|
||||
if err = r.ParseMultipartForm(10 << 20); err != nil {
|
||||
http.Error(w, "Failed to parse multipart form", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
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
|
||||
@@ -130,13 +137,12 @@ func adminMembersHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
err = services.ParseMembersFromBytes(year, fileBytes)
|
||||
if err != nil {
|
||||
if err = services.ParseMembersFromBytes(year, fileBytes); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string]bool{"success": true})
|
||||
} else {
|
||||
@@ -144,6 +150,16 @@ 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)
|
||||
@@ -161,6 +177,24 @@ func main() {
|
||||
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)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Add this line to handle client's AdminMembersPage
|
||||
http.HandleFunc("/admin-members", adminMembersClientHandler)
|
||||
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user