()
| 10 | ) |
| 11 | |
| 12 | func main() { |
| 13 | flag.Parse() |
| 14 | |
| 15 | // Create a kite. |
| 16 | k := kite.New("math", "1.0.0") |
| 17 | |
| 18 | // Add pre handler method. |
| 19 | k.PreHandleFunc(func(r *kite.Request) (interface{}, error) { |
| 20 | fmt.Println("\nThis pre handler is executed before the method is executed") |
| 21 | resp := "hello from pre handler!" |
| 22 | |
| 23 | // let us return an hello to base square method! |
| 24 | r.Context = context.WithValue(r.Context, "response", resp) |
| 25 | return resp, nil |
| 26 | }) |
| 27 | |
| 28 | // Add post handler method. |
| 29 | k.PostHandleFunc(func(r *kite.Request) (interface{}, error) { |
| 30 | fmt.Println("This post handler is executed after the method is executed") |
| 31 | |
| 32 | // Pass the response from the previous square method back to the |
| 33 | // client, this is imporant if you use post handler. |
| 34 | return r.Context.Value("response").(string), nil |
| 35 | }) |
| 36 | |
| 37 | // Add our handler method, authentication is disabled for this example. |
| 38 | k.HandleFunc("square", Square).DisableAuthentication().PreHandleFunc(func(r *kite.Request) (interface{}, error) { |
| 39 | fmt.Println("This pre handler is only valid for this individual method") |
| 40 | return nil, nil |
| 41 | }) |
| 42 | |
| 43 | // Attach to a server and run it. |
| 44 | k.Config.IP = math.Host.IP() |
| 45 | k.Config.Port = math.Host.Port() |
| 46 | k.Run() |
| 47 | } |
| 48 | |
| 49 | func Square(r *kite.Request) (interface{}, error) { |
| 50 | // Unmarshal method arguments. |
nothing calls this directly
no test coverage detected