@@ -2,34 +2,35 @@ package http
22
33import (
44 "encoding/json"
5- "fmt"
65 "net/http"
76
87 m "github.com/antoineaugusti/feature-flags/models"
98 services "github.com/antoineaugusti/feature-flags/services"
109 "github.com/gorilla/mux"
1110)
1211
12+ // Handles incoming requests
1313type APIHandler struct {
1414 FeatureService services.FeatureService
1515}
1616
17+ // A simple structure to respond with error messages
1718type APIMessage struct {
18- code int
19- Status string `json:"status"`
19+ // The HTTP status code
20+ code int
21+ // A status message
22+ Status string `json:"status"`
23+ // A human readable message
2024 Message string `json:"message"`
2125}
2226
27+ // Describes the request when checking the access to a feature
2328type AccessRequest struct {
2429 Groups []string `json:"groups"`
2530 User uint32 `json:"user"`
2631}
2732
28- func (handler * APIHandler ) Welcome (w http.ResponseWriter , r * http.Request ) {
29- fmt .Fprint (w , "Hello World!\n " )
30- }
31-
32- func (handler * APIHandler ) FeatureIndex (w http.ResponseWriter , r * http.Request ) {
33+ func (handler APIHandler ) FeatureIndex (w http.ResponseWriter , r * http.Request ) {
3334 features , err := handler .FeatureService .GetFeatures ()
3435 if err != nil {
3536 panic (err )
@@ -42,11 +43,11 @@ func (handler *APIHandler) FeatureIndex(w http.ResponseWriter, r *http.Request)
4243 }
4344}
4445
45- func (handler * APIHandler ) FeatureShow (w http.ResponseWriter , r * http.Request ) {
46+ func (handler APIHandler ) FeatureShow (w http.ResponseWriter , r * http.Request ) {
4647 vars := mux .Vars (r )
4748
4849 // Check if the feature exists
49- if ! handler .FeatureExists (vars ["featureKey" ]) {
50+ if ! handler .featureExists (vars ["featureKey" ]) {
5051 writeNotFound (w )
5152 return
5253 }
@@ -64,12 +65,12 @@ func (handler *APIHandler) FeatureShow(w http.ResponseWriter, r *http.Request) {
6465 }
6566}
6667
67- func (handler * APIHandler ) FeatureAccess (w http.ResponseWriter , r * http.Request ) {
68+ func (handler APIHandler ) FeatureAccess (w http.ResponseWriter , r * http.Request ) {
6869 var ar AccessRequest
6970 vars := mux .Vars (r )
7071
7172 // Check if the feature exists
72- if ! handler .FeatureExists (vars ["featureKey" ]) {
73+ if ! handler .featureExists (vars ["featureKey" ]) {
7374 writeNotFound (w )
7475 return
7576 }
@@ -109,11 +110,11 @@ func (handler *APIHandler) FeatureAccess(w http.ResponseWriter, r *http.Request)
109110 }
110111}
111112
112- func (handler * APIHandler ) FeatureRemove (w http.ResponseWriter , r * http.Request ) {
113+ func (handler APIHandler ) FeatureRemove (w http.ResponseWriter , r * http.Request ) {
113114 vars := mux .Vars (r )
114115
115116 // Check if the feature exists
116- if ! handler .FeatureExists (vars ["featureKey" ]) {
117+ if ! handler .featureExists (vars ["featureKey" ]) {
117118 writeNotFound (w )
118119 return
119120 }
@@ -127,7 +128,7 @@ func (handler *APIHandler) FeatureRemove(w http.ResponseWriter, r *http.Request)
127128 writeMessage (http .StatusOK , "feature_deleted" , "The feature was successfully deleted" , w )
128129}
129130
130- func (handler * APIHandler ) FeatureCreate (w http.ResponseWriter , r * http.Request ) {
131+ func (handler APIHandler ) FeatureCreate (w http.ResponseWriter , r * http.Request ) {
131132 var feature m.FeatureFlag
132133
133134 if err := json .NewDecoder (r .Body ).Decode (& feature ); err != nil {
@@ -153,11 +154,11 @@ func (handler *APIHandler) FeatureCreate(w http.ResponseWriter, r *http.Request)
153154 }
154155}
155156
156- func (handler * APIHandler ) FeatureEdit (w http.ResponseWriter , r * http.Request ) {
157+ func (handler APIHandler ) FeatureEdit (w http.ResponseWriter , r * http.Request ) {
157158 vars := mux .Vars (r )
158159
159160 // Check if the feature exists
160- if ! handler .FeatureExists (vars ["featureKey" ]) {
161+ if ! handler .featureExists (vars ["featureKey" ]) {
161162 writeNotFound (w )
162163 return
163164 }
@@ -192,7 +193,7 @@ func (handler *APIHandler) FeatureEdit(w http.ResponseWriter, r *http.Request) {
192193 }
193194}
194195
195- func (handler * APIHandler ) FeatureExists (featureKey string ) bool {
196+ func (handler APIHandler ) featureExists (featureKey string ) bool {
196197 return handler .FeatureService .FeatureExists (featureKey )
197198}
198199
0 commit comments