Skip to content

Commit

Permalink
feat(minotaur): add hooks for HTTP server customization
Browse files Browse the repository at this point in the history
Extend the Http function in the minotaur transport network with hooks to allow
for additional customization of the HTTP server before it starts listening.This enhances the flexibility of the HTTP network setup without altering the
core functionality.
  • Loading branch information
kercylan98 committed Jun 17, 2024
1 parent 7d6abf8 commit b6334f6
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 8 deletions.
53 changes: 53 additions & 0 deletions example/internal/login/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module login

go 1.22.0

require github.com/kercylan98/minotaur v0.0.0

require (
github.com/RussellLuo/timingwheel v0.0.0-20220218152713-54845bda3108 // indirect
github.com/armon/go-metrics v0.4.1 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/gobwas/httphead v0.1.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect
github.com/gobwas/ws v1.4.0 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-msgpack/v2 v2.1.2 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-sockaddr v1.0.6 // indirect
github.com/hashicorp/golang-lru v1.0.2 // indirect
github.com/hashicorp/memberlist v0.5.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/klauspost/reedsolomon v1.12.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/miekg/dns v1.1.61 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/panjf2000/gnet/v2 v2.5.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/samber/do/v2 v2.0.0-beta.5 // indirect
github.com/samber/go-type-to-string v1.2.0 // indirect
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 // indirect
github.com/templexxx/cpu v0.1.0 // indirect
github.com/templexxx/xorsimd v0.4.2 // indirect
github.com/tjfoc/gmsm v1.4.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/xtaci/kcp-go/v5 v5.6.8 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/tools v0.22.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
)

replace github.com/kercylan98/minotaur v0.0.0 => ../../../../minotaur
21 changes: 21 additions & 0 deletions example/internal/login/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"github.com/kercylan98/minotaur/minotaur"
"github.com/kercylan98/minotaur/minotaur/transport/network"
"github.com/kercylan98/minotaur/minotaur/vivid"
"net/http"
)

func main() {
minotaur.NewApplication(
minotaur.WithNetwork(network.Http(":8080", func(handler *network.HttpServe) {
handler.HandleFunc("GET /login", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, World!"))
})
})),
minotaur.WithActorSystemName("login"),
).Launch(func(app *minotaur.Application, ctx vivid.MessageContext) {
app.GetServer()
})
}
4 changes: 0 additions & 4 deletions minotaur/cluster/node_message.go

This file was deleted.

2 changes: 1 addition & 1 deletion minotaur/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Options struct {
// defaultApply 设置缺省值
func (o *Options) defaultApply() *Options {
if o.ActorSystemName == "" {
o.ActorSystemName = "minotaur"
o.ActorSystemName = "app"
}
if o.EventBusActorName == "" {
o.EventBusActorName = "event_bus"
Expand Down
4 changes: 4 additions & 0 deletions minotaur/transport/network/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ type httpCore[H http.Handler] struct {
addr string
handler H
srv *http.Server
hook []func(handler H)
}

func (h *httpCore[H]) Launch(ctx context.Context, srv vivid.TypedActorRef[transport.ServerActorExpandTyped]) error {
for _, f := range h.hook {
f(h.handler)
}
return h.srv.ListenAndServe()
}

Expand Down
7 changes: 4 additions & 3 deletions minotaur/transport/network/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import (
)

// Http 创建一个基于 http.ServeMux 的 HTTP 的网络
func Http(addr string) transport.Network {
return HttpWithHandler(addr, &HttpServe{ServeMux: http.NewServeMux()})
func Http(addr string, hook ...func(handler *HttpServe)) transport.Network {
return HttpWithHandler(addr, &HttpServe{ServeMux: http.NewServeMux()}, hook...)
}

// HttpWithHandler 创建一个基于 http.Handler 的 HTTP 的网络
func HttpWithHandler[H http.Handler](addr string, handler H) transport.Network {
func HttpWithHandler[H http.Handler](addr string, handler H, hook ...func(handler H)) transport.Network {
c := &httpCore[H]{
addr: addr,
handler: handler,
hook: hook,
srv: &http.Server{
Addr: addr,
Handler: handler,
Expand Down

0 comments on commit b6334f6

Please sign in to comment.