-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
154 lines (133 loc) · 3.6 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
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
153
154
package api
import (
"fmt"
"net/http"
"github.com/smarthut/smarthut/store"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/jwtauth"
"github.com/rs/cors"
"github.com/smarthut/smarthut/conf"
)
var tokenAuth *jwtauth.JWTAuth
// API is the main REST API
type API struct {
handler http.Handler
db *store.DB
config *conf.Configuration
tokenAuth *jwtauth.JWTAuth
version string
}
// NewAPI instantiates a new REST API
func NewAPI(config *conf.Configuration, db *store.DB, version string) *API {
api := &API{
config: config,
db: db,
tokenAuth: jwtauth.New("HS256", []byte(config.JWT.Secret), nil),
version: version,
}
r := chi.NewRouter()
cors := cors.New(cors.Options{
// AllowedOrigins: []string{config.API.Host},
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link"},
AllowCredentials: true,
MaxAge: 300,
})
r.Use(middleware.RequestID)
r.Use(middleware.Recoverer)
r.Use(cors.Handler)
// Public routes
r.Group(func(r chi.Router) {
// Returns the JWT token
r.Post("/auth", api.authenticate)
// TODO: Remove API calls from public routes
// BODY: All API calls must provide JWT token
// APIv1 routes
r.Route("/api/v1", func(r chi.Router) {
// User routes
r.Route("/user", func(r chi.Router) {
r.Get("/", api.listUsers)
r.Post("/", api.createUser)
r.Route("/{login}", func(r chi.Router) {
r.Get("/", api.getUser)
r.Put("/", api.updateUser)
r.Delete("/", api.deleteUser)
})
})
// Device router
r.Route("/device", func(r chi.Router) {
r.Get("/", api.listDevices)
r.Post("/", api.createDevice)
r.Route("/{device_name}", func(r chi.Router) {
r.Use(api.deviceCtx)
r.Get("/", api.getDevice)
r.Put("/", api.updateDevice)
r.Delete("/", api.deleteDevice)
// Socket operations
r.Route("/socket", func(r chi.Router) {
r.Get("/", api.getSocket)
r.Post("/", api.setSocket)
})
})
})
})
})
// Protected routes
r.Group(func(r chi.Router) {
r.Use(jwtauth.Verifier(api.tokenAuth))
r.Use(jwtauth.Authenticator)
// This endpoint is used to test a JWT token
r.Get("/token", func(w http.ResponseWriter, r *http.Request) {
_, claims, err := jwtauth.FromContext(r.Context())
if err != nil {
handleError(err, w, r)
return
}
w.Write([]byte(fmt.Sprintf("protected area. hi %v", claims["sub"])))
})
// APIv2 routes
r.Route("/api/v2", func(r chi.Router) {
// r.Use(api.userCtx)
// User routes
r.Route("/user", func(r chi.Router) {
r.Get("/", api.listUsers)
r.Post("/", api.createUser)
r.Route("/{login}", func(r chi.Router) {
r.Get("/", api.getUser)
r.Put("/", api.updateUser)
r.Delete("/", api.deleteUser)
})
})
// Device routes
r.Route("/device", func(r chi.Router) {
r.Get("/", api.listDevices)
r.Post("/", api.createDevice)
r.Route("/{device_name}", func(r chi.Router) {
r.Use(api.deviceCtx)
r.Get("/", api.getDevice)
r.Put("/", api.updateDevice)
r.Delete("/", api.deleteDevice)
// Socket operations
r.Route("/socket", func(r chi.Router) {
r.Get("/", api.getSocket)
r.Post("/", api.setSocket)
})
})
})
// Things
r.Route("/thing", func(r chi.Router) {
r.Get("/", api.listThings)
// ...
})
})
})
api.handler = r
return api
}
// Start starts API at address
func (a *API) Start(addr string) {
http.ListenAndServe(addr, a.handler)
}