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