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" "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() { func main() {
log.Println("Email Notification Service") log.Println("Email Notification Service")
err := godotenv.Load() err := godotenv.Load()
@@ -20,9 +36,13 @@ func main() {
db := config.NewDatabase().WithContext(context.Background()) db := config.NewDatabase().WithContext(context.Background())
route.Route(db) mux := http.NewServeMux()
handlerWithCORS := corsMiddleware(mux)
route.Route(db, mux)
port := os.Getenv("PORT") port := os.Getenv("PORT")
http.ListenAndServe(":"+port, nil) http.ListenAndServe(":"+port, handlerWithCORS)
} }

View File

@@ -15,8 +15,8 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
func Route(db *gorm.DB) { func Route(db *gorm.DB, mux *http.ServeMux) {
http.HandleFunc("/birthday-notification", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/birthday-notification", middleware.ResponseTimeMiddleware(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet { if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return return
@@ -68,7 +68,7 @@ func Route(db *gorm.DB) {
w.Write([]byte("success")) 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 { if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return return
@@ -125,7 +125,7 @@ func Route(db *gorm.DB) {
w.Write([]byte("success")) 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 { if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return return
@@ -162,7 +162,7 @@ func Route(db *gorm.DB) {
w.Write([]byte("success")) 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 { if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return return
@@ -199,7 +199,7 @@ func Route(db *gorm.DB) {
w.Write([]byte("success")) 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 { if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return return
@@ -257,7 +257,7 @@ func Route(db *gorm.DB) {
w.Write([]byte("success")) 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 { if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return return