-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
152 lines (127 loc) · 4.06 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"strings"
"github.com/GeertJohan/go.rice"
"github.com/gorilla/mux"
)
var bookingCounter = 0
type Booking struct {
ID int `json:"id"`
StationAddress string `json:"station"`
}
// Structs for json communication.
type NewBooking struct {
DroneAddress string `json:"droneID"`
StationAddress string `json:"stationID"`
}
type DroneStatus struct {
Status string `json:"status"`
Station string `json:"station"`
}
var bookingsMap = make(map[string][]Booking)
var bookings []Booking
var drones []DroneStatus
type handler func(w http.ResponseWriter, r *http.Request)
func basicAuth(pass handler) handler {
return func(w http.ResponseWriter, r *http.Request) {
auth := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
if len(auth) != 2 || auth[0] != "Basic" {
http.Error(w, "authorization failed", http.StatusUnauthorized)
return
}
payload, _ := base64.StdEncoding.DecodeString(auth[1])
pair := strings.SplitN(string(payload), ":", 2)
if len(pair) != 2 || !validate(pair[0], pair[1]) {
http.Error(w, "authorization failed", http.StatusUnauthorized)
return
}
pass(w, r)
}
}
func validate(username, password string) bool {
if username == "test" && password == "test" {
return true
}
return false
}
// POST <- api/book/new {DroneAddress: WalletAddress, StationAddress: WalletAddress}
// GET -> api/drone/bookings/:id [] ? [{id: booking_id, station: station_address}]
// POST <- api/drone/status/:id {ConnectedStation: address ? '', Idle: Boolean}
func handlerNewBooking(w http.ResponseWriter, r *http.Request) {
fmt.Println("NEW BOOKING")
decoder := json.NewDecoder(r.Body)
var b NewBooking
err := decoder.Decode(&b)
if err != nil {
panic(err)
}
booking := Booking{ID: bookingCounter, StationAddress: b.StationAddress}
// Add booking to store
bookings = append(bookings, booking)
bookingsMap[b.DroneAddress] = append(bookingsMap[b.DroneAddress], booking)
bookingCounter++
defer r.Body.Close()
log.Println("Testing booking for drone:" + b.DroneAddress + " station: " + b.StationAddress)
}
func handlerDroneStatus(w http.ResponseWriter, r *http.Request) {
fmt.Println("Called drone status")
params := mux.Vars(r)
id := params["id"]
decoder := json.NewDecoder(r.Body)
var d DroneStatus
err := decoder.Decode(&d)
if err != nil {
panic(err)
}
fmt.Println("Parsed json")
// w.WriteHeader(http.StatusOK)
//drones = append(drones, d)
defer r.Body.Close()
log.Println("Storing status for drone " + id + " station " + d.Station + " status" + d.Status)
}
func handlerGetBookings(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
arr, found := bookingsMap[params["id"]]
fmt.Println(params["id"])
if found == false {
fmt.Fprintf(w, "[]")
return
}
json, err := json.Marshal(arr)
if err != nil {
panic(err)
}
//w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
w.Write(json)
}
func handleRoot(w http.ResponseWriter, r *http.Request) {
fmt.Println("Radnom request: %s", r.URL.Path)
//w http.ResponseWriter, r *http.Request
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}
func apiHandler(w http.ResponseWriter, r *http.Request) {
id := strings.TrimPrefix(r.URL.Path, "/api/")
fmt.Fprintf(w, "Hi there, I love %s!, %s", r.URL.Path[1:], id)
}
func main() {
r := mux.NewRouter()
port := flag.String("p", "8100", "port to serve on")
directory := flag.String("d", "./web/dist", "the directory of static file to host")
flag.Parse()
fmt.Println("Hello from DroneChain server")
r.HandleFunc("/api/drone/status/{id}", handlerDroneStatus).Methods("POST")
r.HandleFunc("/api/booking/new", handlerNewBooking) // .Methods("POST")
r.HandleFunc("/api/drone/bookings/{id}", handlerGetBookings) // .Methods("GET")
r.HandleFunc("/api/status/{id}", handlerDroneStatus).Methods("POST")
r.PathPrefix("/").Handler(http.FileServer(rice.MustFindBox("web/dist").HTTPBox()))
http.Handle("/", r)
log.Printf("Serving %s on HTTP port: %s\n", *directory, *port)
log.Fatal(http.ListenAndServe(":"+*port, nil))
}