-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathmain.go
More file actions
222 lines (188 loc) · 4.75 KB
/
Copy pathmain.go
File metadata and controls
222 lines (188 loc) · 4.75 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"encoding/json"
"net/http"
"os"
"strings"
"github.com/gin-gonic/gin"
_ "github.com/heroku/x/hmetrics/onload"
gp "github.com/jayluxferro/ghanapostgps"
"github.com/joho/godotenv"
)
var params gp.Params
const identifier = "CenterLatitude"
const getAddressIdentifier = "PostCode"
type GetLocationRequest struct {
Address string `json:"address" form:"address"`
}
type GetAddressRequest struct {
Lat string `json:"lat" form:"lat"`
Long string `json:"long" form:"long"`
}
type DataResponse struct {
Table []Info
}
type AddressResponse struct {
Table []Address
}
type Address struct {
GPSName string
Region string
District string
PostCode string
NLat float64
Slat float64
WLong float64
Elong float64
Area string
AddressV1 string
Street string
}
type Info struct {
Area string
AddressV1 string
CenterLatitude float64
CenterLongitude float64
District string
EastLat float64
EastLong float64
GPSName string
NorthLat float64
NorthLong float64
PostCode string
Region string
SouthLat float64
SouthLong float64
Street string
WestLat float64
WestLong float64
}
func unAuthorized(c *gin.Context) {
c.JSON(http.StatusForbidden, gin.H{"error": "unauthorized"})
}
func responseData(c *gin.Context, found bool, data interface{}) {
c.JSON(http.StatusOK, gin.H{
"data": data,
"found": found,
})
}
func bindInput(c *gin.Context, v interface{}) bool {
ct := c.GetHeader("Content-Type")
if strings.HasPrefix(ct, "application/json") {
if err := c.ShouldBindJSON(v); err != nil {
return false
}
} else {
if err := c.ShouldBind(v); err != nil {
return false
}
}
return true
}
func getAPIKeysHandler(c *gin.Context) {
defaults := params
defaults.ApiURL = gp.BaseAPIURL
c.JSON(http.StatusOK, gin.H{"data": gp.GetAPIKeys(&defaults)})
}
func getAddressHandler(c *gin.Context) {
var dataResponse AddressResponse
var req GetAddressRequest
if !bindInput(c, &req) {
unAuthorized(c)
return
}
if !(len(req.Lat) > 0 && len(req.Long) > 0) {
unAuthorized(c)
return
}
response := gp.GetAddress(req.Lat, req.Long, ¶ms)
if !strings.Contains(response, getAddressIdentifier) {
responseData(c, false, dataResponse)
return
}
response = convertToJSON(response)
json.Unmarshal([]byte(response), &dataResponse)
responseData(c, true, dataResponse)
}
func getLocationHandler(c *gin.Context) {
var dataResponse DataResponse
var req GetLocationRequest
if !bindInput(c, &req) {
unAuthorized(c)
return
}
isValid, address := gp.IsValidGPAddress(req.Address)
if !isValid {
unAuthorized(c)
return
}
response := gp.GetLocation(address, ¶ms)
if !strings.Contains(response, identifier) {
responseData(c, false, dataResponse)
return
}
response = convertToJSON(response)
json.Unmarshal([]byte(response), &dataResponse)
responseData(c, true, dataResponse)
}
func convertToJSON(data string) string {
in := []byte(data)
var raw map[string]interface{}
if err := json.Unmarshal(in, &raw); err != nil {
panic(err)
}
out, err := json.Marshal(raw)
if err != nil {
panic(err)
}
return string(out)
}
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(http.StatusNoContent)
return
}
}
}
func main() {
_ = godotenv.Load(".env")
prefix := "GPGPS_"
params = gp.Params{}
params.ApiURL = os.Getenv(prefix + "apiURL")
params.Authorization = os.Getenv(prefix + "authorization")
params.AsaaseUser = os.Getenv(prefix + "asaaseUser")
params.LanguageCode = os.Getenv(prefix + "languageCode")
params.Language = os.Getenv(prefix + "language")
params.DeviceId = os.Getenv(prefix + "deviceId")
params.AndroidCert = os.Getenv(prefix + "androidCert")
params.AndroidPackage = os.Getenv(prefix + "androidPackage")
params.CountryName = os.Getenv(prefix + "countryName")
params.Country = os.Getenv(prefix + "country")
port := os.Getenv("PORT")
if port == "" {
port = "9091"
}
gin.SetMode(gin.ReleaseMode)
router := gin.New()
router.Use(CORSMiddleware())
router.Use(gin.Logger())
router.LoadHTMLGlob("templates/*.tmpl.html")
router.Static("/static", "static")
var production = false
if os.Getenv("MODE") == "prod" {
production = true
}
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl.html", gin.H{
"production": production,
})
})
router.POST("/", getLocationHandler)
router.POST("/get-location", getLocationHandler)
router.POST("/get-address", getAddressHandler)
router.POST("/api-keys", getAPIKeysHandler)
router.Run(":" + port)
}