-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
48 lines (37 loc) · 983 Bytes
/
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
package main
import (
"log"
"github.com/gofiber/fiber/v2"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/vishal1297/crm-basic/database"
"github.com/vishal1297/crm-basic/lead"
)
const HOST_PORT = ":3000"
func main() {
app := fiber.New()
// connect to DB
initDB()
setupRoutes(app)
log.Print("- Application will start at ", HOST_PORT)
// start server
log.Fatal(app.Listen(HOST_PORT))
// at last close DB connection
defer database.DBConn.Close()
}
func setupRoutes(app *fiber.App) {
app.Get("/api/v1/lead", lead.GetLeads)
app.Get("/api/v1/lead/:id", lead.GetLead)
app.Post("/api/v1/lead", lead.NewLead)
app.Delete("/api/v1/lead/:id", lead.DeleteLead)
}
func initDB() {
var err error
database.DBConn, err = gorm.Open("sqlite3", "leads.db")
if err != nil {
panic("Database connection failed")
}
log.Print("- Connection opened to database")
database.DBConn.AutoMigrate(&lead.Lead{})
log.Print("- Database Migrated")
}