Skip to content

Commit

Permalink
Merge pull request #39 from lanvard/#33-configuration-caching
Browse files Browse the repository at this point in the history
#33 configuration caching
  • Loading branch information
Reindert authored Jan 6, 2020
2 parents 534ed26 + 4721a99 commit 7038085
Show file tree
Hide file tree
Showing 21 changed files with 61 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
APP_NAME=Lanvard
APP_ENV=local
APP_ENV=development
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost
5 changes: 5 additions & 0 deletions .env.testing
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
APP_NAME=TestLanvard
APP_ENV=testing
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost
4 changes: 2 additions & 2 deletions app/console/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package console
import "github.com/lanvard/foundation"

type Kernel struct {
App foundation.Application
App *foundation.Application
}

func NewKernel(app foundation.Application) Kernel {
func NewKernel(app *foundation.Application) Kernel {
return Kernel{app}
}
4 changes: 2 additions & 2 deletions app/exception/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package exception
import "github.com/lanvard/foundation"

type Handler struct {
app foundation.Application
app *foundation.Application
}

func NewHandler(app foundation.Application) Handler {
func NewHandler(app *foundation.Application) Handler {
return Handler{app}
}
2 changes: 1 addition & 1 deletion app/http/decorator/boot_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
type BootProviders struct{}

// Providers are located in config/providers/providers.go
func (r BootProviders) Bootstrap(app foundation.Application) foundation.Application {
func (r BootProviders) Bootstrap(app *foundation.Application) *foundation.Application {
for _, bootstrapper := range providers.Providers.BootProviders {
app = bootstrapper.Boot(app)
}
Expand Down
5 changes: 1 addition & 4 deletions app/http/decorator/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ package decorator
import (
contract "github.com/lanvard/contract/decorator"
"github.com/lanvard/foundation"
"github.com/lanvard/foundation/bootstrap"
"github.com/lanvard/foundation/decorator"
)

var bootstraps = []contract.Bootstrap{
bootstrap.BasePathProvider{},
bootstrap.LoadEnvironmentVariables{},
RegisterProviders{},
BootProviders{},
}

func Bootstrap(app foundation.Application) foundation.Application {
func Bootstrap(app *foundation.Application) *foundation.Application {
dec := decorator.BootstrapDecorator{Bootstraps: bootstraps}

return dec.BootstrapWith(app)
Expand Down
2 changes: 1 addition & 1 deletion app/http/decorator/register_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
type RegisterProviders struct{}

// Providers are located in config/providers/providers.go
func (r RegisterProviders) Bootstrap(app foundation.Application) foundation.Application {
func (r RegisterProviders) Bootstrap(app *foundation.Application) *foundation.Application {
for _, bootstrapper := range providers.Providers.RegisterProviders {
app = bootstrapper.Register(app)
}
Expand Down
4 changes: 2 additions & 2 deletions app/http/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"lanvard/app/http/middleware"
)

func NewKernel(app foundation.Application) http.Kernel {
func NewKernel(app *foundation.Application) http.Kernel {
return http.Kernel{
App: app,
Middleware: pipes(),
Expand All @@ -16,7 +16,7 @@ func NewKernel(app foundation.Application) http.Kernel {

func pipes() []middleware.PipeInterface {
return []middleware.PipeInterface{
// todo push app
// todo remove or use ValidatePostSize
foundationMiddleware.ValidatePostSize{},
}
}
4 changes: 2 additions & 2 deletions app/http/middleware/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type PipeInterface interface {

// noinspection GoNameStartsWithPackageName
type Pipeline struct {
App foundation.Application
App *foundation.Application

// The object being passed through the contract.
Passable Passable
Expand All @@ -28,7 +28,7 @@ type Pipeline struct {
Pipes []PipeInterface
}

func NewPipeline(app foundation.Application) Pipeline {
func NewPipeline(app *foundation.Application) Pipeline {
return Pipeline{App: app}
}

Expand Down
4 changes: 2 additions & 2 deletions app/providers/app_service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
type AppServiceProvider struct{}

// Register any application services.
func (a AppServiceProvider) Register(app foundation.Application) foundation.Application {
func (a AppServiceProvider) Register(app *foundation.Application) *foundation.Application {

//

return app
}

// Bootstrap any application services.
func (a AppServiceProvider) Boot(app foundation.Application) foundation.Application {
func (a AppServiceProvider) Boot(app *foundation.Application) *foundation.Application {

//

Expand Down
2 changes: 1 addition & 1 deletion app/providers/route_service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type RouteServiceProvider struct{}

// Define your router model bindings, pattern filters, etc.
func (p RouteServiceProvider) Boot(app foundation.Application) foundation.Application {
func (p RouteServiceProvider) Boot(app *foundation.Application) *foundation.Application {
collection := routing.NewRouteCollection()

collection.Merge(routes.Api)
Expand Down
4 changes: 2 additions & 2 deletions bin/generic_generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
func main() {
for _, generic := range generics.Generics {

file := generic.Struct.AppPath()
file := config.App.BasePath.AppPath()
content := contentByPath(file)

content = replaceDocs(content, file)
Expand Down Expand Up @@ -57,7 +57,7 @@ func contentByPath(file string) string {
}

func replaceDocs(content string, originalFile string) string {
originalRelative := strings.Replace(originalFile, config.App.BasePath, "", 1)
originalRelative := strings.Replace(originalFile, string(config.App.BasePath), "", 1)

regexOriginal := regexp.MustCompile("This is the original file")
content = regexOriginal.ReplaceAllString(content, "Original file: "+originalRelative)
Expand Down
16 changes: 8 additions & 8 deletions bootstrap/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func init() {
| Create The Application
|--------------------------------------------------------------------------
|
| The first thing we will do is create a new Laravel application instance
| which serves as the "glue" for all the components of Laravel, and is
| The first thing we will do is create a new Lanvard application instance
| which serves as the "glue" for all the components of Lanvard, and is
| the IoC container for the system binding all of the various parts.
|
*/
Expand All @@ -28,29 +28,29 @@ func init() {

bootApp.BindPathsInContainer()

bootApp = http.NewKernel(bootApp).Bootstrap()
bootApp = *http.NewKernel(&bootApp).Bootstrap()
}

func NewApp() foundation.Application {
func NewApp() *foundation.Application {

app := foundation.Application{
Container: bootApp.Container.Copy(),
}

app.Container.Singleton(
(*interfaceHttp.Kernel)(nil),
http.NewKernel(app),
http.NewKernel(&app),
)

app.Container.Singleton(
(*consoleInterface.Kernel)(nil),
console.NewKernel(app),
console.NewKernel(&app),
)

app.Container.Singleton(
(*exceptionInterface.Handler)(nil),
exception.NewHandler(app),
exception.NewHandler(&app),
)

return app
return &app
}
24 changes: 12 additions & 12 deletions config/app.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package config

import (
"github.com/lanvard/support/environment/boo"
"github.com/lanvard/support/environment/str"
"github.com/lanvard/support/environment"
"golang.org/x/text/language"
"lanvard/config/entity"
"time"
Expand Down Expand Up @@ -34,19 +33,19 @@ var App = struct {
| any other location as required by the application or its packages.
|
*/
Name: str.EnvOr("APP_NAME", "Lanvard"),
Name: environment.StrEnvOr("APP_NAME", "Lanvard"),

/*
|--------------------------------------------------------------------------
| Application Environment
| Application IsEnvironment
|--------------------------------------------------------------------------
|
| This value determines the "environment" your application is currently
| running in. This may determine how you prefer to configure various
| services the application utilizes. Set this in your ".env" file.
|
*/
Env: str.EnvOr("APP_ENV", "production"),
Env: environment.StrEnvOr("APP_ENV", "production"),

/*
|--------------------------------------------------------------------------
Expand All @@ -58,7 +57,7 @@ var App = struct {
| application. If disabled, a simple generic error page is shown.
|
*/
Debug: boo.EnvOr("APP_DEBUG", false),
Debug: environment.BoolEnvOr("APP_DEBUG", false),

/*
|--------------------------------------------------------------------------
Expand All @@ -70,7 +69,7 @@ var App = struct {
| your application so that it is used when running Artisan tasks.
|
*/
Url: str.EnvOr("APP_URL", "http://localhost"),
Url: environment.StrEnvOr("APP_URL", "http://localhost"),

/*
|--------------------------------------------------------------------------
Expand All @@ -81,7 +80,7 @@ var App = struct {
| assets on an external service like Amazon S3.
|
*/
AssetUrl: str.EnvOr("ASSET_URL", "http://asset.localhost"),
AssetUrl: environment.StrEnvOr("ASSET_URL", "http://asset.localhost"),

/*
|--------------------------------------------------------------------------
Expand All @@ -91,14 +90,15 @@ var App = struct {
| Determine what the line separator should be for your application
|
*/
LineSeparator: str.EnvOr("LINE_SEPARATOR", "\n"),
LineSeparator: environment.StrEnvOr("LINE_SEPARATOR", "\n"),

/*
|--------------------------------------------------------------------------
| Base AppPath
|--------------------------------------------------------------------------
|
| The base path is the fully qualified path to the project root.
| The base path is the fully qualified path to the project root. Feel free
| to adjust this so that it fits to your needs.
|
*/
BasePath: entity.NewBasePath(),
Expand Down Expand Up @@ -156,12 +156,12 @@ var App = struct {
| Encryption Key
|--------------------------------------------------------------------------
|
| This key is used by the Illuminate encrypter service and should be set
| This key is used by the Lanvard encrypter service and should be set
| to a random, 32 character string, otherwise these encrypted strings
| will not be safe. Please do this before deploying an application!
|
*/
Key: str.Env("APP_KEY"),
Key: environment.StrEnv("APP_KEY"),

/*
|--------------------------------------------------------------------------
Expand Down
9 changes: 7 additions & 2 deletions config/entity/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ type BasePath string

func NewBasePath() BasePath {
_, filename, _, _ := runtime.Caller(0)
return BasePath(filepath.Dir(filepath.Dir(filename)))
return BasePath(filepath.Dir(filepath.Dir(filepath.Dir(filename))))
}

// Get the path to the application "app" directory.
func (basePath BasePath) AppPath() string {
return string(basePath) + pathSeparator + "app"
}

// Get the base path of the Laravel installation.
// Get the base path of the Lanvard installation.
func (basePath BasePath) BasePath() string {
return string(basePath)
}
Expand Down Expand Up @@ -64,3 +64,8 @@ func (basePath BasePath) ResourcePath() string {
func (basePath BasePath) EnvironmentFile() string {
return string(basePath) + pathSeparator + ".env"
}

// Get the path to the environment file for environment testing.
func (basePath BasePath) EnvironmentTestingFile() string {
return string(basePath) + pathSeparator + ".env.testing"
}
3 changes: 1 addition & 2 deletions config/generics/generics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
type Vars map[string]interface{}

type GenericAble interface {
AppPath() string
}

type Generic struct {
Expand All @@ -33,7 +32,7 @@ type Generic struct {
var Generics = []Generic{
{
Struct: pipeline.Pipeline{},
SaveTo: config.App.BasePath + "/src/app/http/middleware/pipeline.go",
SaveTo: string(config.App.BasePath) + "/app/http/middleware/pipeline.go",
Vars: Vars{
"Passable": (*http.Request)(nil),
"Result": (*http.Response)(nil),
Expand Down
Binary file modified main
Binary file not shown.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func handleKernel(response net.ResponseWriter, request *net.Request) {
|
*/
kernel := app.Make((*httpInterface.Kernel)(nil)).(foundation.Kernel)
appResponse := kernel.Handle(appHttp.NewRequest(app, *request))
appResponse := kernel.Handle(appHttp.NewRequest(&app, *request))

response.Write([]byte(appResponse.Content()))
// todo convert custom 'buffer' response to default go response
Expand Down
1 change: 1 addition & 0 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Because the lanvard/lanvard repository actually only contains configuration, the logic is mainly in the vendor modules. Are you looking for the right test, then search in the other respositories. Tests are also referred to in the documentation.
9 changes: 9 additions & 0 deletions test/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package test
import (
"github.com/lanvard/support"
"github.com/stretchr/testify/assert"
"lanvard/app/http/decorator"
"lanvard/bootstrap"
"lanvard/config"
"os"
"reflect"
"testing"
)
Expand All @@ -14,3 +16,10 @@ func Test_get_config(t *testing.T) {

assert.IsType(t, reflect.String, support.Type(config.App.Name))
}

func Test_get_environments_for_testing(t *testing.T) {
_ = decorator.Bootstrap(bootstrap.NewApp())

assert.Equal(t, "TestLanvard", os.Getenv("APP_NAME"))
assert.Equal(t, "TestLanvard", config.App.Name)
}
1 change: 0 additions & 1 deletion test/decorator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
)

func Test_bootstrap_environments(t *testing.T) {

_ = decorator.Bootstrap(bootstrap.NewApp())

assert.NotZero(t, os.Getenv("APP_ENV"))
Expand Down

0 comments on commit 7038085

Please sign in to comment.