forked from y9v/cities-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoints.go
50 lines (43 loc) · 1.12 KB
/
endpoints.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
package main
import (
"github.com/boltdb/bolt"
"github.com/gin-gonic/gin"
"github.com/lebedev-yury/cities/cache"
"github.com/lebedev-yury/cities/config"
"github.com/lebedev-yury/cities/ds"
)
func MakeApplicationStatusEndpoint(db *bolt.DB) func(*gin.Context) {
return func(c *gin.Context) {
appStatus := ds.GetAppStatus(db)
c.JSON(200, appStatus)
}
}
func MakeCityEndpoint(db *bolt.DB, options *config.Options) func(*gin.Context) {
return func(c *gin.Context) {
city, err := ds.FindCity(db, c.Param("id"), true)
if err != nil {
c.JSON(500, nil)
} else if city == nil {
c.JSON(404, nil)
} else {
c.JSON(200, city.ForSerialization(c.Request.URL, options))
}
}
}
func MakeCitiesEndpoint(
db *bolt.DB, options *config.Options, cache *cache.Cache,
) func(*gin.Context) {
return func(c *gin.Context) {
query := ds.PrepareCityNameKey(c.Query("q"))
if query == "" {
c.JSON(200, nil)
} else {
cities, err := ds.CachedCitiesSearch(db, cache, options.Locales, query, 5)
if err != nil {
c.JSON(500, nil)
} else {
c.JSON(200, cities.ForSerialization(c.Request.URL, options))
}
}
}
}