-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (65 loc) · 1.88 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"context"
"fmt"
"github.com/joho/godotenv"
"github.com/julienschmidt/httprouter"
"github.com/sudoplox/mongo-CRuD-go/controllers"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
"net/http"
"os"
"os/signal"
)
func init() {
if err := godotenv.Load(); err != nil {
log.Print("No .env file found")
}
}
func main() {
// creates a new instance
r := httprouter.New()
client := getClient()
// user controller
uc := controllers.NewUserController(client)
// SIGINT -> disconnects from Mongo and exits
go func() {
sigchan := make(chan os.Signal)
signal.Notify(sigchan, os.Interrupt)
<-sigchan
log.Println("Program killed !")
if err := client.Disconnect(context.TODO()); err != nil {
panic(err)
}
fmt.Println("You successfully disconnected from MongoDB!")
os.Exit(0)
}()
r.GET("/user/:id", uc.GetUser)
r.POST("/user", uc.CreateUser)
r.DELETE("/user/:id", uc.DeleteUser)
// creates the golang server
err := http.ListenAndServe("localhost:9000", r)
if err != nil {
fmt.Println(err)
}
}
func getClient() *mongo.Client {
// example: mongodb+srv://root:[email protected]/
uri := fmt.Sprintf("mongodb+srv://%s:%s@%s", os.Getenv("DB_USER"), os.Getenv("DB_PASS"), os.Getenv("DB_CLUSTER"))
uri = uri + "?retryWrites=true&w=majority"
serverAPI := options.ServerAPI(options.ServerAPIVersion1)
opts := options.Client().ApplyURI(uri).SetServerAPIOptions(serverAPI)
// Create a new client and connect to the server
client, err := mongo.Connect(context.TODO(), opts)
if err != nil {
panic(err)
}
// Send a ping to confirm a successful connection
if err := client.Database("admin").RunCommand(context.TODO(), bson.D{{"ping", 1}}).Err(); err != nil {
panic(err)
}
fmt.Println("Pinged your deployment. You successfully connected to MongoDB!")
return client
}