-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.go
200 lines (165 loc) · 4.2 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
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
package main
import (
"encoding/json"
"log"
"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 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 getAPIKeysHandler(c *gin.Context){
// copy current params
defaults := params
// set new values
defaults.ApiURL = gp.BaseAPIURL
// return data
c.JSON(http.StatusOK, gin.H{"data": gp.GetAPIKeys(&defaults)})
}
func getAddressHandler(c *gin.Context) {
var dataResponse AddressResponse
lat := string(c.PostForm("lat"))
long := string(c.PostForm("long"))
if !(len(lat) > 0 && len(long) > 0) {
unAuthorized(c)
return
}
response := gp.GetAddress(lat, 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
isValid, address := gp.IsValidGPAddress(c.PostForm("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", "*")
}
}
func main() {
//load .env file
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Error loading .env file")
}
// inits
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 == "" {
log.Fatal("$PORT must be set")
}
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
}
// routes
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)
}