-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (46 loc) · 1.16 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
package main
import (
"fmt"
"go-fiber-todos/database"
"go-fiber-todos/todos"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func setupV1(app *fiber.App) {
v1 := app.Group("/v1")
setupTodosRoutes(v1)
}
func setupTodosRoutes(grp fiber.Router) {
todosRoutes := grp.Group("/todos")
todosRoutes.Get("/", todos.GetTodos)
todosRoutes.Get("/:id", todos.GetTodo)
todosRoutes.Post("/", todos.CreateTodo)
todosRoutes.Delete("/:id", todos.DeleteTodo)
todosRoutes.Patch("/:id", todos.UpdateTodo)
}
func initDatabase() {
var err error
database.DBConn, err = gorm.Open(sqlite.Open("todos.db"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
fmt.Println("Database successfully connected")
database.DBConn.AutoMigrate(&todos.Todo{})
fmt.Println("Database Migrated")
}
func main() {
app := fiber.New()
initDatabase()
// defer database.DBConn.
app.Use(logger.New(logger.Config{
Format: "[${ip}]:${port} ${status} - ${method} ${path}\n",
}))
setupV1(app)
// Listen on PORT 3000
err := app.Listen(":34000")
if err != nil {
panic(err)
}
}