-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from didikprabowo/enhance/routes/handle
Routes ::: handle route and remove cache routes
- Loading branch information
Showing
5 changed files
with
190 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/didikprabowo/dipra" | ||
) | ||
|
||
func main() { | ||
d := dipra.Default() | ||
|
||
d.GET("/quick", func(c *dipra.Context) error { | ||
return c.JSON(200, "WELCOME") | ||
}) | ||
|
||
if err := d.Run(":9020"); err != nil { | ||
fmt.Println(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package dipra | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var ( | ||
// List method is allowed | ||
allowMethod = []string{ | ||
http.MethodGet, | ||
http.MethodDelete, | ||
http.MethodPost, | ||
http.MethodConnect, | ||
http.MethodPatch, | ||
http.MethodPut, | ||
} | ||
) | ||
|
||
// allowMethods for use check your define a method | ||
func (e *Engine) allowMethods(m string) (ok bool) { | ||
for _, mlist := range allowMethod { | ||
if mlist == m { | ||
return true | ||
} | ||
} | ||
return ok | ||
} | ||
|
||
func (e *Engine) cleanPath(p string) string { | ||
return strings.ReplaceAll(p, "/", "") | ||
} | ||
|
||
// allowPath for use check your path | ||
func (e *Engine) allowPath(p string) (ok bool) { | ||
p = e.cleanPath(p) | ||
isAllow := regexp.MustCompile(`[a-zA-Z0-9]$`) | ||
ok = isAllow.MatchString(p) | ||
|
||
return ok | ||
} | ||
|
||
/* | ||
findRouter for used to checking existing path, | ||
method and handler | ||
*/ | ||
func (e *Engine) findRouter(m, p string, h HandlerFunc) (exist bool) { | ||
|
||
if ok := e.allowMethods(m); !ok { | ||
log.Printf("Method %s not allow", m) | ||
return | ||
} | ||
|
||
if ok := e.allowPath(p); !ok { | ||
log.Printf("Path %s is invalid syntax", p) | ||
return | ||
} | ||
|
||
toLower := func(s string) string { | ||
return strings.ToLower(s) | ||
} | ||
|
||
for i := range e.getRoutes() { | ||
if toLower(e.Route[i].Method) == toLower(m) && | ||
toLower(e.Route[i].Path) == toLower(p) { | ||
e.Route[i].Handler = h | ||
return true | ||
} | ||
} | ||
return exist | ||
} | ||
|
||
// getRoutes for get The routes you define | ||
func (e *Engine) getRoutes() []Route { | ||
return e.Route | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package dipra_test | ||
|
||
import ( | ||
"net/http/httptest" | ||
"testing" | ||
|
||
. "github.com/didikprabowo/dipra" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRoutes(t *testing.T) { | ||
|
||
d := Default() | ||
|
||
t.Run("troute", func(t *testing.T) { | ||
t.Run("single", func(t *testing.T) { | ||
testTableRouteData := []Route{ | ||
Route{ | ||
Path: "/index", | ||
Method: "GET", | ||
Handler: func(c *Context) error { | ||
return c.String(200, "OK") | ||
}, | ||
}, | ||
} | ||
|
||
for _, v := range testTableRouteData { | ||
d.AddRoute(v.Path, v.Method, v.Handler) | ||
} | ||
|
||
req := httptest.NewRequest("GET", "/index", nil) | ||
resp := httptest.NewRecorder() | ||
d.ServeHTTP(resp, req) | ||
assert.Equal(t, "OK", resp.Body.String()) | ||
}) | ||
|
||
t.Run("double", func(t *testing.T) { | ||
testTableRouteData := []Route{ | ||
Route{ | ||
Path: "/index", | ||
Method: "GET", | ||
Handler: func(c *Context) error { | ||
return c.String(200, "OK") | ||
}, | ||
}, | ||
Route{ | ||
Path: "/index", | ||
Method: "GET", | ||
Handler: func(c *Context) error { | ||
return c.String(200, "OKE") | ||
}, | ||
}, | ||
} | ||
|
||
for _, v := range testTableRouteData { | ||
d.AddRoute(v.Path, v.Method, v.Handler) | ||
} | ||
|
||
req := httptest.NewRequest("GET", "/index", nil) | ||
resp := httptest.NewRecorder() | ||
d.ServeHTTP(resp, req) | ||
assert.Equal(t, "OKE", resp.Body.String()) | ||
}) | ||
}) | ||
|
||
} |