-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
46 lines (38 loc) · 874 Bytes
/
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
// main.go
package main
import (
"encoding/json"
"fmt"
"net/http"
"os/exec"
)
type Location struct {
IP string `json:"ip"`
City string `json:"city"`
Region string `json:"region"`
Country string `json:"country"`
Loc string `json:"loc"`
}
func getLocation() Location {
cmd := exec.Command("python3", "location.py")
output, err := cmd.Output()
if err != nil {
fmt.Println("Error running Python script:", err)
}
var loc Location
err = json.Unmarshal(output, &loc)
if err != nil {
fmt.Println("Error parsing JSON:", err)
}
return loc
}
func handler(w http.ResponseWriter, r *http.Request) {
loc := getLocation()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(loc)
}
func main() {
http.HandleFunc("/location", handler)
fmt.Println("Server running on port 8080...")
http.ListenAndServe(":8080", nil)
}