-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (34 loc) · 1.32 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"database/sql"
"fmt"
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/inaciogu/go-notifications/internal/application/usecase"
"github.com/inaciogu/go-notifications/internal/infra/messaging/consumer"
"github.com/inaciogu/go-notifications/internal/infra/repository/postgres"
"github.com/inaciogu/go-notifications/internal/infra/web/handlers"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
)
func main() {
godotenv.Load(".env")
db, err := sql.Open("postgres", "postgres://root:password@localhost:5432/notifications?sslmode=disable")
if err != nil {
panic(err)
}
defer db.Close()
repo := postgres.NewPostgresNotificationRepository(db)
createNotification := usecase.NewCreateNotification(repo)
listRecipientNotifications := usecase.NewListRecipientNotifications(repo)
notificationHandler := handlers.NewNotificationHandler(createNotification, listRecipientNotifications)
notificationConsumer := consumer.NewNotificationConsumer(createNotification)
go notificationConsumer.Start()
router := chi.NewRouter()
router.Use(middleware.Logger)
router.Post("/notifications", notificationHandler.Create)
router.Get("/notifications/from/{id}", notificationHandler.ListManyByRecipient)
fmt.Println("Server running on port 3001")
http.ListenAndServe(":3001", router)
}