From 64d1ab03f77b20e274fc0aa1d01a23e6d3b72c70 Mon Sep 17 00:00:00 2001 From: Reinaldo Oliveira Date: Mon, 15 May 2023 13:15:34 -0300 Subject: [PATCH 1/2] Add post build hooks --- builder.go | 17 ++++++++++++++- builder_test.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 builder_test.go diff --git a/builder.go b/builder.go index 96906673..3d96ff6e 100644 --- a/builder.go +++ b/builder.go @@ -25,6 +25,7 @@ import ( // Builder holds dependency instances for a pitaya App type Builder struct { acceptors []acceptor.Acceptor + postBuildHooks []func(app Pitaya) Config config.BuilderConfig DieChan chan bool PacketDecoder codec.PacketDecoder @@ -46,6 +47,8 @@ type Builder struct { // PitayaBuilder Builder interface type PitayaBuilder interface { + // AddPostBuildHook adds a post-build hook to the builder, a function receiving a Pitaya instance as parameter. + AddPostBuildHook(hook func(app Pitaya)) Build() Pitaya } @@ -185,6 +188,7 @@ func NewBuilder(isFrontend bool, return &Builder{ acceptors: []acceptor.Acceptor{}, + postBuildHooks: make([]func(app Pitaya), 0), Config: config, DieChan: dieChan, PacketDecoder: codec.NewPomeloPacketDecoder(), @@ -214,6 +218,11 @@ func (builder *Builder) AddAcceptor(ac acceptor.Acceptor) { builder.acceptors = append(builder.acceptors, ac) } +// AddPostBuildHook adds a post-build hook to the builder, a function receiving a Pitaya instance as parameter. +func (builder *Builder) AddPostBuildHook(hook func(app Pitaya)) { + builder.postBuildHooks = append(builder.postBuildHooks, hook) +} + // Build returns a valid App instance func (builder *Builder) Build() Pitaya { handlerPool := service.NewHandlerPool() @@ -270,7 +279,7 @@ func (builder *Builder) Build() Pitaya { handlerPool, ) - return NewApp( + app := NewApp( builder.ServerMode, builder.Serializer, builder.acceptors, @@ -288,6 +297,12 @@ func (builder *Builder) Build() Pitaya { builder.MetricsReporters, builder.Config.Pitaya, ) + + for _, postBuildHook := range builder.postBuildHooks { + postBuildHook(app) + } + + return app } // NewDefaultApp returns a default pitaya app instance diff --git a/builder_test.go b/builder_test.go new file mode 100644 index 00000000..da07c097 --- /dev/null +++ b/builder_test.go @@ -0,0 +1,58 @@ +// Copyright (c) nano Author and TFG Co. All Rights Reserved. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +package pitaya + +import ( + "github.com/stretchr/testify/assert" + "github.com/topfreegames/pitaya/v2/acceptor" + "github.com/topfreegames/pitaya/v2/config" + "testing" +) + +func TestPostBuildHooks(t *testing.T) { + acc := acceptor.NewTCPAcceptor("0.0.0.0:0") + for _, table := range tables { + builderConfig := config.NewDefaultBuilderConfig() + + t.Run("with_post_build_hooks", func(t *testing.T) { + called := false + builder := NewDefaultBuilder(table.isFrontend, table.serverType, table.serverMode, table.serverMetadata, *builderConfig) + builder.AddAcceptor(acc) + builder.AddPostBuildHook(func(app Pitaya) { + called = true + }) + app := builder.Build() + + assert.True(t, called) + assert.NotNil(t, app) + }) + + t.Run("without_post_build_hooks", func(t *testing.T) { + called := false + builder := NewDefaultBuilder(table.isFrontend, table.serverType, table.serverMode, table.serverMetadata, *builderConfig) + builder.AddAcceptor(acc) + app := builder.Build() + + assert.False(t, called) + assert.NotNil(t, app) + }) + } +} From d2ba92b0111678908b12e9158c9255bf30fa971f Mon Sep 17 00:00:00 2001 From: Reinaldo Oliveira Date: Wed, 24 May 2023 09:47:36 -0300 Subject: [PATCH 2/2] Add documentation about the post build hooks --- docs/builder.md | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 docs/builder.md diff --git a/docs/builder.md b/docs/builder.md new file mode 100644 index 00000000..a3de1cbb --- /dev/null +++ b/docs/builder.md @@ -0,0 +1,41 @@ +Builder +=== + +Pitaya offers a [`Builder`](../builder.go) object which can be utilized to define a sort of pitaya properties. + +### PostBuildHooks + +Post-build hooks can be used to execute additional actions automatically after the build process. It also allows you to interact with the built pitaya app. + +A common use case is where it becomes necessary to perform configuration steps in both the pitaya builder and the pitaya app being built. In such cases, an effective approach is to internalize these configurations, enabling you to handle them collectively in a single operation or process. It simplifies the overall configuration process, reducing the need for separate and potentially repetitive steps. + +```go +// main.go +cfg := config.NewDefaultBuilderConfig() +builder := pitaya.NewDefaultBuilder(isFrontEnd, "my-server-type", pitaya.Cluster, map[string]string{}, *cfg) + +customModule := NewCustomModule(builder) +customModule.ConfigurePitaya(builder) + +app := builder.Build() + +// custom_object.go +type CustomObject struct { + builder *pitaya.Builder +} + +func NewCustomObject(builder *pitaya.Builder) *CustomObject { + return &CustomObject{ + builder: builder, + } +} + +func (object *CustomObject) ConfigurePitaya() { + object.builder.AddAcceptor(...) + object.builder.AddPostBuildHook(func (app pitaya.App) { + app.Register(...) + }) +} +``` + +In the above example the `ConfigurePitaya` method of the `CustomObject` is adding an `Acceptor` to the pitaya app being built, and also adding a post-build function which will register a handler `Component` that will expose endpoints to receive calls.