/
main.go
55 lines (44 loc) · 1.07 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/ernesto27/docs/db"
"github.com/ernesto27/docs/routers"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
myDb, errNew := db.New(os.Getenv("DATABASE_USER"), os.Getenv("DATABASE_PASSWORD"),
os.Getenv("DATABASE_HOST"), os.Getenv("DATABASE_PORT"), os.Getenv("DATABASE_NAME"), os.Getenv("DATABASE_TLS"))
if errNew != nil {
log.Fatal(errNew)
}
r := gin.Default()
r.LoadHTMLGlob("templates/*")
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", nil)
})
r.POST("/docs/create", func(c *gin.Context) {
routers.CreateDoc(&myDb, c)
})
r.GET("/docs/:id", func(c *gin.Context) {
id := c.Param("id")
fmt.Println(id)
c.HTML(http.StatusOK, "doc.tmpl", gin.H{
"id": id,
})
})
r.GET("/ws", func(c *gin.Context) {
routers.WebsocketHandler(c.Writer, c.Request, c, &myDb)
})
go routers.BroadcastDocByID()
go routers.BroadcastUsersConnected()
go routers.BroadcastError()
r.Run()
}