-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
54 lines (44 loc) · 1.2 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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)
// Functionality struct, defining device functionality
type Functionality struct {
Description string `json:"desc"`
Path string `json:"path"`
}
// Functionalities is a Functionality array
type Functionalities []Functionality
// Device structure
type Device struct {
ID string `json:"id"`
Name string `json:"name"`
Funcs Functionalities `json:"funcs"`
}
// Devices is a Device array
type Devices []Device
func allDevices(w http.ResponseWriter, r *http.Request) {
devices := Devices{
Device{ID: "1", Name: "Ceiling Lightbulb", Funcs: Functionalities{
Functionality{Description: "Toggle on and off", Path: "/toggle"},
}},
}
fmt.Println("All Devices Endpoint hit!")
json.NewEncoder(w).Encode(devices)
}
func handleHome(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello world!")
}
func handleRequest() {
router := mux.NewRouter().StrictSlash(true).PathPrefix("/api").Subrouter()
router.HandleFunc("/", handleHome).Methods("GET")
router.HandleFunc("/devices", allDevices).Methods("GET")
log.Fatal(http.ListenAndServe(":8080", router))
}
func main() {
handleRequest()
}