diff --git a/go.mod b/go.mod index 2a88e85..bcfcba3 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module go-crud-postgres -go 1.20 +go 1.20.0 require ( github.com/gorilla/mux v1.8.1 diff --git a/main.go b/main.go index bf06ef1..43e3792 100644 --- a/main.go +++ b/main.go @@ -5,13 +5,18 @@ import ( "go-crud-postgres/routes" "log" "net/http" + "os" ) func main() { - r := router.Router() - // fs := http.FileServer(http.Dir("build")) - // http.Handle("/", fs) - fmt.Println("Server dijalankan pada port 8080...") + // Use the router from the imported package + r := routes.Router() - log.Fatal(http.ListenAndServe(":8080", r)) + port := os.Getenv("PORT") + if port == "" { + port = "8080" + } + + fmt.Printf("Server is running on port %s...\n", port) + log.Fatal(http.ListenAndServe(":"+port, r)) } \ No newline at end of file diff --git a/routes/routes.go b/routes/routes.go index 092d1d4..dde1d58 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -1,7 +1,7 @@ -package router +package routes import ( - "go-crud-postgres/controllers" + "go-crud-postgres/controllers" // Update the import path to match your project structure "github.com/gorilla/mux" ) @@ -10,11 +10,11 @@ func Router() *mux.Router { router := mux.NewRouter() - router.HandleFunc("/api/buku", controller.AmbilSemuaBuku).Methods("GET", "OPTIONS") + router.HandleFunc("/api/buku", controller.AmbilSemuaBuku).Methods("GET", "OPTIONS") // Update the controller reference router.HandleFunc("/api/buku/{id}", controller.AmbilBuku).Methods("GET", "OPTIONS") router.HandleFunc("/api/buku", controller.TmbhBuku).Methods("POST", "OPTIONS") router.HandleFunc("/api/buku/{id}", controller.UpdateBuku).Methods("PUT", "OPTIONS") router.HandleFunc("/api/buku/{id}", controller.HapusBuku).Methods("DELETE", "OPTIONS") return router -} \ No newline at end of file +}