added admin functions for user management and working on database management
This commit is contained in:
parent
3cb082b3b0
commit
2d63c02048
|
@ -0,0 +1,85 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
// GetUsersHandler handles GET requests to /admin/user
|
||||
func GetUsersHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var users []User
|
||||
db.Find(&users)
|
||||
json.NewEncoder(w).Encode(users)
|
||||
}
|
||||
|
||||
// CreateUserHandler handles POST requests to /admin/user
|
||||
func CreateUserHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var user User
|
||||
err := json.NewDecoder(r.Body).Decode(&user)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
db.Create(&user)
|
||||
json.NewEncoder(w).Encode(user)
|
||||
}
|
||||
|
||||
// GetUserHandler handles GET requests to /admin/user/{id}
|
||||
func GetUserHandler(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
id := vars["id"]
|
||||
var user User
|
||||
db.First(&user, id)
|
||||
if user.ID == 0 {
|
||||
http.Error(w, "User not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(user)
|
||||
}
|
||||
|
||||
// DeleteUserHandler handles DELETE requests to /admin/user/{id}
|
||||
func DeleteUserHandler(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
id := vars["id"]
|
||||
var user User
|
||||
db.First(&user, id)
|
||||
if user.ID == 0 {
|
||||
http.Error(w, "User not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
db.Delete(&user)
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// BackupDatabaseHandler handles GET requests to /admin/db
|
||||
func BackupDatabaseHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// ...
|
||||
fmt.Println("BackupDatabaseHandler called")
|
||||
// Open the database file using the path from the config
|
||||
file, err := os.Open(config.DatabasePath)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to open database file", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Copy the file to the response writer
|
||||
_, err = io.Copy(w, file)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to send database file", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// RestoreDatabaseHandler handles POST requests to /admin/db
|
||||
func RestoreDatabaseHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// implement database restore logic here
|
||||
fmt.Println("Restoring database...")
|
||||
// ...
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
11
main.go
11
main.go
|
@ -65,11 +65,22 @@ func main() {
|
|||
Methods("POST").
|
||||
Handler(AuthMiddleware(http.HandlerFunc(UploadItemImageHandler)))
|
||||
|
||||
managementRouter := router.PathPrefix("/admin").Subrouter()
|
||||
managementRouter.Use(AuthMiddleware)
|
||||
|
||||
managementRouter.Handle("/user", http.HandlerFunc(GetUsersHandler)).Methods("GET", "OPTIONS")
|
||||
managementRouter.Handle("/user", http.HandlerFunc(CreateUserHandler)).Methods("POST", "OPTIONS")
|
||||
managementRouter.Handle("/user/{id}", http.HandlerFunc(GetUserHandler)).Methods("GET", "OPTIONS")
|
||||
managementRouter.Handle("/user/{id}", http.HandlerFunc(DeleteUserHandler)).Methods("DELETE", "OPTIONS")
|
||||
managementRouter.Handle("/db", http.HandlerFunc(BackupDatabaseHandler)).Methods("GET", "OPTIONS")
|
||||
managementRouter.Handle("/db", http.HandlerFunc(RestoreDatabaseHandler)).Methods("POST", "OPTIONS")
|
||||
|
||||
// Apply CORS middleware
|
||||
c := cors.New(cors.Options{
|
||||
AllowedOrigins: []string{"http://localhost:3000", "http://10.0.0.16:3000"}, // Change this to your frontend domain
|
||||
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
||||
AllowedHeaders: []string{"Authorization", "Content-Type"},
|
||||
ExposedHeaders: []string{"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Cache-Control", "Content-Language", "Content-Type", "Expires", "Last-Modified", "Pragma", "ETag"},
|
||||
AllowCredentials: true,
|
||||
})
|
||||
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
#!/bin/bash
|
||||
|
||||
# API base URL
|
||||
API_BASE_URL="http://localhost:8080"
|
||||
|
||||
# Login credentials
|
||||
USERNAME="boxuser"
|
||||
PASSWORD="boxuser"
|
||||
|
||||
# Get a new JWT token
|
||||
TOKEN=$(curl -s -X POST -H "Content-Type: application/json" \
|
||||
-d "{\"username\":\"$USERNAME\", \"password\":\"$PASSWORD\"}" \
|
||||
"$API_BASE_URL/login" | jq -r '.token')
|
||||
|
||||
curl -X GET \
|
||||
$API_BASE_URL/admin/db \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
--output ./test.db
|
|
@ -0,0 +1,23 @@
|
|||
#!/bin/bash
|
||||
|
||||
# API base URL
|
||||
API_BASE_URL="http://localhost:8080"
|
||||
|
||||
# Login credentials
|
||||
USERNAME="boxuser"
|
||||
PASSWORD="boxuser"
|
||||
|
||||
JSON_PAYLOAD='{
|
||||
"username": "testuser",
|
||||
"password": "testuser"
|
||||
}'
|
||||
|
||||
# Get a new JWT token
|
||||
TOKEN=$(curl -s -X POST -H "Content-Type: application/json" \
|
||||
-d "{\"username\":\"$USERNAME\", \"password\":\"$PASSWORD\"}" \
|
||||
"$API_BASE_URL/login" | jq -r '.token')
|
||||
|
||||
curl -X DELETE \
|
||||
$API_BASE_URL/admin/user/2 \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
|
@ -0,0 +1,18 @@
|
|||
#!/bin/bash
|
||||
|
||||
# API base URL
|
||||
API_BASE_URL="http://localhost:8080"
|
||||
|
||||
# Login credentials
|
||||
USERNAME="boxuser"
|
||||
PASSWORD="boxuser"
|
||||
|
||||
# Get a new JWT token
|
||||
TOKEN=$(curl -s -X POST -H "Content-Type: application/json" \
|
||||
-d "{\"username\":\"$USERNAME\", \"password\":\"$PASSWORD\"}" \
|
||||
"$API_BASE_URL/login" | jq -r '.token')
|
||||
|
||||
curl -X GET \
|
||||
$API_BASE_URL/admin/user \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Content-Type: application/json"
|
|
@ -0,0 +1,24 @@
|
|||
#!/bin/bash
|
||||
|
||||
# API base URL
|
||||
API_BASE_URL="http://localhost:8080"
|
||||
|
||||
# Login credentials
|
||||
USERNAME="boxuser"
|
||||
PASSWORD="boxuser"
|
||||
|
||||
JSON_PAYLOAD='{
|
||||
"username": "testuser",
|
||||
"password": "testuser"
|
||||
}'
|
||||
|
||||
# Get a new JWT token
|
||||
TOKEN=$(curl -s -X POST -H "Content-Type: application/json" \
|
||||
-d "{\"username\":\"$USERNAME\", \"password\":\"$PASSWORD\"}" \
|
||||
"$API_BASE_URL/login" | jq -r '.token')
|
||||
|
||||
curl -X POST \
|
||||
$API_BASE_URL/admin/user \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$JSON_PAYLOAD"
|
Loading…
Reference in New Issue