-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
121 lines (105 loc) · 2.66 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
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/barsuk/sxgeo"
"github.com/gin-gonic/gin"
"log"
"net/http"
"os"
"os/signal"
"syscall"
)
func main() {
var gracefulStop = make(chan os.Signal)
signal.Notify(gracefulStop, syscall.SIGTERM)
signal.Notify(gracefulStop, syscall.SIGINT)
go func() {
sig := <-gracefulStop
fmt.Printf("Caught sig: %+v\n", sig)
fmt.Println("I am that force which ever brings you good though wants be evil")
os.Exit(0)
}()
var ip string
var endian bool
var setEndian int
var dbPath string
flag.StringVar(&ip, "ip", "", "ip address to convert")
flag.IntVar(&setEndian, "se", 0, "set endianness")
flag.BoolVar(&endian, "e", false, "check endianness of your system")
flag.StringVar(&dbPath, "d", "./SxGeoCity.dat", "path to SxGeoCity.dat file")
flag.Parse()
if endian {
sxgeo.DetectEndian()
os.Exit(0)
}
if setEndian > 0 {
sxgeo.SetEndian(sxgeo.BIG)
fmt.Printf("host binary endian set to %s\n", sxgeo.Endian())
}
if _, err := sxgeo.ReadDBToMemory(dbPath); err != nil {
log.Fatalf("error: cannot read database file: %v", err)
}
if len(ip) > 0 {
city, err := sxgeo.GetCityFull(ip)
if err != nil {
fmt.Printf("error: %v", err)
os.Exit(1)
}
enc, err := json.Marshal(city)
if err != nil {
fmt.Printf("error: %v", err)
os.Exit(1)
}
fmt.Printf("%s\n", enc)
os.Exit(0)
}
r := gin.New()
r.Use(setHeaders)
r.OPTIONS("/", optionsHandler)
r.GET("/", sxgeoHandler)
erro := r.Run(fmt.Sprintf(":%d", 8080))
if erro != nil {
log.Fatal("is there any angel? Gin don't like them...")
}
}
func sxgeoHandler(c *gin.Context) {
ip := c.Query("ip")
if len(ip) < 4 {
c.IndentedJSON(http.StatusBadRequest, gin.H{"error": "give me an IP, please"})
return
}
fmt.Printf("IP: %s\n", ip)
city, err := sxgeo.GetCityFull(ip)
if err != nil {
c.IndentedJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.IndentedJSON(http.StatusAccepted, city)
}
func optionsHandler(c *gin.Context) {
c.JSON(http.StatusAccepted, "")
}
func setHeaders(c *gin.Context) {
c.Header("Content-Type", "application/json; charset=utf-8")
c.Header("Access-Control-Allow-Origin", setAccessHost(c.Request.Header.Get("Origin")))
c.Header("Access-Control-Allow-Credentials", "true")
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
c.Header("Access-Control-Max-Age", "600")
c.Header("Access-Control-Allow-Headers", "origin, content-type")
c.Header("Connection", "keep-alive")
}
// CORS hosts
func setAccessHost(origin string) string {
for _, v := range accessHosts {
if v == "origin" {
return origin
}
if v == origin {
return v
}
}
return ""
}
var accessHosts = []string{"origin",}