This commit is contained in:
teddy
2025-04-29 10:51:39 +07:00
parent b6ed73248b
commit cce3b3e1f1
2 changed files with 29 additions and 9 deletions

24
main.go
View File

@@ -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)
}