forked from iron-io/functions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
50 lines (41 loc) · 1.34 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
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.NewFromEnv(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
}