@@ -149,8 +149,8 @@ func (a *App) deleteProduct(w http.ResponseWriter, r *http.Request) {
149149 respondWithMessage (w , http .StatusOK , "Deleted." )
150150}
151151
152- func (a * App ) authHandler ( f http.HandlerFunc ) http.HandlerFunc {
153- return func (w http.ResponseWriter , r * http.Request ) {
152+ func (a * App ) authMiddleware ( next http.Handler ) http.Handler {
153+ return http . HandlerFunc ( func (w http.ResponseWriter , r * http.Request ) {
154154 s := strings .SplitN (r .Header .Get ("Authorization" ), " " , 2 )
155155 if len (s ) != 2 {
156156 respondWithError (w , http .StatusUnauthorized , "Invalid/Missing Credentials." )
@@ -181,16 +181,16 @@ func (a *App) authHandler(f http.HandlerFunc) http.HandlerFunc {
181181 return
182182 }
183183
184- f (w , r )
185- }
184+ next . ServeHTTP (w , r )
185+ })
186186}
187187
188188func (a * App ) InitializeRoutes () {
189- a .Router .HandleFunc ("/api/products/list" , a .authHandler ( a . getProducts ) ).Methods ("GET" )
190- a .Router .HandleFunc ("/api/products/new" , a .authHandler ( a . createProduct ) ).Methods ("POST" )
191- a .Router .HandleFunc ("/api/products/{id:[0-9]+}" , a .authHandler ( a . getProduct ) ).Methods ("GET" )
192- a .Router .HandleFunc ("/api/products/{id:[0-9]+}" , a .authHandler ( a . updateProduct ) ).Methods ("PUT" )
193- a .Router .HandleFunc ("/api/products/{id:[0-9]+}" , a .authHandler ( a . deleteProduct ) ).Methods ("DELETE" )
189+ a .Router .HandleFunc ("/api/products/list" , a .getProducts ).Methods ("GET" )
190+ a .Router .HandleFunc ("/api/products/new" , a .createProduct ).Methods ("POST" )
191+ a .Router .HandleFunc ("/api/products/{id:[0-9]+}" , a .getProduct ).Methods ("GET" )
192+ a .Router .HandleFunc ("/api/products/{id:[0-9]+}" , a .updateProduct ).Methods ("PUT" )
193+ a .Router .HandleFunc ("/api/products/{id:[0-9]+}" , a .deleteProduct ).Methods ("DELETE" )
194194}
195195
196196func (a * App ) Initialize (username , password , server , port , dbName string ) {
@@ -202,9 +202,15 @@ func (a *App) Initialize(username, password, server, port, dbName string) {
202202
203203 a .Router = mux .NewRouter ()
204204 a .Logger = handlers .CombinedLoggingHandler (os .Stdout , a .Router )
205+ a .Router .Use (a .authMiddleware )
205206 a .InitializeRoutes ()
206207}
207208
208209func (a * App ) Run (addr string ) {
209- log .Fatal (http .ListenAndServe (":" + viper .GetString ("Server.port" ), a .Logger ))
210+ // https://stackoverflow.com/questions/38376226/how-to-allow-options-method-from-mobile-using-gorilla-handler
211+ headersOk := handlers .AllowedHeaders ([]string {"X-Requested-With" , "Content-Type" , "Authorization" })
212+ originsOk := handlers .AllowedOrigins ([]string {"*" })
213+ methodsOk := handlers .AllowedMethods ([]string {"GET" , "HEAD" , "POST" , "PUT" , "OPTIONS" })
214+ log .Fatal (http .ListenAndServe (":" + viper .GetString ("Server.port" ),
215+ handlers .CORS (headersOk , originsOk , methodsOk )(a .Logger )))
210216}
0 commit comments