package main import ( "fmt" "log" "net/http" "github.com/gorilla/mux" "github.com/jinzhu/gorm" ) var ( db *gorm.DB // Declare db globally config *Config ) func main() { var err error config, err = LoadConfig("config.yaml") fmt.Println(config.DatabasePath) fmt.Println(config.ImageStorageDir) fmt.Println(config.JWTSecret) fmt.Println(config.LogFile) fmt.Println(config.ListeningPort) if err != nil || config == nil { log.Fatalf("Failed to load config: %v", err) } // Conne:ct to the database 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() // Apply JWT authentication middleware to protected endpoints router.Handle("/login", http.HandlerFunc(LoginHandler)).Methods("POST") router.Handle("/boxes", AuthMiddleware(http.HandlerFunc(GetBoxesHandler))).Methods("GET") router.Handle("/boxes", AuthMiddleware(http.HandlerFunc(CreateBoxHandler))).Methods("POST") router.Handle("/boxes/{id}", AuthMiddleware(http.HandlerFunc(DeleteBoxHandler))).Methods("DELETE") router.Handle("/items", AuthMiddleware(http.HandlerFunc(GetItemsHandler))).Methods("GET") router.Handle("/items", AuthMiddleware(http.HandlerFunc(CreateItemHandler))).Methods("POST") router.Handle("/items/{id}", AuthMiddleware(http.HandlerFunc(GetItemHandler))).Methods("GET") router.Handle("/items/{id}/items", AuthMiddleware(http.HandlerFunc(GetItemsInBoxHandler))).Methods("GET") router.Handle("/items/{id}", AuthMiddleware(http.HandlerFunc(UpdateItemHandler))).Methods("PUT") router.Handle("/items/{id}", AuthMiddleware(http.HandlerFunc(DeleteItemHandler))).Methods("DELETE") // Start the server fmt.Printf("Server listening on port %d\n", config.ListeningPort) http.ListenAndServe(fmt.Sprintf(":%d", config.ListeningPort), router) }