539053a32a3bc670932ab0abe382ad89e2654bd1
[msb/apigateway.git] / openresty-ext / src / assembly / resources / openresty / nginx / luaext / plugins / sampleplugin / handler.lua
1 --[[
2
3     Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
4
5     Licensed under the Apache License, Version 2.0 (the "License");
6     you may not use this file except in compliance with the License.
7     You may obtain a copy of the License at
8
9             http://www.apache.org/licenses/LICENSE-2.0
10
11     Unless required by applicable law or agreed to in writing, software
12     distributed under the License is distributed on an "AS IS" BASIS,
13     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14     See the License for the specific language governing permissions and
15     limitations under the License.
16
17 --]]
18
19 local BasePlugin = require "plugins.base_plugin"
20
21 local SamplePluginHandler = BasePlugin:extend()
22
23 function SamplePluginHandler:new()
24         SamplePluginHandler.super.new(self, "sampleplugin")
25 end
26
27 function SamplePluginHandler:access()
28         SamplePluginHandler.super.access(self)
29         --[[more about the use of APIs please refer to github doc 
30             https://github.com/openresty/lua-nginx-module
31         ]]
32         --validate and rewrite
33         if(ngx.req.get_method() == "GET") then
34                 ngx.req.set_uri("/sayhello")
35                 ngx.var.backend = "127.0.0.1:10089"
36         else
37                 ngx.status = ngx.HTTP_NOT_ALLOWED
38                 ngx.exit(ngx.status)
39         end
40         --access
41         --[[
42         local client_ip = ngx.var.remote_addr
43         if(client_ip ~= "127.0.0.1") then
44                 ngx.status = ngx.HTTP_FORBIDDEN
45                 ngx.exit(ngx.status)
46         end
47         ]]
48 end
49
50 return SamplePluginHandler