Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 36727b9

Browse filesBrowse files
authored
Merge pull request #14 from lbernardo/feature/gorilla
Feature/gorilla
2 parents a5e3f6f + 3f87aa6 commit 36727b9
Copy full SHA for 36727b9

6 files changed

+42-85Lines changed: 42 additions & 85 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎.gitignore‎

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea/
Collapse file

‎bin/darwin/lambda-local‎

Copy file name to clipboard
123 KB
Binary file not shown.
Collapse file

‎bin/linux/lambda-local‎

Copy file name to clipboard
128 KB
Binary file not shown.
Collapse file

‎cmd/start.go‎

Copy file name to clipboardExpand all lines: cmd/start.go
-9Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,5 @@ func ExecuteCmdStart(cmd *cobra.Command, args []string) {
4242
Network: Network,
4343
EnvironmentFile: Environment,
4444
}
45-
ep := controller.EndpointsController{
46-
Host: Host,
47-
Port: Port,
48-
Yaml: Yaml,
49-
}
50-
se.StartConfig()
51-
52-
ep.ListEndpoints()
53-
5445
se.StartServer()
5546
}
Collapse file

‎controller/server.go‎

Copy file name to clipboardExpand all lines: controller/server.go
+39-74Lines changed: 39 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"log"
88
"net/http"
99
"os"
10-
"regexp"
1110
"strings"
1211

12+
"github.com/gorilla/mux"
1313
"github.com/lbernardo/lambda-local/lambda"
1414
"github.com/lbernardo/lambda-local/model"
1515
)
@@ -24,53 +24,6 @@ type Server struct {
2424
JSON model.Serverless
2525
}
2626

27-
var strReg = "{[a-z0-9A-Z-]+}"
28-
29-
const varsKey int = iota
30-
31-
func checkPath(event model.HttpEvent, reqPath string, method string) (bool, map[string]string) {
32-
parameters := map[string]string{}
33-
emethod := strings.ToUpper(event.Method)
34-
35-
event.Path = strings.ReplaceAll("/"+event.Path, "//", "/")
36-
37-
if emethod != method {
38-
return false, parameters
39-
}
40-
41-
if event.Path == reqPath {
42-
return true, parameters
43-
}
44-
45-
match, _ := regexp.MatchString(strReg, event.Path)
46-
if match == true {
47-
reg, _ := regexp.Compile(strReg)
48-
49-
ep := reg.ReplaceAllString(event.Path, "[a-z0-9A-Z-]+")
50-
p := strings.ReplaceAll(ep, "[a-z0-9A-Z-]+", "")
51-
52-
match, _ = regexp.MatchString(ep, reqPath)
53-
if match == true {
54-
reg2, _ := regexp.Compile("[a-z0-9A-Z-]+")
55-
params := reg.FindStringSubmatch(event.Path)
56-
m := strings.ReplaceAll(reqPath, p, "")
57-
values := reg2.FindStringSubmatch(m)
58-
var value string
59-
60-
for i, param := range params {
61-
value = values[i]
62-
param = strings.ReplaceAll(param, "{", "")
63-
param = strings.ReplaceAll(param, "}", "")
64-
parameters[param] = value
65-
}
66-
67-
return true, parameters
68-
}
69-
}
70-
71-
return false, parameters
72-
}
73-
7427
func (se *Server) ContentYaml() {
7528
content, err := ReadYaml(se.Yaml)
7629
if err != nil {
@@ -82,7 +35,7 @@ func (se *Server) ContentYaml() {
8235
func (se *Server) ReadEnv() {
8336
se.JSON.Provider.Environment = make(map[string]string, 0)
8437
if se.EnvironmentFile != "" {
85-
file, err := os.Open(".local.env")
38+
file, err := os.Open(se.EnvironmentFile)
8639
if err != nil {
8740
log.Fatal(err)
8841
}
@@ -100,37 +53,49 @@ func (se *Server) ReadEnv() {
10053
}
10154
}
10255

103-
func (se *Server) StartConfig() {
104-
se.ContentYaml()
56+
func (se *Server) executeHandler(handler string, ) {
57+
58+
}
59+
60+
func (se *Server) StartConfig() *mux.Router {
10561
lambda.PullImageDocker(se.JSON.Provider.Runtime)
10662

107-
http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
108-
se.ContentYaml()
109-
se.ReadEnv()
110-
for _, functions := range se.JSON.Functions {
111-
check, parameters := checkPath(functions.Events[0].HttpEvent, r.URL.RequestURI(), r.Method)
112-
if check {
113-
result, off := lambda.ExecuteDockerLambda(se.Volume, se.Network, functions.Handler, se.JSON.Provider.Runtime, se.JSON.Provider.Environment, r.Body, parameters)
114-
if result.StatusCode == 0 {
115-
w.WriteHeader(400)
116-
fmt.Println(off)
117-
return
118-
}
119-
120-
for key, val := range result.Headers {
121-
w.Header().Set(key, val)
122-
}
123-
w.WriteHeader(result.StatusCode)
124-
w.Write([]byte(result.Body))
63+
route := mux.NewRouter()
64+
65+
for _, functions := range se.JSON.Functions {
66+
path := "/" + functions.Events[0].HttpEvent.Path
67+
method := functions.Events[0].HttpEvent.Method
68+
function := functions.Handler
69+
70+
path = strings.ReplaceAll(path,"//","/")
71+
72+
fmt.Printf("http://%v:%v%v [%v]\n\n", se.Host, se.Port, path, strings.ToUpper(method))
73+
fff := func(w http.ResponseWriter, r *http.Request) {
74+
parameters := mux.Vars(r)
75+
result, off := lambda.ExecuteDockerLambda(se.Volume, se.Network, function, se.JSON.Provider.Runtime, se.JSON.Provider.Environment, r.Body, parameters)
76+
if result.StatusCode == 0 {
77+
w.WriteHeader(400)
78+
fmt.Println(off)
12579
return
12680
}
81+
82+
for key, val := range result.Headers {
83+
w.Header().Set(key, val)
84+
}
85+
w.WriteHeader(result.StatusCode)
86+
w.Write([]byte(result.Body))
87+
return
12788
}
128-
w.WriteHeader(404)
129-
w.Write([]byte("404 Not found"))
130-
}))
89+
route.HandleFunc(path, fff).Methods(method)
90+
}
91+
92+
return route
13193
}
13294

13395
func (se *Server) StartServer() {
134-
fmt.Printf("Start server lambda %v:%v\n", se.Host, se.Port)
135-
log.Fatal(http.ListenAndServe(se.Host+":"+se.Port, nil))
96+
se.ContentYaml()
97+
se.ReadEnv()
98+
99+
fmt.Printf("Start server API Gateway -> lambda %v:%v\n", se.Host, se.Port)
100+
log.Fatal(http.ListenAndServe(se.Host+":"+se.Port, se.StartConfig()))
136101
}
Collapse file

‎example/go/serverless.yml‎

Copy file name to clipboardExpand all lines: example/go/serverless.yml
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ functions:
1212
handler: bin/hello
1313
events:
1414
- http:
15-
path: hello
15+
path: hello/{id}
1616
method: get
1717
world:
1818
handler: bin/world
1919
events:
2020
- http:
21-
path: world
21+
path: /world
2222
method: get

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.