Skip to content

Commit

Permalink
Merge pull request #98 from osspkg/fix-web-use-tag
Browse files Browse the repository at this point in the history
use tag for http server named
  • Loading branch information
markus621 authored Sep 28, 2024
2 parents d0366d7 + ac38ec9 commit 4d472d2
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 70 deletions.
6 changes: 2 additions & 4 deletions _example/basic/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ SHELL=/bin/bash
run:
go run main.go --config=config.yaml

show_metrics:
check:
time ab -n 1230 -c 5 http://127.0.0.1:10000/users
curl -v http://127.0.0.1:12000/metrics

ab_test:
time ab -n 1230 -c 5 http://127.0.0.1:10000/users
16 changes: 8 additions & 8 deletions _example/basic/config.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
env: dev

log:
file_path: /dev/stdout
format: string
level: 4
file_path: /dev/stdout
format: string
level: 4

http:
main:
addr: 0.0.0.0:10000
- tag: main
addr: 0.0.0.0:10000

metrics:
addr: 0.0.0.0:12000
gauge:
- users_request
addr: 0.0.0.0:12000
gauge:
- users_request

2 changes: 1 addition & 1 deletion _example/database/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ log:
level: 4

http:
main:
- tag: main
addr: 0.0.0.0:10000

mysql:
Expand Down
2 changes: 1 addition & 1 deletion _example/geoip/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ log:
level: 4

http:
main:
- tag: main
addr: 0.0.0.0:10000

geoip:
Expand Down
2 changes: 1 addition & 1 deletion _example/oauth/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ log:
level: 4

http:
main:
- tag: main
addr: 0.0.0.0:10000

oauth:
Expand Down
2 changes: 1 addition & 1 deletion _example/ws-server/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ log:
level: 4

http:
main:
- tag: main
addr: 0.0.0.0:10000
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ log:
level: 4 # 0-Fatal, 1-Error, 2-Warning, 3-Info, 4-Debug

