mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Middleware (#502)
* API endpoint extensions working. extensions example. extensions example. * Added server.NewEnv and some docs for the API extensions example. extensions example. extensions example. * Uncommented special handler stuff. * First example of middleware. easier to use. * Added a special Middleware context to make middleware easier to use. * Fix tests. * Cleanup based on PR comments.
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
# Extensions Example
|
||||
|
||||
This example adds extra endpoints to the API.
|
||||
This example adds extra endpoints to the API. See [main.go](main.go) for example code.
|
||||
|
||||
## Building and Running
|
||||
|
||||
```sh
|
||||
go build -o functions
|
||||
./functions
|
||||
go build
|
||||
./extensions
|
||||
```
|
||||
|
||||
Then test with:
|
||||
|
||||
2
examples/middleware/.gitignore
vendored
Normal file
2
examples/middleware/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/middleware
|
||||
/middleware.exe
|
||||
27
examples/middleware/README.md
Normal file
27
examples/middleware/README.md
Normal file
@@ -0,0 +1,27 @@
|
||||
# Middleware Example
|
||||
|
||||
This example adds a simple authentication middleware to IronFunctions. See [main.go](main.go) for example code.
|
||||
|
||||
## Building and Running
|
||||
|
||||
```sh
|
||||
go build
|
||||
./middleware
|
||||
```
|
||||
|
||||
Then test with:
|
||||
|
||||
```sh
|
||||
# First, create an app
|
||||
fn apps create myapp
|
||||
# And test
|
||||
curl http://localhost:8080/v1/apps
|
||||
```
|
||||
|
||||
You should get a 401 error.
|
||||
|
||||
Add an auth header and it should go through successfully:
|
||||
|
||||
```sh
|
||||
curl -X GET -H "Authorization: Bearer KlaatuBaradaNikto" http://localhost:8080/v1/apps
|
||||
```
|
||||
50
examples/middleware/main.go
Normal file
50
examples/middleware/main.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/iron-io/functions/api/models"
|
||||
"github.com/iron-io/functions/api/server"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx := context.Background()
|
||||
|
||||
funcServer := server.NewEnv(ctx)
|
||||
|
||||
funcServer.AddMiddlewareFunc(func(ctx server.MiddlewareContext, w http.ResponseWriter, r *http.Request, app *models.App) error {
|
||||
start := time.Now()
|
||||
fmt.Println("CustomMiddlewareFunc called at:", start)
|
||||
// TODO: probably need a way to let the chain go forward here and return back to the middleware, for things like timing, etc.
|
||||
ctx.Next()
|
||||
fmt.Println("Duration:", (time.Now().Sub(start)))
|
||||
return nil
|
||||
})
|
||||
funcServer.AddMiddleware(&CustomMiddleware{})
|
||||
|
||||
funcServer.Start(ctx)
|
||||
}
|
||||
|
||||
type CustomMiddleware struct {
|
||||
}
|
||||
|
||||
func (h *CustomMiddleware) Serve(ctx server.MiddlewareContext, w http.ResponseWriter, r *http.Request, app *models.App) error {
|
||||
fmt.Println("CustomMiddleware called")
|
||||
|
||||
// check auth header
|
||||
tokenHeader := strings.SplitN(r.Header.Get("Authorization"), " ", 3)
|
||||
if len(tokenHeader) < 2 || tokenHeader[1] != "KlaatuBaradaNikto" {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
m := map[string]string{"error": "Invalid Authorization token. Sorry!"}
|
||||
json.NewEncoder(w).Encode(m)
|
||||
return errors.New("Invalid authorization token.")
|
||||
}
|
||||
fmt.Println("auth succeeded!")
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user