Skip to content

Commit

Permalink
Merge pull request #15 from RobyFerro/dynamic-config
Browse files Browse the repository at this point in the history
Dynamic config
  • Loading branch information
RobyFerro committed Sep 1, 2021
2 parents c1d14d5 + f402526 commit 6eb090a
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 180 deletions.
34 changes: 21 additions & 13 deletions CHANGELOG.md
Expand Up @@ -3,53 +3,61 @@
All notable changes to the "Go-Web Framework" will be documented in this file.

## [Unreleased]


## [v0.7.0-beta] - 2021-08-27
### Changed
- Changed .yml routing system in favour of the router module. Now every route/group have to be registered directly in go structure.
- Changed .yml configuration. Now system and custom configuration are located into the new config module.

### Removed
- Removed Mail struct from configuration

## [v0.6-3-beta] - 2021-08-27

## [v0.6.3-beta] - 2021-08-27
### Fixed
- Fixed cookie store issue

## [v0.6-2-beta] - 2021-08-27
## [v0.6.2-beta] - 2021-08-27
### Changed
- Changed middleware architecture. Now every middleware consists in an isolated structure.

## [v0.6-1-beta] - 2021-08-27
## [v0.6.1-beta] - 2021-08-27
### Fix
- Resolved command service container issue

## [v0.6-0-beta] - 2021-08-25
## [v0.6.0-beta] - 2021-08-25
### Changed
- Split command line interfaces and http server.

## [v0.5-9-beta] - 2021-08-23
## [v0.5.9-beta] - 2021-08-23
### Added
- Gzip compression over HTTP responses

### Fixed
- Fixed IOC arguments order issue

## [v0.5-8-beta] - 2021-08-20
## [v0.5.8-beta] - 2021-08-20

### Fixed
- Fixed IOC arguments order issue

## [v0.5-7-beta] - 2021-08-19
## [v0.5.7-beta] - 2021-08-19

### Fixed
- Implemented command ioc container

## [v0.5-6-beta] - 2021-08-19
## [v0.5.6-beta] - 2021-08-19

### Added
- Merged factory and singleton container abstraction

## [v0.5-5-beta] - 2021-08-19
## [v0.5.5-beta] - 2021-08-19

### Fixed
- Controller raw template with right 'kernel' package

## [v0.5-4-beta] - 2021-07-02
## [v0.5.4-beta] - 2021-07-02

### Added
- Insert readme.md
Expand All @@ -60,13 +68,13 @@ All notable changes to the "Go-Web Framework" will be documented in this file.
- Go-Web `server:run` command runs outside the IOC container.
- Expose`SingletonServices`

## [v0.5-3-beta] - 2021-06-21
## [v0.5.3-beta] - 2021-06-21

### Fix

- Fix DI by encapsulating custom services into a separate service container.

## [v0.5-2-beta] - 2021-06-21
## [v0.5.2-beta] - 2021-06-21

### Added

Expand All @@ -75,7 +83,7 @@ All notable changes to the "Go-Web Framework" will be documented in this file.
- Implemented "DecodeJsonRequest" tool
- Implemented "RunJob" tool

## [v0.5-1-beta] - 2021-06-16
## [v0.5.1-beta] - 2021-06-16

### Fix

Expand Down
5 changes: 3 additions & 2 deletions application.go
Expand Up @@ -19,12 +19,13 @@ type BaseEntities struct {
CommandServices register.ServiceRegister
Middlewares register.MiddlewareRegister
Models register.ModelRegister
Router []kernel.HTTRouter
}

