-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
add ghttp server start hooks #4474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wangle201210
wants to merge
4
commits into
gogf:master
Choose a base branch
from
wangle201210:feat/server_event
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. | ||
| // | ||
| // This Source Code Form is subject to the terms of the MIT License. | ||
| // If a copy of the MIT was not distributed with this file, | ||
| // You can obtain one at https://github.com/gogf/gf. | ||
|
|
||
| package ghttp_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/gogf/gf/v2/frame/g" | ||
| "github.com/gogf/gf/v2/net/ghttp" | ||
| ) | ||
|
|
||
| // ExampleServer_SetBeforeStart demonstrates how to use the SetBeforeStart hook. | ||
| // This hook is executed before the server starts listening. | ||
| func ExampleServer_SetBeforeStart() { | ||
| s := g.Server() | ||
| s.BindHandler("/", func(r *ghttp.Request) { | ||
| r.Response.Write("Hello World") | ||
| }) | ||
|
|
||
| // Set a hook that executes before the server starts | ||
| s.SetBeforeStart(func(s *ghttp.Server) error { | ||
| fmt.Println("Server is preparing to start...") | ||
| // Perform initialization tasks here, such as: | ||
| // - Loading configuration | ||
| // - Connecting to databases | ||
| // - Initializing caches | ||
| // - Validating required resources | ||
| return nil | ||
| }) | ||
|
|
||
| // If any before-start hook returns an error, the server will not start | ||
| s.SetBeforeStart(func(s *ghttp.Server) error { | ||
| // Return error to prevent server from starting | ||
| // return fmt.Errorf("initialization failed") | ||
| return nil | ||
| }) | ||
|
|
||
| // Start the server | ||
| // s.Run() | ||
| } | ||
|
|
||
| // ExampleServer_SetAfterStart demonstrates how to use the SetAfterStart hook. | ||
| // This hook is executed after the server has successfully started listening. | ||
| func ExampleServer_SetAfterStart() { | ||
| s := g.Server() | ||
| s.BindHandler("/", func(r *ghttp.Request) { | ||
| r.Response.Write("Hello World") | ||
| }) | ||
|
|
||
| // Set a hook that executes after the server starts | ||
| s.SetAfterStart(func(s *ghttp.Server) error { | ||
| port := s.GetListenedPort() | ||
| fmt.Printf("Server started successfully on port %d\n", port) | ||
| // Perform post-startup tasks here, such as: | ||
| // - Registering with service discovery | ||
| // - Starting background jobs | ||
| // - Sending startup notifications | ||
| // - Logging server information | ||
| return nil | ||
| }) | ||
|
|
||
| // Multiple after-start hooks can be registered | ||
| s.SetAfterStart(func(s *ghttp.Server) error { | ||
| fmt.Println("Performing additional startup tasks...") | ||
| return nil | ||
| }) | ||
|
|
||
| // If an after-start hook returns an error, it will be logged | ||
| // but the server will continue running | ||
| s.SetAfterStart(func(s *ghttp.Server) error { | ||
| // This error will be logged but won't stop the server | ||
| // return fmt.Errorf("non-critical error") | ||
| return nil | ||
| }) | ||
|
|
||
| // Start the server | ||
| // s.Run() | ||
| } | ||
|
|
||
| // Example_lifecycleHooks demonstrates a complete example | ||
| // using both before-start and after-start hooks together. | ||
| func Example_lifecycleHooks() { | ||
| s := g.Server() | ||
| s.SetAddr(":8080") | ||
|
|
||
| // Register routes | ||
| s.BindHandler("/health", func(r *ghttp.Request) { | ||
| r.Response.WriteJson(g.Map{ | ||
| "status": "healthy", | ||
| }) | ||
| }) | ||
|
|
||
| // Before-start hook: validate configuration | ||
| s.SetBeforeStart(func(s *ghttp.Server) error { | ||
| fmt.Println("1. Validating server configuration...") | ||
| // Validate required configuration here | ||
| return nil | ||
| }) | ||
|
|
||
| // Before-start hook: initialize resources | ||
| s.SetBeforeStart(func(s *ghttp.Server) error { | ||
| fmt.Println("2. Initializing database connections...") | ||
| // Initialize database connections here | ||
| return nil | ||
| }) | ||
|
|
||
| // After-start hook: register with service discovery | ||
| s.SetAfterStart(func(s *ghttp.Server) error { | ||
| port := s.GetListenedPort() | ||
| fmt.Printf("3. Registering service on port %d with discovery...\n", port) | ||
| // Register with service discovery here | ||
| return nil | ||
| }) | ||
|
|
||
| // After-start hook: log startup information | ||
| s.SetAfterStart(func(s *ghttp.Server) error { | ||
| fmt.Println("4. Server is ready to accept requests") | ||
| return nil | ||
| }) | ||
|
|
||
| // Start the server | ||
| // s.Run() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. | ||
| // | ||
| // This Source Code Form is subject to the terms of the MIT License. | ||
| // If a copy of the MIT was not distributed with this file, | ||
| // You can obtain one at https://github.com/gogf/gf. | ||
|
|
||
| package ghttp | ||
|
|
||
| import "github.com/gogf/gf/v2/os/gctx" | ||
|
|
||
| // SetBeforeStart sets the hook function that is called before the server starts. | ||
| // Multiple hooks can be registered and they will be executed in the order they were registered. | ||
| // If any hook returns an error, the server will not start and the error will be returned. | ||
| // | ||
| // Example: | ||
| // | ||
| // s := ghttp.GetServer() | ||
| // s.SetBeforeStart(func(s *ghttp.Server) error { | ||
| // fmt.Println("Server is about to start") | ||
| // return nil | ||
| // }) | ||
| func (s *Server) SetBeforeStart(hook ServerHookFunc) { | ||
| s.beforeStartHooks = append(s.beforeStartHooks, hook) | ||
| } | ||
|
|
||
| // SetAfterStart sets the hook function that is called after the server starts successfully. | ||
| // Multiple hooks can be registered and they will be executed in the order they were registered. | ||
| // The hooks are executed after all server listeners have been created and started. | ||
| // If any hook returns an error, the error will be logged but the server will continue running. | ||
| // | ||
| // Example: | ||
| // | ||
| // s := ghttp.GetServer() | ||
| // s.SetAfterStart(func(s *ghttp.Server) error { | ||
| // fmt.Println("Server started successfully") | ||
| // return nil | ||
| // }) | ||
| func (s *Server) SetAfterStart(hook ServerHookFunc) { | ||
| s.afterStartHooks = append(s.afterStartHooks, hook) | ||
| } | ||
|
|
||
| // executeBeforeStartHooks executes all registered before-start hooks. | ||
| // It returns an error if any hook fails, which will prevent the server from starting. | ||
| func (s *Server) executeBeforeStartHooks() error { | ||
| for _, hook := range s.beforeStartHooks { | ||
| if err := hook(s); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // executeAfterStartHooks executes all registered after-start hooks. | ||
| // Errors from hooks are logged but do not stop the server. | ||
| func (s *Server) executeAfterStartHooks() { | ||
| var ctx = gctx.GetInitCtx() | ||
| for _, hook := range s.afterStartHooks { | ||
| if err := hook(s); err != nil { | ||
| s.Logger().Errorf(ctx, `after-start hook error: %+v`, err) | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.