/
main.go
45 lines (36 loc) · 970 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
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/hculpan/railtrack/handlers"
"github.com/joho/godotenv"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/gin-gonic/gin"
)
func main() {
if err := godotenv.Load(); err != nil {
log.Fatal("No .env file found")
}
uri := os.Getenv("MONGODB_URI")
if uri == "" {
log.Fatal("You must set your 'MONGODB_URI' environmental variable. See\n\t https://www.mongodb.com/docs/drivers/go/current/usage-examples/#environment-variable")
}
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
if err != nil {
panic(err)
}
defer func() {
if err := client.Disconnect(context.TODO()); err != nil {
panic(err)
}
}()
fmt.Println("Connected to DB")
r := gin.Default()
r.LoadHTMLGlob("templates/*")
r.GET("/", handlers.IndexHandler)
r.GET("/api/about", handlers.AboutHandler)
r.Run() // listen and serve on 0.0.0.0:8080
}