-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: server 包新增 service 模式的加载函数 server.BindService
- Loading branch information
1 parent
6bf9c5e
commit bdf4a23
Showing
3 changed files
with
74 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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,37 @@ | ||
package server | ||
|
||
import ( | ||
"github.com/kercylan98/minotaur/utils/log" | ||
"reflect" | ||
) | ||
|
||
// Service 兼容传统 service 设计模式的接口 | ||
type Service interface { | ||
// OnInit 初始化服务,该方法将会在 Server 初始化时执行 | ||
// - 通常来说,该阶段发生任何错误都应该 panic 以阻止 Server 启动 | ||
OnInit(srv *Server) | ||
} | ||
|
||
// BindService 绑定服务到特定 Server,被绑定的服务将会在 Server 初始化时执行 Service.OnInit 方法 | ||
func BindService(srv *Server, services ...Service) { | ||
for _, service := range services { | ||
srv.services = append(srv.services, func() { | ||
name := reflect.TypeOf(service).String() | ||
defer func(name string) { | ||
if err := recover(); err != nil { | ||
log.Error("Server", log.String("service", name), log.String("status", "initialization"), log.Any("err", err)) | ||
panic(err) | ||
} | ||
}(name) | ||
service.OnInit(srv) | ||
log.Info("Server", log.String("service", name), log.String("status", "initialized")) | ||
}) | ||
} | ||
} | ||
|
||
// onServicesInit 服务初始化 | ||
func onServicesInit(srv *Server) { | ||
for _, service := range srv.services { | ||
service() | ||
} | ||
} |
This file contains 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,30 @@ | ||
package server_test | ||
|
||
import ( | ||
"github.com/kercylan98/minotaur/server" | ||
"testing" | ||
"time" | ||
) | ||
|
||
type TestService struct { | ||
} | ||
|
||
func (ts *TestService) OnInit(srv *server.Server) { | ||
srv.RegStartFinishEvent(func(srv *server.Server) { | ||
println("Server started") | ||
}) | ||
|
||
srv.RegStopEvent(func(srv *server.Server) { | ||
println("Server stopped") | ||
}) | ||
} | ||
|
||
func TestBindService(t *testing.T) { | ||
srv := server.New(server.NetworkNone, server.WithLimitLife(time.Second)) | ||
|
||
server.BindService(srv, new(TestService)) | ||
|
||
if err := srv.RunNone(); err != nil { | ||
panic(err) | ||
} | ||
} |