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-
7427func (se * Server ) ContentYaml () {
7528 content , err := ReadYaml (se .Yaml )
7629 if err != nil {
@@ -82,7 +35,7 @@ func (se *Server) ContentYaml() {
8235func (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
13395func (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}
0 commit comments