-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (48 loc) · 1.22 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 (
"encoding/json"
"fmt"
"io/ioutil"
"github.com/gin-gonic/gin"
"github.com/globalsign/mgo"
"github.com/nahidhasan98/todo/auth"
"github.com/nahidhasan98/todo/config"
"github.com/nahidhasan98/todo/todo"
"github.com/nahidhasan98/todo/user"
)
func initializeAllServices(router *gin.RouterGroup, dbSession *mgo.Session) {
authService := auth.Init(router, dbSession)
user.Init(router, dbSession, authService)
todo.Init(router, dbSession, authService)
}
func main() {
router := gin.Default()
v1 := router.Group("/api")
session, err := mgo.Dial(config.DbConnectionString)
if err != nil {
fmt.Println("Database connection failed!")
return
}
// data seeding from JSON file
err = seedFromJSONFile(session)
if err != nil {
fmt.Println(err)
}
fmt.Println("Server running on port 8080...")
initializeAllServices(v1, session)
router.Run(":8080")
}
func seedFromJSONFile(session *mgo.Session) error {
data, err := ioutil.ReadFile("static/data/user.json")
if err != nil {
fmt.Println(err)
}
var dataArray user.User
err = json.Unmarshal(data, &dataArray)
if err != nil {
fmt.Println(err)
}
coll := session.DB(config.DBName).C(config.UserTable)
err = coll.Insert(&dataArray)
return err
}