diff --git a/main.go b/main.go index 3cbef61..fbf90bd 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,22 @@ import ( "github.com/joho/godotenv" ) +func corsMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Access-Control-Allow-Origin", "*") // Ganti * dengan origin spesifik jika perlu + w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") + w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization") + + // Untuk preflight request + if r.Method == "OPTIONS" { + w.WriteHeader(http.StatusOK) + return + } + + next.ServeHTTP(w, r) + }) +} + func main() { log.Println("Email Notification Service") err := godotenv.Load() @@ -20,9 +36,13 @@ func main() { db := config.NewDatabase().WithContext(context.Background()) - route.Route(db) + mux := http.NewServeMux() + + handlerWithCORS := corsMiddleware(mux) + + route.Route(db, mux) port := os.Getenv("PORT") - http.ListenAndServe(":"+port, nil) + http.ListenAndServe(":"+port, handlerWithCORS) } diff --git a/route/route.go b/route/route.go index 791ce64..6bc5c0e 100644 --- a/route/route.go +++ b/route/route.go @@ -15,8 +15,8 @@ import ( "gorm.io/gorm" ) -func Route(db *gorm.DB) { - http.HandleFunc("/birthday-notification", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) { +func Route(db *gorm.DB, mux *http.ServeMux) { + mux.HandleFunc("/birthday-notification", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return @@ -68,7 +68,7 @@ func Route(db *gorm.DB) { w.Write([]byte("success")) })) - http.HandleFunc("/workanniversary-notification", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) { + mux.HandleFunc("/workanniversary-notification", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return @@ -125,7 +125,7 @@ func Route(db *gorm.DB) { w.Write([]byte("success")) })) - http.HandleFunc("/server-down-notification", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) { + mux.HandleFunc("/server-down-notification", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return @@ -162,7 +162,7 @@ func Route(db *gorm.DB) { w.Write([]byte("success")) })) - http.HandleFunc("/high-memory-notification", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) { + mux.HandleFunc("/high-memory-notification", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return @@ -199,7 +199,7 @@ func Route(db *gorm.DB) { w.Write([]byte("success")) })) - http.HandleFunc("/send-email", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) { + mux.HandleFunc("/send-email", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return @@ -257,7 +257,7 @@ func Route(db *gorm.DB) { w.Write([]byte("success")) })) - http.Handle("/", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) { + mux.Handle("/", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return