-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
64 lines (50 loc) · 1.1 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
64
package main
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"gopkg.in/yaml.v2"
"os"
"time"
"web-app-go/utils"
)
func ConnectDatabase() (*mongo.Database, func()) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://127.0.0.1:27017"))
if err != nil {
panic(err)
}
var disconnect = func() {
if err := client.Disconnect(ctx); err != nil {
panic(err)
}
}
db := client.Database("notesappgo")
fmt.Println("Connected to db!")
return db, disconnect
}
func loadConfig() (cfg utils.Config) {
f, err := os.Open("config.yaml")
if err != nil {
panic(err)
}
defer f.Close()
decoder := yaml.NewDecoder(f)
err = decoder.Decode(&cfg)
if err != nil {
panic(err)
}
return
}
func main() {
router := gin.Default()
db, disconnect := ConnectDatabase()
defer disconnect()
routerGroup := router.Group("/api/v1/")
cfg := loadConfig()
SetRouters(routerGroup, db, cfg)
router.Run("127.0.0.1:8080")
}