-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.go
85 lines (71 loc) · 1.96 KB
/
app.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
79
80
81
82
83
84
85
package main
import (
"fmt"
"log"
"os"
"strconv"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/compress"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/etag"
"github.com/gofiber/fiber/v2/middleware/favicon"
"github.com/gofiber/fiber/v2/middleware/limiter"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/recover"
database "github.com/jonhealy1/goapi-stac/database"
router "github.com/jonhealy1/goapi-stac/router"
)
func main() {
app := Setup()
value, exists := os.LookupEnv("API_PORT")
api_port := 6002
if exists {
api_port, _ = strconv.Atoi(value)
}
// Listen on api port
log.Fatal(app.Listen(fmt.Sprintf(":%d", api_port)))
}
func Setup() *fiber.App {
// connect to database: postgres and elastic search
database.ConnectDb()
database.ConnectES()
// create new fiber app
app := fiber.New()
// register middleware
app.Use(cors.New())
app.Use(compress.New())
//app.Use(cache.New())
app.Use(etag.New())
app.Use(favicon.New())
app.Use(limiter.New(limiter.Config{
Max: 1000,
LimitReached: func(c *fiber.Ctx) error {
return c.Status(fiber.StatusTooManyRequests).JSON(&fiber.Map{
"status": "fail",
"message": "You have requested too many in a single time-frame! Please wait another minute!",
})
},
}))
app.Use(logger.New())
app.Use(recover.New())
// app.Use(cache.New(cache.Config{
// Next: func(c *fiber.Ctx) bool {
// return c.Query("refresh") == "true"
// },
// Expiration: 30 * time.Minute,
// CacheControl: true,
// }))
router.CollectionRoute(app)
router.ESCollectionRoute(app)
router.ItemRoute(app)
router.ESItemRoute(app)
router.SearchRoute(app)
app.All("*", func(c *fiber.Ctx) error {
errorMessage := fmt.Sprintf("Route '%s' does not exist in this API!", c.OriginalURL())
return c.Status(fiber.StatusNotFound).JSON(&fiber.Map{
"status": "fail",
"message": errorMessage,
})
})
return app
}