http:
main:
- tag: main
addr: 0.0.0.0:10000
```
Expand Down
4 changes: 2 additions & 2 deletions metrics/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ type Server struct {

func New(app env.AppInfo, c Config, l logx.Logger) *Server {
router := web.NewBaseRouter()
conf := web.Config{Addr: c.Addr}
conf := web.Config{Addr: c.Addr, Tag: "metric"}
return &Server{
server: web.NewServer("Metrics", conf, router, l),
server: web.NewServer(conf, router, l),
route: router,
conf: c,
app: app,
Expand Down
9 changes: 5 additions & 4 deletions web/plugin_http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import (

// ConfigHttpPool config to initialize HTTP service
type ConfigHttpPool struct {
Config map[string]Config `yaml:"http"`
Config []Config `yaml:"http"`
}

func (v *ConfigHttpPool) Default() {
if v.Config == nil {
v.Config = map[string]Config{
"main": {Addr: "0.0.0.0:8080"},
}
v.Config = append(v.Config, Config{
Tag: "main",
Addr: "0.0.0.0:8080",
})
}
}

Expand Down
20 changes: 10 additions & 10 deletions web/plugin_http_server_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ type (
// Main method to get Main route handler
Main() Router
// Get method to get route handler by key
Get(name string) Router
Tag(name string) Router
}

routeProvider struct {
pool map[string]*routePoolItem
}
)

func newRouteProvider(configs map[string]Config, l logx.Logger) *routeProvider {
func newRouteProvider(configs []Config, l logx.Logger) *routeProvider {
v := &routeProvider{
pool: make(map[string]*routePoolItem),
}
for name, config := range configs {
v.pool[name] = &routePoolItem{
for _, config := range configs {
v.pool[config.Tag] = &routePoolItem{
active: false,
route: newRouter(name, config, l),
route: newRouter(config.Tag, config, l),
}
}
return v
Expand All @@ -59,16 +59,16 @@ func (v *routeProvider) All(call func(name string, router Router)) {

// Main method to get Main route handler
func (v *routeProvider) Main() Router {
return v.Get("main")
return v.Tag("main")
}

// Admin method to get Admin route handler
func (v *routeProvider) Admin() Router {
return v.Get("admin")
return v.Tag("admin")
}

// Get method to get route handler by key
func (v *routeProvider) Get(name string) Router {
// Tag method to get route handler by tag
func (v *routeProvider) Tag(name string) Router {
if r, ok := v.pool[name]; ok {
return r.route
}
Expand Down Expand Up @@ -137,7 +137,7 @@ func newRouter(name string, c Config, l logx.Logger) *route {
}

func (v *route) Up(c xc.Context) error {
v.serv = NewServer(v.name, v.config, v.route, v.log)
v.serv = NewServer(v.config, v.route, v.log)
return v.serv.Up(c)
}
func (v *route) Down() error {
Expand Down
73 changes: 36 additions & 37 deletions web/server_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

type (
Config struct {
Tag string `yaml:"tag"`
Addr string `yaml:"addr"`
Network string `yaml:"network,omitempty"`
ReadTimeout time.Duration `yaml:"read_timeout,omitempty"`
Expand All @@ -29,7 +30,6 @@ type (
}

Server struct {
name string
conf Config
serv *http.Server
handler http.Handler
Expand All @@ -40,9 +40,8 @@ type (
)

// NewServer create default http server
func NewServer(name string, conf Config, handler http.Handler, l logx.Logger) *Server {
func NewServer(conf Config, handler http.Handler, l logx.Logger) *Server {
srv := &Server{
name: name,
conf: conf,
handler: handler,
log: l,
Expand All @@ -53,67 +52,67 @@ func NewServer(name string, conf Config, handler http.Handler, l logx.Logger) *S
return srv
}

func (s *Server) validate() {
if s.conf.ReadTimeout == 0 {
s.conf.ReadTimeout = defaultTimeout
func (v *Server) validate() {
if v.conf.ReadTimeout == 0 {
v.conf.ReadTimeout = defaultTimeout
}
if s.conf.WriteTimeout == 0 {
s.conf.WriteTimeout = defaultTimeout
if v.conf.WriteTimeout == 0 {
v.conf.WriteTimeout = defaultTimeout
}
if s.conf.IdleTimeout == 0 {
s.conf.IdleTimeout = defaultTimeout
if v.conf.IdleTimeout == 0 {
v.conf.IdleTimeout = defaultTimeout
}
if s.conf.ShutdownTimeout == 0 {
s.conf.ShutdownTimeout = defaultShutdownTimeout
if v.conf.ShutdownTimeout == 0 {
v.conf.ShutdownTimeout = defaultShutdownTimeout
}
if len(s.conf.Network) == 0 {
s.conf.Network = defaultNetwork
if len(v.conf.Network) == 0 {
v.conf.Network = defaultNetwork
}
if _, ok := networkType[s.conf.Network]; !ok {
s.conf.Network = defaultNetwork
if _, ok := networkType[v.conf.Network]; !ok {
v.conf.Network = defaultNetwork
}
s.conf.Addr = address.CheckHostPort(s.conf.Addr)
v.conf.Addr = address.CheckHostPort(v.conf.Addr)
}

// Up start http server
func (s *Server) Up(ctx xc.Context) error {
if !s.sync.On() {
return errors.Wrapf(errServAlreadyRunning, "starting server on %s", s.conf.Addr)
func (v *Server) Up(ctx xc.Context) error {
if !v.sync.On() {
return errors.Wrapf(errServAlreadyRunning, "starting server on %s", v.conf.Addr)
}
s.serv = &http.Server{
ReadTimeout: s.conf.ReadTimeout,
WriteTimeout: s.conf.WriteTimeout,
IdleTimeout: s.conf.IdleTimeout,
Handler: s.handler,
v.serv = &http.Server{
ReadTimeout: v.conf.ReadTimeout,
WriteTimeout: v.conf.WriteTimeout,
IdleTimeout: v.conf.IdleTimeout,
Handler: v.handler,
}

nl, err := net.Listen(s.conf.Network, s.conf.Addr)
nl, err := net.Listen(v.conf.Network, v.conf.Addr)
if err != nil {
return err
}

s.log.Info("Http server started", "name", s.name, "ip", s.conf.Addr)
v.log.Info("Http server started", "tag", v.conf.Tag, "ip", v.conf.Addr)

s.wg.Background(func() {
v.wg.Background(func() {
defer ctx.Close()
if err = s.serv.Serve(nl); err != nil && !errors.Is(err, http.ErrServerClosed) {
s.log.Error("Http server stopped", "name", s.name, "ip", s.conf.Addr, "err", err)
if err = v.serv.Serve(nl); err != nil && !errors.Is(err, http.ErrServerClosed) {
v.log.Error("Http server stopped", "tag", v.conf.Tag, "ip", v.conf.Addr, "err", err)
return
}

s.log.Info("Http server stopped", "name", s.name, "ip", s.conf.Addr)
v.log.Info("Http server stopped", "tag", v.conf.Tag, "ip", v.conf.Addr)
})
return nil
}

// Down stop http server
func (s *Server) Down() error {
if !s.sync.Off() {
return errors.Wrapf(errServAlreadyStopped, "stopping server on %s", s.conf.Addr)
func (v *Server) Down() error {
if !v.sync.Off() {
return errors.Wrapf(errServAlreadyStopped, "stopping server on %s", v.conf.Addr)
}
ctx, cncl := context.WithTimeout(context.Background(), s.conf.ShutdownTimeout)
ctx, cncl := context.WithTimeout(context.Background(), v.conf.ShutdownTimeout)
defer cncl()
err := s.serv.Shutdown(ctx)
s.wg.Wait()
err := v.serv.Shutdown(ctx)
v.wg.Wait()
return err
}

0 comments on commit 4d472d2

Please sign in to comment.