-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (48 loc) · 1.44 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
package main
import (
"context"
"fmt"
"log"
"github.com/gofiber/contrib/websocket"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/greyhands2/searchly/config"
"github.com/greyhands2/searchly/controllers"
router "github.com/greyhands2/searchly/router"
)
func main() {
es, err := config.ElasticSearchConnect()
if err != nil {
log.Fatalf("Error creating Elasticsearch Client %s", err)
}
app := fiber.New()
//fiber middleware to set the es connection to context that is compatible with fiber
app.Use(func(c *fiber.Ctx) error {
//add the es connection to app context
ctx := context.WithValue(c.Context(), "esConnection", es)
c.Locals("ctx", ctx)
return c.Next()
})
app.Use(logger.New())
//handle panics if any
app.Use(recover.New())
app.Use("/socket", func(c *fiber.Ctx) error {
// IsWebSocketUpgrade returns true if the client
// requested upgrade to the WebSocket protocol.
var upgrade bool = websocket.IsWebSocketUpgrade(c)
fmt.Println(upgrade)
if upgrade {
fmt.Println("upgraded")
return c.Next()
}
return fiber.ErrUpgradeRequired
})
//
app.Get("/socket", websocket.New(controllers.SearchSocketServer))
//let us create an app group
api := app.Group("/api")
router.HandleDocumentRoutes(api.Group("/document")) //used an alias named router on the import
//listen to server on port 3000
log.Fatal(app.Listen(":3000"))
}