-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (48 loc) · 1.19 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
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"fmt"
"guestbook-example/internal/api"
"guestbook-example/internal/api/handler"
"guestbook-example/internal/domain"
"guestbook-example/internal/infra/repository"
"guestbook-example/internal/service"
"log/slog"
"os"
"gorm.io/gorm"
"github.com/glebarez/sqlite"
)
// GORM with glebarez/sqlite
func initDB() (*gorm.DB, error) {
db, err := gorm.Open(sqlite.Open("sqlite.db"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
// Migrate the schema
db.AutoMigrate(&domain.Message{})
return db, err
}
func migrateDB(db *gorm.DB) error {
err := db.AutoMigrate(&domain.Message{})
if err != nil {
return fmt.Errorf("failed to migrate database: %w", err)
}
return nil
}
func main() {
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
db, err := initDB()
if err != nil {
// TODO: handle error
panic(err)
}
err = migrateDB(db)
if err != nil {
// TODO: handle error
panic(err)
}
messageRepo := repository.NewMessageRepo(logger, db)
messageService := service.NewMessageService(logger, messageRepo)
messageHandler := handler.NewMessageHandler(logger, messageService)
router := api.SetupRouter(messageHandler)
router.Run(":8080")
}