boxes-api/main.go

78 lines
2.8 KiB
Go
Raw Normal View History

2024-10-05 01:10:35 +00:00
package main
import (
"fmt"
"log"
"net/http"
"os"
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() {
configFile := os.Getenv("CONFIG")
2024-10-05 01:10:35 +00:00
var err error
config, err = LoadConfig(configFile)
// 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)
fmt.Println("DB Connection String:", db.DB().Ping())
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
router := mux.NewRouter()
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")
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")
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")
// 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-05 03:05:30 +00:00
// Apply CORS middleware
c := cors.New(cors.Options{
AllowedOrigins: []string{"http://localhost:3000"}, // Change this to your frontend domain
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Authorization", "Content-Type"},
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
}