Skip to content

Commit

Permalink
#14: Added the router struct
Browse files Browse the repository at this point in the history
  • Loading branch information
roma-glushko committed Dec 21, 2023
1 parent d17a295 commit baabc11
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
17 changes: 13 additions & 4 deletions pkg/api/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@ import (
"fmt"
"time"

"glide/pkg/pools"
"glide/pkg/telemetry"

"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/common/utils"
"github.com/cloudwego/hertz/pkg/protocol/consts"
)

type Server struct {
server *server.Hertz
telemetry *telemetry.Telemetry
router *pools.Router
server *server.Hertz
}

func NewServer(config *ServerConfig) (*Server, error) {
func NewServer(config *ServerConfig, tel *telemetry.Telemetry, router *pools.Router) (*Server, error) {
return &Server{
server: config.ToServer(),
telemetry: tel,
router: router,
server: config.ToServer(),
}, nil
}

Expand All @@ -32,7 +39,9 @@ func (srv *Server) Run() error {
func (srv *Server) Shutdown(_ context.Context) error {
exitWaitTime := srv.server.GetOptions().ExitWaitTimeout

println(fmt.Sprintf("Begin graceful shutdown, wait at most %d seconds...", exitWaitTime/time.Second))
srv.telemetry.Logger().Info(
fmt.Sprintf("Begin graceful shutdown, wait at most %d seconds...", exitWaitTime/time.Second),
)

ctx, cancel := context.WithTimeout(context.Background(), exitWaitTime)
defer cancel()
Expand Down
10 changes: 7 additions & 3 deletions pkg/api/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"context"
"sync"

"glide/pkg/pools"
"glide/pkg/telemetry"

"glide/pkg/api/http"
)

Expand All @@ -12,13 +15,14 @@ type ServerManager struct {
shutdownWG *sync.WaitGroup
}

func NewServerManager(httpConfig *http.ServerConfig) (*ServerManager, error) {
httpServer, err := http.NewServer(httpConfig)
// TODO: init other servers like gRPC in future
func NewServerManager(httpConfig *http.ServerConfig, tel *telemetry.Telemetry, router *pools.Router) (*ServerManager, error) {
httpServer, err := http.NewServer(httpConfig, tel, router)
if err != nil {
return nil, err
}

// TODO: init other servers like gRPC in future

return &ServerManager{
httpServer: httpServer,
shutdownWG: &sync.WaitGroup{},
Expand Down
9 changes: 8 additions & 1 deletion pkg/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os/signal"
"syscall"

"glide/pkg/pools"

"glide/pkg/telemetry"
"go.uber.org/zap"

Expand Down Expand Up @@ -40,7 +42,12 @@ func NewGateway() (*Gateway, error) {
return nil, err
}

serverManager, err := api.NewServerManager(&http.ServerConfig{})
router, err := pools.NewRouter(tel)
if err != nil {
return nil, err
}

serverManager, err := api.NewServerManager(&http.ServerConfig{}, tel, router)
if err != nil {
return nil, err
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/pools/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package pools

import "glide/pkg/telemetry"

type Router struct {
telemetry *telemetry.Telemetry
}

func NewRouter(tel *telemetry.Telemetry) (*Router, error) {
return &Router{
telemetry: tel,
}, nil
}

0 comments on commit baabc11

Please sign in to comment.