Browse Source

create page for fetching location shared.

master
Craig Stewart 3 months ago
parent
commit
01b5575b78
3 changed files with 73 additions and 9 deletions
  1. +37
    -0
      getlocation.html
  2. +27
    -8
      location-finder.go
  3. +9
    -1
      setlocation.html

+ 37
- 0
getlocation.html View File

@@ -0,0 +1,37 @@
<!DOCTYPE html>
<html>
<head>
<title>Location</title>
<script>
function getLocation() {
var post = new XMLHttpRequest();
post.open("GET", "/api/location?id={{.Id}}&key={{.Key}}", true);
post.send();
post.onload = function () {
switch ( this.status ) {
case 404:
document.getElementById("shared").innerHTML = "This Location Id does not appear to exist";
break;
case 204:
document.getElementById("shared").innerHTML = "The Location is currently empty, retrying";
getLocation();
break;
case 200:
document.getElementById("shared").innerHTML = "Location Retrived";
var location = JSON.parse(this.response);
document.getElementById("location").innerHTML = "Latitude: " + location.latitude + ", Longitude: " + location.longitude;
break;
default:
document.getElementById("shared").innerHTML = "There was a problem";
}
};
// document.getElementById("location").innerHTML = "Latitude: " + location.coords.latitude + ", Longitude: " + location.coords.longitude;
}
window.onload = getLocation;
</script>
</head>
<body>
<p id="shared"></p>
<p id="location"></p>
</body>
</html>

+ 27
- 8
location-finder.go View File

@@ -19,12 +19,30 @@ type location_set struct {
Key string `json:"key"`
}

func locate(w http.ResponseWriter, r *http.Request){
t, err := template.ParseFiles("location.html")
type item_key struct {
Id string
Key string
}

func setlocation(w http.ResponseWriter, r *http.Request){
item := item_key{r.FormValue("id"), r.FormValue("key")}
t, err := template.ParseFiles("setlocation.html")
if err != nil {
log.Print("template parsing error: ", err)
}
err = t.Execute(w, item)
if err != nil {
log.Print("template executing error: ", err)
}
}

func getlocation(w http.ResponseWriter, r *http.Request){
item := item_key{r.FormValue("id"), r.FormValue("key")}
t, err := template.ParseFiles("getlocation.html")
if err != nil {
log.Print("template parsing error: ", err)
}
err = t.Execute(w, nil)
err = t.Execute(w, item)
if err != nil {
log.Print("template executing error: ", err)
}
@@ -43,14 +61,14 @@ func api_location(w http.ResponseWriter, r *http.Request){
}
latitude, latitude_err := read_key("latitude-" + id)
if latitude_err != true {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(http.StatusText(http.StatusNotFound) + "\n"))
w.WriteHeader(http.StatusNoContent)
w.Write([]byte(http.StatusText(http.StatusNoContent) + "\n"))
return
}
longitude, longitude_err := read_key("longitude-" + id)
if longitude_err != true {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(http.StatusText(http.StatusNotFound) + "\n"))
w.WriteHeader(http.StatusNoContent)
w.Write([]byte(http.StatusText(http.StatusNoContent) + "\n"))
return
}
output, marshal_err := json.Marshal(map[string]float64{"latitude": latitude, "longitude": longitude})
@@ -150,7 +168,8 @@ func read_key(id string) (float64, bool) {
}

func main() {
http.HandleFunc("/", locate)
http.HandleFunc("/setlocation.html", setlocation)
http.HandleFunc("/getlocation.html", getlocation)
http.HandleFunc("/api/location", api_location)
http.ListenAndServe("127.0.0.1:8080", nil)
}

location.html → setlocation.html View File

@@ -13,13 +13,21 @@ function getLocation() {
function showLocation(location) {
var post = new XMLHttpRequest();
post.open("POST", "/api/location", true);
post.send("{\"latitude\": " + location.coords.latitude + ", \"longitude\": " + location.coords.longitude + ", \"id\": \"test\", \"key\": \"test\"}");
post.send("{\"latitude\": " + location.coords.latitude + ", \"longitude\": " + location.coords.longitude + ", \"id\": \"{{.Id}}\", \"key\": \"{{.Key}}\"}");
post.onload = function () {
if ( this.status == 201 ) {
document.getElementById("shared").innerHTML = "Your Location Has Been Shared";
} else {
document.getElementById("shared").innerHTML = "There was a probelem sharing your location, try refreshing the page"
}
};
document.getElementById("location").innerHTML = "Latitude: " + location.coords.latitude + ", Longitude: " + location.coords.longitude;
}
window.onload = getLocation;
</script>
</head>
<body>
<p id="shared"></p>
<p id="location"></p>
</body>
</html>

Loading…
Cancel
Save