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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user