Skip to content

Commit

Permalink
Merge pull request #20 from RobyFerro/improve-kernel
Browse files Browse the repository at this point in the history
Improve kernel
  • Loading branch information
RobyFerro authored Sep 2, 2021
2 parents 9d67066 + 07d2744 commit 62a8671
Show file tree
Hide file tree
Showing 21 changed files with 124 additions and 157 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to the "Go-Web Framework" will be documented in this file.

## [Unreleased]

## [v0.8.0-beta] - 2021-09-02
### Changed
- Abstract CLI command structure
- Improved kernel registry removing useless declaration.
- Implemented HTTP router in base entities

## [v0.7.1-beta] - 2021-09-02
### Fixed
Expand Down
25 changes: 13 additions & 12 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type BaseEntities struct {
CommandServices register.ServiceRegister
Middlewares register.MiddlewareRegister
Models register.ModelRegister
Router []kernel.HTTRouter
Router []register.HTTPRouter
}

// Start will run the HTTP web server
Expand All @@ -33,7 +33,7 @@ func StartCommand(args []string, e BaseEntities) {
startup(e)

c := kernel.BuildCommandContainer()
cmd := kernel.Commands.List[args[0]]
cmd := kernel.Commands[args[0]]
if cmd == nil {
fmt.Println("Command not found!")
os.Exit(1)
Expand Down Expand Up @@ -63,46 +63,47 @@ func RegisterBaseEntities(entities BaseEntities) {
kernel.Controllers = entities.Controllers
kernel.Middlewares = entities.Middlewares
kernel.Models = entities.Models
kernel.Router = entities.Router

mergeCommands(entities.Commands)
mergeServices(entities.Services.List)
mergeServices(entities.Services)
mergeMiddleware(entities.Middlewares)

mergeSingletonServices(entities.SingletonServices.List)
mergeCommandServices(entities.CommandServices.List)
mergeSingletonServices(entities.SingletonServices)
mergeCommandServices(entities.CommandServices)
}

// Merge services with defaults
func mergeServices(services []interface{}) {
for _, s := range services {
kernel.Services.List = append(kernel.Services.List, s)
kernel.Services = append(kernel.Services, s)
}
}

// Merge singleton services with defaults
func mergeSingletonServices(services []interface{}) {
for _, s := range services {
kernel.SingletonServices.List = append(kernel.SingletonServices.List, s)
kernel.SingletonServices = append(kernel.SingletonServices, s)
}
}

// MergeCommands will merge system command with customs
func mergeCommands(commands register.CommandRegister) {
for i, c := range commands.List {
kernel.Commands.List[i] = c
for i, c := range commands {
kernel.Commands[i] = c
}
}

// MergeCommands will merge system command with customs
func mergeMiddleware(mw register.MiddlewareRegister) {
for i, c := range mw.List {
kernel.Middlewares.List[i] = c
for i, c := range mw {
kernel.Middlewares[i] = c
}
}

// MergeCommands will merge system command with customs
func mergeCommandServices(services []interface{}) {
for _, s := range services {
kernel.CommandServices.List = append(kernel.CommandServices.List, s)
kernel.CommandServices = append(kernel.CommandServices, s)
}
}
5 changes: 2 additions & 3 deletions cli/cmd_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"fmt"
"github.com/RobyFerro/go-web-framework/register"
"github.com/RobyFerro/go-web-framework/tool"
"io/ioutil"
"log"
Expand All @@ -13,9 +14,7 @@ import (

// CmdCreate will create a new CLI command.
type CmdCreate struct {
Signature string
Description string
Args string
register.Command
}

// Register this command
Expand Down
5 changes: 2 additions & 3 deletions cli/controller_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"fmt"
"github.com/RobyFerro/go-web-framework/register"
"github.com/RobyFerro/go-web-framework/tool"
"io/ioutil"
"log"
Expand All @@ -14,9 +15,7 @@ import (

// ControllerCreate will create a new controller
type ControllerCreate struct {
Signature string
Description string
Args string
register.Command
}

// Register this command
Expand Down
12 changes: 5 additions & 7 deletions cli/database_seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import (

// Seeder will handle database seeding.
type Seeder struct {
Signature string
Description string
Args string
register.Command
}

// Register this command
Expand All @@ -27,15 +25,15 @@ func (c *Seeder) Register() {
func (c *Seeder) Run(db *gorm.DB, models register.ModelRegister) {
fmt.Println("Execute database seeding...")
if len(c.Args) > 0 {
extractSpecificModel(c.Args, &models.List)
extractSpecificModel(c.Args, &models)
}

seed(models.List, db)
seed(models, db)
}

// Extract the specified models from model list
func extractSpecificModel(name string, models *[]interface{}) {
var newModels []interface{}
func extractSpecificModel(name string, models *register.ModelRegister) {
var newModels register.ModelRegister

for _, m := range *models {
modelName := reflect.TypeOf(m).Name()
Expand Down
5 changes: 2 additions & 3 deletions cli/generate_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/rand"
"crypto/sha256"
"fmt"
"github.com/RobyFerro/go-web-framework/register"
"github.com/RobyFerro/go-web-framework/tool"
"io/ioutil"
"log"
Expand All @@ -12,9 +13,7 @@ import (

// GenerateKey will generate Go-Web application key in main config.yml file
type GenerateKey struct {
Signature string
Description string
Args string
register.Command
}

// Register this command
Expand Down
5 changes: 2 additions & 3 deletions cli/middleware_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"fmt"
"github.com/RobyFerro/go-web-framework/register"
"github.com/RobyFerro/go-web-framework/tool"
"io/ioutil"
"log"
Expand All @@ -13,9 +14,7 @@ import (

// MiddlewareCreate will create a new http middleware
type MiddlewareCreate struct {
Signature string
Description string
Args string
register.Command
}

// Register this command
Expand Down
5 changes: 2 additions & 3 deletions cli/migration_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"fmt"
"github.com/RobyFerro/go-web-framework/register"
"github.com/RobyFerro/go-web-framework/tool"
"io/ioutil"
"log"
Expand All @@ -13,9 +14,7 @@ import (
// UP: Work only for migrate operation
// DOWN: Work only for rollback operation
type MigrationCreate struct {
Signature string
Description string
Args string
register.Command
}

// Register this command
Expand Down
5 changes: 2 additions & 3 deletions cli/migration_rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"fmt"
"github.com/RobyFerro/go-web-framework/register"
"io/ioutil"
"log"
"strconv"
Expand All @@ -12,9 +13,7 @@ import (

// MigrateRollback will rollback some migration in your database
type MigrateRollback struct {
Signature string
Description string
Args string
register.Command
}

// Register this command
Expand Down
5 changes: 2 additions & 3 deletions cli/migration_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"crypto/sha256"
"fmt"
"github.com/RobyFerro/go-web-framework/register"
"github.com/RobyFerro/go-web-framework/tool"
"io/ioutil"
"log"
Expand All @@ -15,9 +16,7 @@ import (

// MigrationUp will execute database migration
type MigrationUp struct {
Signature string
Description string
Args string
register.Command
}

// Register command
Expand Down
5 changes: 2 additions & 3 deletions cli/model_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"fmt"
"github.com/RobyFerro/go-web-framework/register"
"github.com/RobyFerro/go-web-framework/tool"
"io/ioutil"
"log"
Expand All @@ -14,9 +15,7 @@ import (

// ModelCreate will create a new Gorm model
type ModelCreate struct {
Signature string
Description string
Args string
register.Command
}

// Register this command
Expand Down
4 changes: 1 addition & 3 deletions cli/raw/command.raw
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package console

type @@TMP@@ struct {
Signature string
Description string
Args string
register.Command
}

// Command registration
Expand Down
6 changes: 2 additions & 4 deletions cli/show_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import (

// ShowCommands will show all registered commands
type ShowCommands struct {
Signature string
Description string
Args string
register.Command
}

// Register this command
Expand All @@ -25,7 +23,7 @@ func (c *ShowCommands) Run(commands register.CommandRegister) {

var data [][]string

for _, c := range commands.List {
for _, c := range commands {
m := reflect.ValueOf(c).MethodByName("Register")
m.Call([]reflect.Value{})

Expand Down
20 changes: 0 additions & 20 deletions kernel/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,3 @@ package kernel
func RetrieveAppConf() *ServerConf {
return config
}

/*// RetrieveRoutingConf will parse router.yml file (present in Go-Web root dir) and return a Router structure.
// This structure will be used by the HTTP kernel to setup every routes.
func RetrieveRoutingConf() (*Router, error) {
var conf Router
routePath := tool.GetDynamicPath("routing.yml")
c, err := os.Open(routePath)
if err != nil {
return nil, err
}
decoder := yaml.NewDecoder(c)
if err := decoder.Decode(&conf); err != nil {
return nil, err
}
return &conf, nil
}*/
10 changes: 7 additions & 3 deletions kernel/di.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func BuildCustomContainer() *dig.Container {
container := dig.New()

for _, s := range Services.List {
for _, s := range Services {
if err := container.Provide(s); err != nil {
log.Fatal(err)
}
Expand All @@ -24,7 +24,7 @@ func BuildCustomContainer() *dig.Container {
func BuildCommandContainer() *dig.Container {
container := dig.New()

for _, s := range CommandServices.List {
for _, s := range CommandServices {
if err := container.Provide(s); err != nil {
log.Fatal(err)
}
Expand All @@ -38,7 +38,7 @@ func BuildCommandContainer() *dig.Container {
func BuildSingletonContainer() *dig.Container {
container := dig.New()

for _, s := range SingletonServices.List {
for _, s := range SingletonServices {
if err := container.Provide(s); err != nil {
log.Fatal(err)
}
Expand All @@ -62,4 +62,8 @@ func injectBasicEntities(sc *dig.Container) {
_ = sc.Provide(func() register.ModelRegister {
return Models
})

_ = sc.Provide(func() []register.HTTPRouter {
return Router
})
}
2 changes: 2 additions & 0 deletions kernel/gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
)

// This structure is used to implement GZIP compression over HTTP response.
// Defined as a Gorilla Mux middleware is implemented directly in the gwf router.
type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
Expand Down
8 changes: 2 additions & 6 deletions kernel/middleware.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package kernel

import (
"github.com/RobyFerro/go-web-framework/register"
"github.com/gorilla/mux"
"net/http"
)

type Middleware interface {
Handle(next http.Handler) http.Handler
}

// Parse list of middleware and get an array of []mux.Middleware func
// Required by Gorilla Mux
func parseMiddleware(mwList []Middleware) []mux.MiddlewareFunc {
func parseMiddleware(mwList []register.Middleware) []mux.MiddlewareFunc {
var midFunc []mux.MiddlewareFunc

for i := len(mwList) - 1; i > -1; i-- {
Expand Down
Loading

0 comments on commit 62a8671

Please sign in to comment.