2024-10-05 01:10:35 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2024-10-07 15:28:16 +00:00
|
|
|
"os"
|
2024-10-18 15:00:04 +00:00
|
|
|
"strings"
|
2024-10-05 01:10:35 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/jinzhu/gorm"
|
2024-10-05 03:05:30 +00:00
|
|
|
"github.com/rs/cors"
|
2024-10-05 01:10:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
db *gorm.DB // Declare db globally
|
|
|
|
config *Config
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-10-07 15:28:16 +00:00
|
|
|
|
2024-10-18 15:00:04 +00:00
|
|
|
configFile := os.Getenv("BOXES_API_CONFIG")
|
2024-10-05 01:10:35 +00:00
|
|
|
var err error
|
2024-10-07 15:28:16 +00:00
|
|
|
config, err = LoadConfig(configFile)
|
|
|
|
|
2024-10-18 15:00:04 +00:00
|
|
|
// Get the allowed origins from the ALLOWED_ORIGINS environment variable
|
|
|
|
// If empty, defaults to http://localhost:3000
|
|
|
|
allowedOrigins := os.Getenv("BOXES_API_ALLOWED_ORIGINS")
|
|
|
|
origins := []string{"http://localhost:3000"} // Default value
|
|
|
|
|
|
|
|
if allowedOrigins != "" {
|
|
|
|
// Split the comma-separated string into a slice of strings
|
|
|
|
origins = strings.Split(allowedOrigins, ",")
|
|
|
|
fmt.Println("Listening for connections from: ", origins)
|
|
|
|
}
|
|
|
|
|
2024-10-07 15:28:16 +00:00
|
|
|
// check for errors
|
|
|
|
if err != nil || config == nil {
|
|
|
|
log.Fatalf("Failed to load config: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-10-05 01:10:35 +00:00
|
|
|
fmt.Println(config.DatabasePath)
|
|
|
|
fmt.Println(config.ImageStorageDir)
|
|
|
|
fmt.Println(config.JWTSecret)
|
|
|
|
fmt.Println(config.LogFile)
|
|
|
|
fmt.Println(config.ListeningPort)
|
|
|
|
|
2024-10-05 03:05:30 +00:00
|
|
|
// Connect to the database
|
2024-10-05 01:10:35 +00:00
|
|
|
db, err = ConnectDB(config.DatabasePath)
|
|
|
|
if err != nil || db == nil {
|
|
|
|
log.Fatalf("Failed to connect to database: %v", err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
fmt.Println("Default user 'boxuser' created successfully!")
|
|
|
|
|
|
|
|
// Create the router
|
2024-10-21 16:06:32 +00:00
|
|
|
baseRouter := mux.NewRouter()
|
|
|
|
|
|
|
|
router := baseRouter.PathPrefix("/api/v1").Subrouter()
|
2024-10-05 01:10:35 +00:00
|
|
|
|
2024-10-05 03:05:30 +00:00
|
|
|
// Define your routes
|
2024-10-05 03:03:50 +00:00
|
|
|
router.Handle("/login", http.HandlerFunc(LoginHandler)).Methods("POST", "OPTIONS")
|
|
|
|
router.Handle("/boxes", AuthMiddleware(http.HandlerFunc(GetBoxesHandler))).Methods("GET", "OPTIONS")
|
|
|
|
router.Handle("/boxes", AuthMiddleware(http.HandlerFunc(CreateBoxHandler))).Methods("POST", "OPTIONS")
|
|
|
|
router.Handle("/boxes/{id}", AuthMiddleware(http.HandlerFunc(DeleteBoxHandler))).Methods("DELETE", "OPTIONS")
|
2024-10-14 13:20:04 +00:00
|
|
|
router.Handle("/boxes/{id}", AuthMiddleware(http.HandlerFunc(GetBoxHandler))).Methods("GET", "OPTIONS")
|
2024-10-05 03:03:50 +00:00
|
|
|
router.Handle("/items", AuthMiddleware(http.HandlerFunc(GetItemsHandler))).Methods("GET", "OPTIONS")
|
|
|
|
router.Handle("/items", AuthMiddleware(http.HandlerFunc(CreateItemHandler))).Methods("POST", "OPTIONS")
|
|
|
|
router.Handle("/items/{id}", AuthMiddleware(http.HandlerFunc(GetItemHandler))).Methods("GET", "OPTIONS")
|
2024-10-08 22:34:49 +00:00
|
|
|
router.Handle("/boxes/{id}/items", AuthMiddleware(http.HandlerFunc(GetItemsInBoxHandler))).Methods("GET", "OPTIONS")
|
2024-10-05 03:03:50 +00:00
|
|
|
router.Handle("/items/{id}", AuthMiddleware(http.HandlerFunc(UpdateItemHandler))).Methods("PUT", "OPTIONS")
|
|
|
|
router.Handle("/items/{id}", AuthMiddleware(http.HandlerFunc(DeleteItemHandler))).Methods("DELETE", "OPTIONS")
|
2024-10-10 15:23:35 +00:00
|
|
|
router.Handle("/items/{id}/image", AuthMiddleware(http.HandlerFunc(GetItemImageHandler))).Methods("GET", "OPTIONS")
|
2024-10-12 02:15:15 +00:00
|
|
|
fmt.Println("Registering route for search items...")
|
|
|
|
router.Handle("/search/items", AuthMiddleware(http.HandlerFunc(SearchItemsHandler))).Methods("GET", "OPTIONS")
|
2024-10-06 23:02:38 +00:00
|
|
|
// Add a new route for uploading an image with AuthMiddleware
|
|
|
|
router.HandleFunc("/items/{id}/upload", UploadItemImageHandler).
|
|
|
|
Methods("POST").
|
|
|
|
Handler(AuthMiddleware(http.HandlerFunc(UploadItemImageHandler)))
|
2024-10-05 01:10:35 +00:00
|
|
|
|
2024-10-16 22:10:20 +00:00
|
|
|
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")
|
2024-10-23 22:02:19 +00:00
|
|
|
managementRouter.Handle("/imagearchive", http.HandlerFunc(GetImageArchiveHandler)).Methods("GET", "OPTIONS")
|
2024-10-16 22:10:20 +00:00
|
|
|
|
2024-10-05 03:05:30 +00:00
|
|
|
// Apply CORS middleware
|
|
|
|
c := cors.New(cors.Options{
|
2024-10-18 15:00:04 +00:00
|
|
|
AllowedOrigins: origins,
|
2024-10-05 03:05:30 +00:00
|
|
|
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
|
|
|
AllowedHeaders: []string{"Authorization", "Content-Type"},
|
2024-10-16 22:10:20 +00:00
|
|
|
ExposedHeaders: []string{"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Cache-Control", "Content-Language", "Content-Type", "Expires", "Last-Modified", "Pragma", "ETag"},
|
2024-10-05 03:05:30 +00:00
|
|
|
AllowCredentials: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Start the server with CORS middleware
|
2024-10-05 01:10:35 +00:00
|
|
|
fmt.Printf("Server listening on port %d\n", config.ListeningPort)
|
2024-10-05 03:05:30 +00:00
|
|
|
http.ListenAndServe(fmt.Sprintf(":%d", config.ListeningPort), c.Handler(router))
|
2024-10-05 01:10:35 +00:00
|
|
|
}
|