transient http client
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.itzana.me/strafesnet/go-grpc/bots"
|
||||
"git.itzana.me/strafesnet/go-grpc/times"
|
||||
@@ -21,19 +22,17 @@ import (
|
||||
// TimesHandler handles HTTP requests related to times.
|
||||
type TimesHandler struct {
|
||||
*Handler
|
||||
client *http.Client
|
||||
url string
|
||||
}
|
||||
|
||||
// NewTimesHandler creates a new TimesHandler with the provided options.
|
||||
func NewTimesHandler(httpClient *http.Client, storageUrl string, options ...HandlerOption) (*TimesHandler, error) {
|
||||
func NewTimesHandler(storageUrl string, options ...HandlerOption) (*TimesHandler, error) {
|
||||
baseHandler, err := NewHandler(options...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &TimesHandler{
|
||||
Handler: baseHandler,
|
||||
client: httpClient,
|
||||
url: storageUrl,
|
||||
}, nil
|
||||
}
|
||||
@@ -437,7 +436,9 @@ func (h *TimesHandler) GetDownloadUrl(ctx *gin.Context) {
|
||||
}
|
||||
|
||||
// Send the request.
|
||||
resp, err := h.client.Do(req)
|
||||
resp, err := (&http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
}).Do(req)
|
||||
if err != nil {
|
||||
statusCode := http.StatusInternalServerError
|
||||
errorMessage := "Storage http request failed"
|
||||
|
||||
@@ -67,13 +67,6 @@ func WithStorageUrl(storageUrl string) Option {
|
||||
}
|
||||
}
|
||||
|
||||
// WithHttpClient sets the data http client
|
||||
func WithHttpClient(httpClient *http.Client) Option {
|
||||
return func(cfg *RouterConfig) {
|
||||
cfg.httpClient = httpClient
|
||||
}
|
||||
}
|
||||
|
||||
// WithShutdownTimeout sets the graceful shutdown timeout
|
||||
func WithShutdownTimeout(timeout time.Duration) Option {
|
||||
return func(cfg *RouterConfig) {
|
||||
@@ -92,7 +85,7 @@ func setupRoutes(cfg *RouterConfig) (*gin.Engine, error) {
|
||||
}
|
||||
|
||||
// Times handler
|
||||
timesHandler, err := handlers.NewTimesHandler(cfg.httpClient, cfg.storageUrl, handlerOptions...)
|
||||
timesHandler, err := handlers.NewTimesHandler(cfg.storageUrl, handlerOptions...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package cmds
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.itzana.me/strafesnet/public-api/pkg/api"
|
||||
"github.com/urfave/cli/v2"
|
||||
"google.golang.org/grpc"
|
||||
@@ -58,16 +56,12 @@ func runAPI(ctx *cli.Context) error {
|
||||
|
||||
// Storage service http client
|
||||
storageUrl := ctx.String("storage-host")
|
||||
httpClient := http.Client{
|
||||
Timeout: 10,
|
||||
}
|
||||
|
||||
return api.NewRouter(
|
||||
api.WithContext(ctx),
|
||||
api.WithPort(ctx.Int("port")),
|
||||
api.WithDevClient(devConn),
|
||||
api.WithDataClient(dataConn),
|
||||
api.WithHttpClient(&httpClient),
|
||||
api.WithStorageUrl(storageUrl),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user