-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.go
62 lines (58 loc) · 1.62 KB
/
api.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
package api
// Copyright 2016, JuanDeFu.ca. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
import (
"encoding/json"
"fmt"
"net/http"
"sabey.co/spoofgo/movement"
)
type API struct {
Angle int `json:"angle"`
Mode int `json:"mode"`
ModeString string `json:"mode-string,omitempty"`
Modifier int `json:"modifier"`
Speed float64 `json:"speed"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
Accelerating bool `json:"accelerating"`
Error string `json:"error,omitempty"`
}
func (self *Server) GetAPI() *API {
movement.Calculate()
x := &API{
Angle: movement.GetAngle(),
Speed: movement.GetSpeed(),
Accelerating: movement.IsAccelerating(),
}
x.Mode, x.Modifier = movement.GetModeModifier()
x.Latitude, x.Longitude = movement.GetLatLong()
x.ModeString = movement.PrintMode(x.Mode, true)
return x
}
func (self *Server) APIRespond(
api *API,
w http.ResponseWriter,
r *http.Request,
code int,
) {
if r.URL != nil && r.URL.Query().Get("controller") == "index" {
// client is using the controller
if r.URL.Query().Get("reload") == "no" {
w.Header().Set("Location", fmt.Sprintf("/controller?reload=no&%s", no_cache_query()))
} else {
w.Header().Set("Location", fmt.Sprintf("/controller?%s", no_cache_query()))
}
self.Headers(w, CONTENT_JSON, 301)
} else {
if code == 0 {
code = 200
}
self.Headers(w, CONTENT_JSON, code)
}
if api == nil {
api = self.GetAPI()
}
bs, _ := json.MarshalIndent(api, "", "\t")
fmt.Fprintf(w, "%s", bs)
}