update
This commit is contained in:
24
main.go
24
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user