-
Notifications
You must be signed in to change notification settings - Fork 2
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 #21 from RobyFerro/cmd-show-routes
Cmd show routes
- Loading branch information
Showing
9 changed files
with
144 additions
and
60 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
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,55 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"github.com/RobyFerro/go-web-framework/register" | ||
"github.com/olekukonko/tablewriter" | ||
"os" | ||
"strings" | ||
) | ||
|
||
type RouterShow struct { | ||
register.Command | ||
} | ||
|
||
func (c *RouterShow) Register() { | ||
c.Signature = "router:show" | ||
c.Description = "Show all available routes" | ||
} | ||
|
||
func (c *RouterShow) Run(routes []register.HTTPRouter) { | ||
table := tablewriter.NewWriter(os.Stdout) | ||
table.SetHeader([]string{"PATH", "METHOD", "DESCRIPTION", "MIDDLEWARES"}) | ||
for _, router := range routes { | ||
parseRoutes(router.Route, table, nil) | ||
parseGroups(router.Groups, table) | ||
} | ||
|
||
table.Render() | ||
} | ||
|
||
func parseGroups(groups []register.Group, table *tablewriter.Table) { | ||
for _, group := range groups { | ||
parseRoutes(group.Routes, table, &group.Prefix) | ||
} | ||
} | ||
|
||
func parseRoutes(routes []register.Route, table *tablewriter.Table, prefix *string) { | ||
for _, r := range routes { | ||
middlewares := getMiddlewareString(&r.Middleware) | ||
if prefix != nil { | ||
r.Path = fmt.Sprintf("%s%s", *prefix, r.Path) | ||
} | ||
|
||
table.Append([]string{r.Path, r.Method, r.Description, middlewares}) | ||
} | ||
} | ||
|
||
func getMiddlewareString(middlewares *[]register.Middleware) string { | ||
var list []string | ||
for _, m := range *middlewares { | ||
list = append(list, fmt.Sprintf("%s", m.GetName())) | ||
} | ||
|
||
return strings.Join(list, ",") | ||
} |
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,11 @@ | ||
package register | ||
|
||
// CommandRegister defines all registered commands | ||
type CommandRegister map[string]interface{} | ||
|
||
// Command is used to define a CLI command | ||
type Command struct { | ||
Signature string | ||
Description string | ||
Args string | ||
} |
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,13 @@ | ||
package register | ||
|
||
// ServiceRegister defines a controller register type. | ||
// This will be used to resolve this register in service container | ||
type ServiceRegister []interface{} | ||
|
||
// ModelRegister defines a controller register type. | ||
// This will be used to resolve this register in service container | ||
type ModelRegister []interface{} | ||
|
||
// ControllerRegister defines a controller register type. | ||
// This will be used to resolve this register in service container | ||
type ControllerRegister []interface{} |
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,13 @@ | ||
package register | ||
|
||
import "net/http" | ||
|
||
// Middleware define an interface used to handle all application middleware | ||
type Middleware interface { | ||
Handle(next http.Handler) http.Handler | ||
GetName() string | ||
GetDescription() string | ||
} | ||
|
||
// MiddlewareRegister defines all middleware present in your web application | ||
type MiddlewareRegister []interface{} |
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,26 @@ | ||
package register | ||
|
||
// Route defines an HTTP Router endpoint | ||
type Route struct { | ||
Name string | ||
Path string | ||
Action string | ||
Method string | ||
Description string | ||
Validation interface{} | ||
Middleware []Middleware | ||
} | ||
|
||
// Group defines a group of HTTP Route | ||
type Group struct { | ||
Name string | ||
Prefix string | ||
Routes []Route | ||
Middleware []Middleware | ||
} | ||
|
||
// HTTPRouter contains Route and Group that defines a complete HTTP Router | ||
type HTTPRouter struct { | ||
Route []Route | ||
Groups []Group | ||
} |
This file was deleted.
Oops, something went wrong.