Skip to content

Commit 7517a7c

Browse files
committed
path params
1 parent 302a5aa commit 7517a7c

File tree

5 files changed

+38
-12
lines changed

5 files changed

+38
-12
lines changed

bin/darwin/lambda-local

4.08 KB
Binary file not shown.

bin/linux/lambda-local

4.07 KB
Binary file not shown.

controller/server.go

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,51 @@ type Server struct {
2121
JSON model.Serverless
2222
}
2323

24-
func checkPath(event model.HttpEvent, reqPath string, method string) bool {
24+
var strReg = "{[a-z0-9A-Z-]+}"
25+
26+
const varsKey int = iota
27+
28+
func checkPath(event model.HttpEvent, reqPath string, method string) (bool, map[string]string) {
29+
parameters := map[string]string{}
2530
emethod := strings.ToUpper(event.Method)
2631

2732
event.Path = strings.ReplaceAll("/"+event.Path, "//", "/")
2833

2934
if emethod != method {
30-
return false
35+
return false, parameters
3136
}
3237

3338
if event.Path == reqPath {
34-
return true
39+
return true, parameters
3540
}
3641

37-
match, _ := regexp.MatchString("{[a-z0-9A-Z-]+}", event.Path)
42+
match, _ := regexp.MatchString(strReg, event.Path)
3843
if match == true {
39-
reg, _ := regexp.Compile("{[a-z0-9A-Z-]+}")
44+
reg, _ := regexp.Compile(strReg)
45+
4046
ep := reg.ReplaceAllString(event.Path, "[a-z0-9A-Z-]+")
47+
p := strings.ReplaceAll(ep, "[a-z0-9A-Z-]+", "")
48+
4149
match, _ = regexp.MatchString(ep, reqPath)
4250
if match == true {
43-
return true
51+
reg2, _ := regexp.Compile("[a-z0-9A-Z-]+")
52+
params := reg.FindStringSubmatch(event.Path)
53+
m := strings.ReplaceAll(reqPath, p, "")
54+
values := reg2.FindStringSubmatch(m)
55+
var value string
56+
57+
for i, param := range params {
58+
value = values[i]
59+
param = strings.ReplaceAll(param, "{", "")
60+
param = strings.ReplaceAll(param, "}", "")
61+
parameters[param] = value
62+
}
63+
64+
return true, parameters
4465
}
4566
}
4667

47-
return false
68+
return false, parameters
4869
}
4970

5071
func (se *Server) ContentYaml() {
@@ -62,8 +83,9 @@ func (se *Server) StartConfig() {
6283
http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
6384
se.ContentYaml()
6485
for _, functions := range se.JSON.Functions {
65-
if checkPath(functions.Events[0].HttpEvent, r.URL.RequestURI(), r.Method) {
66-
result, off := lambda.ExecuteDockerLambda(se.Volume, se.Network, functions.Handler, se.JSON.Provider["runtime"], r.Body)
86+
check, parameters := checkPath(functions.Events[0].HttpEvent, r.URL.RequestURI(), r.Method)
87+
if check {
88+
result, off := lambda.ExecuteDockerLambda(se.Volume, se.Network, functions.Handler, se.JSON.Provider["runtime"], r.Body, parameters)
6789
if result.StatusCode == 0 {
6890
w.WriteHeader(400)
6991
fmt.Println(off)

lambda/lambda.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import (
1818
)
1919

2020
type ContentRequest struct {
21-
Body string `json:"body"`
21+
Body string `json:"body"`
22+
PathParameters map[string]string `json:"pathparameters"`
2223
}
2324

2425
func PullImageDocker(runtime string) {
@@ -36,7 +37,7 @@ func PullImageDocker(runtime string) {
3637
io.Copy(os.Stdout, reader)
3738
}
3839

39-
func ExecuteDockerLambda(volume string, net string, handler string, runtime string, body io.ReadCloser) (model.Result, string) {
40+
func ExecuteDockerLambda(volume string, net string, handler string, runtime string, body io.ReadCloser, parameters map[string]string) (model.Result, string) {
4041
var result model.Result
4142
var output bytes.Buffer
4243
var contentRequest ContentRequest
@@ -55,6 +56,7 @@ func ExecuteDockerLambda(volume string, net string, handler string, runtime stri
5556
bodyStr = strings.ReplaceAll(bodyStr, "\t", "")
5657
bodyStr = strings.ReplaceAll(bodyStr, "\n", "")
5758
contentRequest.Body = bodyStr
59+
contentRequest.PathParameters = parameters
5860

5961
jsonRequest, _ := json.Marshal(contentRequest)
6062

main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

3-
import "github.com/lbernardo/lambda-local/cmd"
3+
import (
4+
"github.com/lbernardo/lambda-local/cmd"
5+
)
46

57
func main() {
68
cmd.Execute()

0 commit comments

Comments
 (0)