// Start will run the HTTP web server
func Start(e BaseEntities) {
func Start(e BaseEntities, c kernel.ServerConf) {
startup(e)
kernel.RunServer()
kernel.RunServer(c, e.Router)
}

// StartCommand method runs specific CLI command
Expand Down
48 changes: 0 additions & 48 deletions cli/jobs_create.go

This file was deleted.

6 changes: 3 additions & 3 deletions ioc.go
Expand Up @@ -13,9 +13,9 @@ func RetrieveSingletonContainer() *dig.Container {
}

// RetrieveConfig provides a shortcut to retrieve the global configuration.
func RetrieveConfig() *kernel.Conf {
var config *kernel.Conf
if err := RetrieveSingletonContainer().Invoke(func(c *kernel.Conf) {
func RetrieveConfig() *kernel.ServerConf {
var config *kernel.ServerConf
if err := RetrieveSingletonContainer().Invoke(func(c *kernel.ServerConf) {
config = c
}); err != nil {
log.Fatal(err)
Expand Down
49 changes: 5 additions & 44 deletions kernel/config.go
@@ -1,31 +1,11 @@
package kernel

import (
"github.com/RobyFerro/go-web-framework/tool"
"gopkg.in/yaml.v3"
"os"
)

// Conf represents the main configuration of Go-Web
// You can implement this method if wanna implement more configuration.
// Remember: this struct will be populated by parsing the config.yml file present into the Go-Web main directory.
// You've to implement both to works properly.
type Conf struct {
Server struct {
Name string `yaml:"name"`
Port int `yaml:"port"`
Ssl bool `yaml:"ssl"`
SslCert string `yaml:"sslcert"`
SslKey string `yaml:"sslkey"`
RunUser string `yaml:"run-user"`
RunGroup string `yaml:"run-group"`
} `yaml:"server"`
App struct {
Key string `yaml:"key"`
} `yaml:"app"`
// RetrieveAppConf returns a `Conf` struct by parsing the main config.yml file.
func RetrieveAppConf() *ServerConf {
return config
}

// RetrieveRoutingConf will parse router.yml file (present in Go-Web root dir) and return a Router structure.
/*// 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
Expand All @@ -43,23 +23,4 @@ func RetrieveRoutingConf() (*Router, error) {
}
return &conf, nil
}

// RetrieveAppConf returns a `Conf` struct by parsing the main config.yml file.
func RetrieveAppConf() (*Conf, error) {
var conf Conf
confFile := tool.GetDynamicPath("config.yml")
c, err := os.Open(confFile)

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: 5 additions & 5 deletions kernel/http.go
Expand Up @@ -10,17 +10,17 @@ import (
)

// CreateSessionStore creates a new session CookieStore
func CreateSessionStore(cfg *Conf) *sessions.CookieStore {
return sessions.NewCookieStore([]byte(os.Getenv(cfg.App.Key)))
func CreateSessionStore() *sessions.CookieStore {
return sessions.NewCookieStore([]byte(os.Getenv(config.Key)))
}

// GetHttpServer prepare HTTP server for Service Container
func GetHttpServer(router *mux.Router, cfg *Conf) *http.Server {
serverString := fmt.Sprintf("%s:%d", cfg.Server.Name, cfg.Server.Port)
func GetHttpServer(router *mux.Router, cfg ServerConf) *http.Server {
serverString := fmt.Sprintf("%s:%d", cfg.Name, cfg.Port)

var httpServerConf = http.Server{}

if cfg.Server.Ssl {
if cfg.SSL {
sslCfg := &tls.Config{
MinVersion: tls.VersionTLS12,
CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},
Expand Down
21 changes: 7 additions & 14 deletions kernel/middleware.go
Expand Up @@ -3,26 +3,19 @@ package kernel
import (
"github.com/gorilla/mux"
"net/http"
"reflect"
)

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 []string) []mux.MiddlewareFunc {
func parseMiddleware(mwList []Middleware) []mux.MiddlewareFunc {
var midFunc []mux.MiddlewareFunc

for _, name := range mwList {
for _, mw := range Middlewares.List {
rName := reflect.ValueOf(mw).Elem().FieldByName("Name").String()

if name == rName {
m := reflect.ValueOf(mw)
method := m.MethodByName("Handle")

callable := method.Interface().(func(handler http.Handler) http.Handler)
midFunc = append(midFunc, callable)
}
}
for _, mw := range mwList {
midFunc = append(midFunc, mw.Handle)
}

return midFunc
Expand Down
1 change: 0 additions & 1 deletion kernel/register.go
Expand Up @@ -17,7 +17,6 @@ var (
"cmd:create": &cli.CmdCreate{},
"controller:create": &cli.ControllerCreate{},
"generate:key": &cli.GenerateKey{},
"job:create": &cli.JobCreate{},
"middleware:create": &cli.MiddlewareCreate{},
"migration:create": &cli.MigrationCreate{},
"migration:rollback": &cli.MigrateRollback{},
Expand Down

0 comments on commit 6eb090a

Please sign in to comment.