Deploy Bots API #28
@@ -2,9 +2,10 @@ package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"google.golang.org/grpc"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -14,6 +15,7 @@ const (
|
||||
// Handler is a base handler that provides common functionality for all HTTP handlers.
|
||||
type Handler struct {
|
||||
dataClient *grpc.ClientConn
|
||||
storageUrl string
|
||||
}
|
||||
|
||||
// HandlerOption defines a functional option for configuring a Handler
|
||||
@@ -26,6 +28,13 @@ func WithDataClient(dataClient *grpc.ClientConn) HandlerOption {
|
||||
}
|
||||
}
|
||||
|
||||
// WithStorageUrl sets the storage url
|
||||
func WithStorageUrl(storageUrl string) HandlerOption {
|
||||
return func(cfg *Handler) {
|
||||
cfg.storageUrl = storageUrl
|
||||
}
|
||||
}
|
||||
|
||||
// NewHandler creates a new Handler with the provided options.
|
||||
// It requires both a datastore and an authentication service to function properly.
|
||||
func NewHandler(options ...HandlerOption) (*Handler, error) {
|
||||
|
||||
@@ -22,18 +22,16 @@ import (
|
||||
// TimesHandler handles HTTP requests related to times.
|
||||
type TimesHandler struct {
|
||||
*Handler
|
||||
url string
|
||||
}
|
||||
|
||||
// NewTimesHandler creates a new TimesHandler with the provided options.
|
||||
func NewTimesHandler(storageUrl string, options ...HandlerOption) (*TimesHandler, error) {
|
||||
func NewTimesHandler(options ...HandlerOption) (*TimesHandler, error) {
|
||||
baseHandler, err := NewHandler(options...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &TimesHandler{
|
||||
Handler: baseHandler,
|
||||
url: storageUrl,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -406,7 +404,7 @@ func (h *TimesHandler) GetDownloadUrl(ctx *gin.Context) {
|
||||
|
||||
// fetch download url from storage service
|
||||
// Build the full URL.
|
||||
fullURL, err := url.JoinPath(h.url, botData.FileID)
|
||||
fullURL, err := url.JoinPath(h.storageUrl, botData.FileID)
|
||||
if err != nil {
|
||||
statusCode := http.StatusInternalServerError
|
||||
errorMessage := "Error joining Url"
|
||||
|
||||
@@ -85,7 +85,10 @@ func setupRoutes(cfg *RouterConfig) (*gin.Engine, error) {
|
||||
}
|
||||
|
||||
// Times handler
|
||||
timesHandler, err := handlers.NewTimesHandler(cfg.storageUrl, handlerOptions...)
|
||||
timesHandler, err := handlers.NewTimesHandler([]handlers.HandlerOption{
|
||||
handlers.WithDataClient(cfg.dataClient),
|
||||
handlers.WithStorageUrl(cfg.storageUrl),
|
||||
}...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user