-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
98 lines (80 loc) · 1.82 KB
/
server.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func main() {
// Echo instance
e := echo.New()
// CORS
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowMethods: []string{echo.GET},
}))
// Middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
// Routes
e.GET("/", getLocation)
// Start server
e.Logger.Fatal(e.Start(":8080"))
}
// Structs
type location struct {
Country string `json:"country"`
Region string `json:"region"`
City string `json:"city"`
Lat float64 `json:"lat"`
Long float64 `json:"lng"`
PostalCode string `json:"postalCode"`
Timezone string `json:"timezone"`
GeonameID int `json:"geonameId"`
}
type as struct {
ASN int `json:"asn"`
Name string `json:"name"`
Route string `json:"route"`
Domain string `json:"domain"`
Type string `json:"type"`
}
type proxy struct {
Proxy bool `json:"proxy"`
VPN bool `json:"vpn"`
Tor bool `json:"tor"`
}
type geo struct {
IP string `json:"ip"`
Location location `json:"location"`
Domains []string `json:"domains"`
As as `json:"as"`
ISP string `json:"isp"`
Proxy proxy `json:"proxy"`
}
// Handler
func getLocation(c echo.Context) error {
apiKey := os.Getenv("API_KEY")
url := fmt.Sprintf("https://geo.ipify.org/api/v1?apiKey=%s", apiKey)
// Request
res, getErr := http.Get(url)
if getErr != nil {
log.Fatal(getErr)
}
defer res.Body.Close()
body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
log.Fatal(readErr)
}
// Fill the data with the body from the JSON
var g geo
jsonErr := json.Unmarshal(body, &g)
if jsonErr != nil {
log.Fatal(jsonErr)
}
return c.JSON(http.StatusOK, g)
}