Skip to content

Commit

Permalink
Merge pull request #21 from RobyFerro/cmd-show-routes
Browse files Browse the repository at this point in the history
Cmd show routes
  • Loading branch information
RobyFerro authored Sep 3, 2021
2 parents 62a8671 + 9caa5ef commit d3cdcd1
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 60 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to the "Go-Web Framework" will be documented in this file.

## [Unreleased]

## [v0.8.1-beta] - 2021-09-03
### Added
- New show routes command

### Changed
- Middleware architecture

## [v0.8.0-beta] - 2021-09-02
### Changed
- Abstract CLI command structure
Expand Down
18 changes: 18 additions & 0 deletions cli/raw/middleware.raw
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,21 @@ func (@@TMP@@Middleware) Handle(next http.Handler) http.Handler {
// Do stuff here
})
}

// GetName returns the middleware name
func (m *@@TMP@@Middleware) GetName() string {
return m.Name
}

// GetDescription returns the middleware description
func (m *@@TMP@@Middleware) GetDescription() string {
return m.Description
}

func New@@TMP@@Middleware() @@TMP@@Middleware{
return @@TMP@@Middleware{
Name: "REPLACE WITH YOUR MIDDLEWARE NAME",
Description: "REPLACE WITH YOUR MIDDLEWARE DESCRIPTION",
}
}

55 changes: 55 additions & 0 deletions cli/router_show.go
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, ",")
}
1 change: 1 addition & 0 deletions kernel/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var (
"migration:rollback": &cli.MigrateRollback{},
"migration:up": &cli.MigrationUp{},
"model:create": &cli.ModelCreate{},
"router:show": &cli.RouterShow{},
// Here is where you've to register your custom controller
}
SingletonServices = register.ServiceRegister{
Expand Down
11 changes: 11 additions & 0 deletions register/cli.go
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
}
13 changes: 13 additions & 0 deletions register/generic.go
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{}
13 changes: 13 additions & 0 deletions register/middleware.go
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{}
26 changes: 26 additions & 0 deletions register/router.go
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
}
60 changes: 0 additions & 60 deletions register/types.go

This file was deleted.

0 comments on commit d3cdcd1

Please sign in to comment.