Better handling of react app
This commit is contained in:
@@ -40,11 +40,8 @@ CREATE TABLE IF NOT EXISTS members (
|
||||
var db *sql.DB
|
||||
|
||||
func Connect() (*sql.DB, error) {
|
||||
log.Printf("Connecting to database")
|
||||
|
||||
db_config := config.GetConfig()
|
||||
|
||||
log.Printf("Database path: %s", db_config.DBPath)
|
||||
db, err := sql.Open("sqlite", db_config.DBPath)
|
||||
if err != nil {
|
||||
log.Printf("Error opening database: %v", err)
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Upload Members</title>
|
||||
<link rel="icon" type="image/x-icon" href="/favicon.ico">
|
||||
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
|
||||
<script src="https://unpkg.com/react@17/umd/react.production.min.js"></script>
|
||||
<script src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
|
||||
<script type="text/babel">
|
||||
function MembersForm() {
|
||||
const [year, setYear] = React.useState(0);
|
||||
const [file, setFile] = React.useState(null);
|
||||
|
||||
const handleSubmit = (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
if (!year || !file) {
|
||||
alert('Both year and file are required!');
|
||||
return;
|
||||
}
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('year', year);
|
||||
formData.append('members.csv', file);
|
||||
|
||||
fetch('/admin/members', {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
alert('Members uploaded successfully!');
|
||||
} else {
|
||||
alert(`Error: ${data.error}`);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<form onSubmit={handleSubmit}>
|
||||
<h1>Upload Members CSV</h1>
|
||||
<label htmlFor="year">Year:</label>
|
||||
<input
|
||||
type="number"
|
||||
id="year"
|
||||
name="year"
|
||||
value={year}
|
||||
onChange={(e) => setYear(e.target.value)}
|
||||
required
|
||||
/><br/><br/>
|
||||
|
||||
<label htmlFor="members.csv">CSV File:</label>
|
||||
<input
|
||||
type="file"
|
||||
id="members.csv"
|
||||
name="members.csv"
|
||||
accept=".csv"
|
||||
onChange={(e) => setFile(e.target.files[0])}
|
||||
required
|
||||
/><br/><br/>
|
||||
|
||||
<button type="submit">Upload</button>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
const root = ReactDOM.createRoot(document.getElementById('root'));
|
||||
root.render(
|
||||
<MembersForm />
|
||||
);
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,10 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Stats</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Hello World!</h1>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user