add auth service and check token fro widget module
This commit is contained in:
parent
1469917c24
commit
6f2ca61d91
@ -52,7 +52,7 @@ func main() {
|
|||||||
targetRepo := TargetRepo.New(db)
|
targetRepo := TargetRepo.New(db)
|
||||||
|
|
||||||
// SERVICES
|
// SERVICES
|
||||||
widgetService := WidgetService.New(widgetRepo)
|
widgetService := WidgetService.New(widgetRepo, authClient)
|
||||||
donatService := DonatService.New(donatRepo, widgetRepo, paymentClient, authClient)
|
donatService := DonatService.New(donatRepo, widgetRepo, paymentClient, authClient)
|
||||||
targetService := TargetService.New(targetRepo, authClient)
|
targetService := TargetService.New(targetRepo, authClient)
|
||||||
|
|
||||||
|
@ -1,226 +1,209 @@
|
|||||||
//package widget
|
package widget
|
||||||
//
|
|
||||||
//import (
|
import (
|
||||||
// "context"
|
"context"
|
||||||
// "donat-widget/internal/model"
|
"donat-widget/internal/model"
|
||||||
// "github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
// "log/slog"
|
"log/slog"
|
||||||
// "net/http"
|
"net/http"
|
||||||
// "strconv"
|
"strconv"
|
||||||
//)
|
)
|
||||||
//
|
|
||||||
//func CreateWidget(widgetService model.WidgetService, authClient model.AuthClient) echo.HandlerFunc {
|
func CreateWidget(widgetService model.WidgetService, authClient model.AuthClient) echo.HandlerFunc {
|
||||||
// type CreateWidgetBody struct {
|
return func(request echo.Context) error {
|
||||||
// TemplateID model.TemplateID `json:"templateID"`
|
ctx := context.Background()
|
||||||
// MinAmount model.DonatAmount `json:"minAmount"`
|
var body model.CreateWidgetBody
|
||||||
// Duration model.Duration `json:"duration"`
|
if err := request.Bind(&body); err != nil {
|
||||||
// }
|
slog.Error(err.Error())
|
||||||
//
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
||||||
// type CreateWidgetResponse struct {
|
}
|
||||||
// WidgetID model.WidgetID `json:"widgetID"`
|
if err := request.Validate(&body); err != nil {
|
||||||
// }
|
slog.Error(err.Error())
|
||||||
// return func(request echo.Context) error {
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
||||||
// ctx := context.Background()
|
}
|
||||||
// var body CreateWidgetBody
|
|
||||||
// if err := request.Bind(&body); err != nil {
|
authData, err := widgetService.CheckToken(request)
|
||||||
// slog.Error(err.Error())
|
if err != nil {
|
||||||
// return request.JSON(http.StatusInternalServerError, err.Error())
|
slog.Error(err.Error())
|
||||||
// }
|
return echo.NewHTTPError(http.StatusUnauthorized, err.Error())
|
||||||
// if err := request.Validate(&body); err != nil {
|
}
|
||||||
// slog.Error(err.Error())
|
|
||||||
// return request.JSON(http.StatusInternalServerError, err.Error())
|
widgetID, err := widgetService.CreateWidget(
|
||||||
// }
|
ctx,
|
||||||
//
|
model.StreamerID(authData.AccountID),
|
||||||
// authData, err := authClient.CheckToken(request)
|
body.TemplateID,
|
||||||
// if err != nil {
|
body.Duration,
|
||||||
// slog.Error(err.Error())
|
body.MinAmount,
|
||||||
// return request.JSON(http.StatusInternalServerError, err.Error())
|
)
|
||||||
// }
|
if err != nil {
|
||||||
//
|
slog.Error(err.Error())
|
||||||
// if authData.StreamerID == 0 {
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
||||||
// slog.Error("Unauthorized account")
|
}
|
||||||
// return request.JSON(http.StatusUnauthorized, "Unauthorized")
|
|
||||||
// }
|
response := model.CreateWidgetResponse{
|
||||||
// if authData.StreamerID == -1 {
|
WidgetID: widgetID,
|
||||||
// slog.Error("Expired token")
|
}
|
||||||
// return request.JSON(http.StatusUnauthorized, "Expired token")
|
|
||||||
// }
|
return request.JSON(http.StatusOK, response)
|
||||||
//
|
}
|
||||||
// widgetID, err := widgetService.CreateWidget(
|
}
|
||||||
// ctx,
|
|
||||||
// model.StreamerID(authData.StreamerID),
|
func GetWidgetHTML(widgetService model.WidgetService) echo.HandlerFunc {
|
||||||
// body.TemplateID,
|
return func(request echo.Context) error {
|
||||||
// body.Duration,
|
ctx := context.Background()
|
||||||
// body.MinAmount,
|
|
||||||
// )
|
streamerID, err := strconv.Atoi(request.Param("streamerID"))
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// slog.Error(err.Error())
|
slog.Error(err.Error())
|
||||||
// return request.JSON(http.StatusInternalServerError, err.Error())
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// response := CreateWidgetResponse{
|
widgetHTML, err := widgetService.GetWidgetHTML(
|
||||||
// WidgetID: widgetID,
|
ctx,
|
||||||
// }
|
model.StreamerID(streamerID),
|
||||||
//
|
)
|
||||||
// return request.JSON(200, response)
|
if err != nil {
|
||||||
// }
|
slog.Error(err.Error())
|
||||||
//}
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
||||||
//
|
}
|
||||||
//func GetWidgetHTML(widgetService model.WidgetService) echo.HandlerFunc {
|
|
||||||
// return func(request echo.Context) error {
|
slog.Info("Get widget HTML successfully")
|
||||||
// ctx := context.Background()
|
return request.HTML(200, string(widgetHTML))
|
||||||
//
|
}
|
||||||
// streamerID, err := strconv.Atoi(request.Param("streamerID"))
|
}
|
||||||
// if err != nil {
|
|
||||||
// slog.Error(err.Error())
|
func GetWidgetInfo(widgetService model.WidgetService) echo.HandlerFunc {
|
||||||
// return request.JSON(http.StatusInternalServerError, err.Error())
|
return func(request echo.Context) error {
|
||||||
// }
|
ctx := context.Background()
|
||||||
//
|
|
||||||
// widgetHTML, err := widgetService.GetWidgetHTML(
|
widgetID, err := strconv.Atoi(request.Param("widgetID"))
|
||||||
// ctx,
|
if err != nil {
|
||||||
// model.StreamerID(streamerID),
|
slog.Error(err.Error())
|
||||||
// )
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
||||||
// if err != nil {
|
}
|
||||||
// slog.Error(err.Error())
|
|
||||||
// return request.JSON(http.StatusInternalServerError, err.Error())
|
widget, err := widgetService.GetWidgetByID(ctx, model.WidgetID(widgetID))
|
||||||
// }
|
if err != nil {
|
||||||
//
|
slog.Error(err.Error())
|
||||||
// slog.Info("Get widget HTML successfully")
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
||||||
// return request.HTML(200, string(widgetHTML))
|
}
|
||||||
// }
|
if len(widget) == 0 {
|
||||||
//}
|
return request.JSON(http.StatusNotFound, "widget not found")
|
||||||
//
|
}
|
||||||
//func GetWidgetInfo(widgetService model.WidgetService) echo.HandlerFunc {
|
|
||||||
// type Response struct {
|
var ResponseWidget = model.WidgetInfoResponse{
|
||||||
// AudioUrl model.MediaUrl `json:"audioUrl"`
|
AudioUrl: widget[0].AudioUrl,
|
||||||
// ImageUrl model.MediaUrl `json:"imageUrl"`
|
ImageUrl: widget[0].ImageUrl,
|
||||||
// Duration model.Duration `json:"duration"`
|
Duration: widget[0].Duration,
|
||||||
// }
|
}
|
||||||
// return func(request echo.Context) error {
|
|
||||||
// ctx := context.Background()
|
return request.JSON(http.StatusOK, ResponseWidget)
|
||||||
//
|
}
|
||||||
// widgetID, err := strconv.Atoi(request.Param("widgetID"))
|
}
|
||||||
// if err != nil {
|
|
||||||
// slog.Error(err.Error())
|
func SetMediaFile(widgetService model.WidgetService) echo.HandlerFunc {
|
||||||
// return request.JSON(http.StatusInternalServerError, err.Error())
|
return func(request echo.Context) error {
|
||||||
// }
|
ctx := context.Background()
|
||||||
//
|
|
||||||
// widget, err := widgetService.GetWidgetByID(ctx, model.WidgetID(widgetID))
|
widgetID, err := strconv.Atoi(request.FormValue("widgetID"))
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// slog.Error(err.Error())
|
slog.Error(err.Error())
|
||||||
// return request.JSON(http.StatusInternalServerError, err.Error())
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
||||||
// }
|
}
|
||||||
// if len(widget) == 0 {
|
|
||||||
// return request.JSON(http.StatusNotFound, "widget not found")
|
mediaType := request.Param("mediaType")
|
||||||
// }
|
|
||||||
//
|
file, err := request.FormFile("file")
|
||||||
// return request.JSON(200, widget)
|
if err != nil {
|
||||||
// }
|
slog.Error(err.Error())
|
||||||
//}
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
||||||
//
|
}
|
||||||
//func SetMediaFile(widgetService model.WidgetService) echo.HandlerFunc {
|
|
||||||
// return func(request echo.Context) error {
|
src, err := file.Open()
|
||||||
// ctx := context.Background()
|
if err != nil {
|
||||||
//
|
slog.Error(err.Error())
|
||||||
// widgetID, err := strconv.Atoi(request.FormValue("widgetID"))
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
||||||
// if err != nil {
|
}
|
||||||
// slog.Error(err.Error())
|
|
||||||
// return request.JSON(http.StatusInternalServerError, err.Error())
|
err = widgetService.SetMediaFile(
|
||||||
// }
|
ctx,
|
||||||
//
|
model.MediaType(mediaType),
|
||||||
// mediaType := request.Param("mediaType")
|
model.WidgetID(widgetID),
|
||||||
//
|
&src,
|
||||||
// file, err := request.FormFile("file")
|
file.Filename,
|
||||||
// if err != nil {
|
file.Size,
|
||||||
// slog.Error(err.Error())
|
"",
|
||||||
// return request.JSON(http.StatusInternalServerError, err.Error())
|
)
|
||||||
// }
|
if err != nil {
|
||||||
//
|
slog.Error(err.Error())
|
||||||
// src, err := file.Open()
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
||||||
// if err != nil {
|
}
|
||||||
// slog.Error(err.Error())
|
slog.Info("set " + mediaType + " file successfully")
|
||||||
// return request.JSON(http.StatusInternalServerError, err.Error())
|
return request.String(200, "File successfully uploaded")
|
||||||
// }
|
}
|
||||||
//
|
}
|
||||||
// err = widgetService.SetMediaFile(
|
|
||||||
// ctx,
|
func GetMediaFile(widgetService model.WidgetService) echo.HandlerFunc {
|
||||||
// model.MediaType(mediaType),
|
return func(request echo.Context) error {
|
||||||
// model.WidgetID(widgetID),
|
ctx := context.Background()
|
||||||
// &src,
|
|
||||||
// file.Filename,
|
mediaType := request.Param("mediaType")
|
||||||
// file.Size,
|
if mediaType != "background" && mediaType != "image" && mediaType != "audio" {
|
||||||
// "",
|
slog.Error("Path parameter 'mediaType' is invalid")
|
||||||
// )
|
return echo.NewHTTPError(400, "Path parameter 'mediaType' is invalid")
|
||||||
// if err != nil {
|
}
|
||||||
// slog.Error(err.Error())
|
|
||||||
// return request.JSON(http.StatusInternalServerError, err.Error())
|
widgetID, err := strconv.Atoi(request.Param("widgetID"))
|
||||||
// }
|
if err != nil {
|
||||||
// slog.Info("set " + mediaType + " file successfully")
|
slog.Error(err.Error())
|
||||||
// return request.String(200, "File successfully uploaded")
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
||||||
// }
|
}
|
||||||
//}
|
|
||||||
//
|
file, err := widgetService.GetMediaFile(
|
||||||
//func GetMediaFile(widgetService model.WidgetService) echo.HandlerFunc {
|
ctx,
|
||||||
// return func(request echo.Context) error {
|
model.WidgetID(widgetID),
|
||||||
// ctx := context.Background()
|
model.MediaType(mediaType),
|
||||||
//
|
)
|
||||||
// mediaType := request.Param("mediaType")
|
if err != nil {
|
||||||
// if mediaType != "background" && mediaType != "image" && mediaType != "audio" {
|
slog.Error(err.Error())
|
||||||
// slog.Error("Path parameter 'mediaType' is invalid")
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
||||||
// return echo.NewHTTPError(400, "Path parameter 'mediaType' is invalid")
|
}
|
||||||
// }
|
|
||||||
//
|
slog.Info("get " + mediaType + " file successfully")
|
||||||
// widgetID, err := strconv.Atoi(request.Param("widgetID"))
|
return request.Blob(200, "application/octet-stream", file)
|
||||||
// if err != nil {
|
}
|
||||||
// slog.Error(err.Error())
|
}
|
||||||
// return request.JSON(http.StatusInternalServerError, err.Error())
|
|
||||||
// }
|
func SetMediaUrl(widgetService model.WidgetService) echo.HandlerFunc {
|
||||||
//
|
type Body struct {
|
||||||
// file, err := widgetService.GetMediaFile(
|
WidgetID model.WidgetID `json:"widgetID" validate:"required"`
|
||||||
// ctx,
|
MediaUrl model.MediaUrl `json:"mediaUrl" validate:"required"`
|
||||||
// model.WidgetID(widgetID),
|
}
|
||||||
// model.MediaType(mediaType),
|
return func(request echo.Context) error {
|
||||||
// )
|
ctx := context.Background()
|
||||||
// if err != nil {
|
var body Body
|
||||||
// slog.Error(err.Error())
|
if err := request.Bind(&body); err != nil {
|
||||||
// return request.JSON(http.StatusInternalServerError, err.Error())
|
slog.Error(err.Error())
|
||||||
// }
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
||||||
//
|
}
|
||||||
// slog.Info("get " + mediaType + " file successfully")
|
if err := request.Validate(&body); err != nil {
|
||||||
// return request.Blob(200, "application/octet-stream", file)
|
slog.Error(err.Error())
|
||||||
// }
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
||||||
//}
|
}
|
||||||
//
|
|
||||||
//func SetMediaUrl(widgetService model.WidgetService) echo.HandlerFunc {
|
mediaType := request.Param("mediaType")
|
||||||
// type Body struct {
|
|
||||||
// WidgetID model.WidgetID `json:"widgetID" validate:"required"`
|
err := widgetService.SetMediaUrl(
|
||||||
// MediaUrl model.MediaUrl `json:"mediaUrl" validate:"required"`
|
ctx,
|
||||||
// }
|
model.MediaType(mediaType),
|
||||||
// return func(request echo.Context) error {
|
body.WidgetID,
|
||||||
// ctx := context.Background()
|
body.MediaUrl,
|
||||||
// var body Body
|
)
|
||||||
// if err := request.Bind(&body); err != nil {
|
if err != nil {
|
||||||
// slog.Error(err.Error())
|
slog.Error(err.Error())
|
||||||
// return request.JSON(http.StatusInternalServerError, err.Error())
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
||||||
// }
|
}
|
||||||
// if err := request.Validate(&body); err != nil {
|
|
||||||
// slog.Error(err.Error())
|
return request.String(200, "Media URL successfully set")
|
||||||
// return request.JSON(http.StatusInternalServerError, err.Error())
|
}
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// mediaType := request.Param("mediaType")
|
|
||||||
//
|
|
||||||
// err := widgetService.SetMediaUrl(
|
|
||||||
// ctx,
|
|
||||||
// model.MediaType(mediaType),
|
|
||||||
// body.WidgetID,
|
|
||||||
// body.MediaUrl,
|
|
||||||
// )
|
|
||||||
// if err != nil {
|
|
||||||
// slog.Error(err.Error())
|
|
||||||
// return request.JSON(http.StatusInternalServerError, err.Error())
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// return request.String(200, "Media URL successfully set")
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
@ -595,7 +595,7 @@ const docTemplate = `{
|
|||||||
var SwaggerInfo = &swag.Spec{
|
var SwaggerInfo = &swag.Spec{
|
||||||
Version: "3.0",
|
Version: "3.0",
|
||||||
Host: "",
|
Host: "",
|
||||||
BasePath: "/api/donat-wiget",
|
BasePath: "/api/widget",
|
||||||
Schemes: []string{},
|
Schemes: []string{},
|
||||||
Title: "Donate Auth Documentation",
|
Title: "Donate Auth Documentation",
|
||||||
Description: "Donate auth service docs.",
|
Description: "Donate auth service docs.",
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
"contact": {},
|
"contact": {},
|
||||||
"version": "3.0"
|
"version": "3.0"
|
||||||
},
|
},
|
||||||
"basePath": "/api/donat-wiget",
|
"basePath": "/api/widget",
|
||||||
"paths": {
|
"paths": {
|
||||||
"/donat-page": {
|
"/donat-page": {
|
||||||
"patch": {
|
"patch": {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
basePath: /api/donat-wiget
|
basePath: /api/widget
|
||||||
definitions:
|
definitions:
|
||||||
donat-widget_internal_model.FilterSettingResponse:
|
donat-widget_internal_model.FilterSettingResponse:
|
||||||
properties:
|
properties:
|
||||||
|
@ -8,6 +8,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type WidgetService interface {
|
type WidgetService interface {
|
||||||
|
CheckToken(request echo.Context) (api.CheckTokenResponse, error)
|
||||||
|
|
||||||
CreateWidget(ctx context.Context, streamerID StreamerID, templateId TemplateID, duration Duration, minAmount DonatAmount) (WidgetID, error)
|
CreateWidget(ctx context.Context, streamerID StreamerID, templateId TemplateID, duration Duration, minAmount DonatAmount) (WidgetID, error)
|
||||||
|
|
||||||
GetWidgetByID(ctx context.Context, widgetID WidgetID) ([]*Widget, error)
|
GetWidgetByID(ctx context.Context, widgetID WidgetID) ([]*Widget, error)
|
||||||
|
@ -160,6 +160,22 @@ type UpdateModeration struct {
|
|||||||
Duration int `json:"duration"`
|
Duration int `json:"duration"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CreateWidgetBody struct {
|
||||||
|
TemplateID TemplateID `json:"templateID"`
|
||||||
|
MinAmount DonatAmount `json:"minAmount"`
|
||||||
|
Duration Duration `json:"duration"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type CreateWidgetResponse struct {
|
||||||
|
WidgetID WidgetID `json:"widgetID"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type WidgetInfoResponse struct {
|
||||||
|
AudioUrl MediaUrl `json:"audioUrl"`
|
||||||
|
ImageUrl MediaUrl `json:"imageUrl"`
|
||||||
|
Duration Duration `json:"duration"`
|
||||||
|
}
|
||||||
|
|
||||||
type DonatAndWidget struct {
|
type DonatAndWidget struct {
|
||||||
Widget *Widget
|
Widget *Widget
|
||||||
Donat *Donat
|
Donat *Donat
|
||||||
|
@ -3,20 +3,26 @@ package widget
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"donat-widget/internal/model"
|
"donat-widget/internal/model"
|
||||||
|
"donat-widget/internal/model/api"
|
||||||
"errors"
|
"errors"
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func New(
|
func New(
|
||||||
widgetRepo model.WidgetRepo,
|
widgetRepo model.WidgetRepo,
|
||||||
|
authClient model.AuthClient,
|
||||||
) *ServiceWidget {
|
) *ServiceWidget {
|
||||||
return &ServiceWidget{
|
return &ServiceWidget{
|
||||||
widgetRepo: widgetRepo,
|
widgetRepo: widgetRepo,
|
||||||
|
authClient: authClient,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServiceWidget struct {
|
type ServiceWidget struct {
|
||||||
widgetRepo model.WidgetRepo
|
widgetRepo model.WidgetRepo
|
||||||
|
authClient model.AuthClient
|
||||||
}
|
}
|
||||||
|
|
||||||
func (widgetService *ServiceWidget) CreateWidget(
|
func (widgetService *ServiceWidget) CreateWidget(
|
||||||
@ -157,3 +163,27 @@ func (widgetService *ServiceWidget) SetMediaUrl(
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (widgetService *ServiceWidget) CheckToken(
|
||||||
|
request echo.Context,
|
||||||
|
) (api.CheckTokenResponse, error) {
|
||||||
|
accessToken := model.Token(request.Request().Header.Get("Authorization"))
|
||||||
|
|
||||||
|
checkTokenResponse, err := widgetService.authClient.CheckToken(accessToken)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("Failed to check token", "error", err.Error())
|
||||||
|
return api.CheckTokenResponse{}, echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
if checkTokenResponse.AccountID == 0 {
|
||||||
|
slog.Error("Unauthorized account")
|
||||||
|
return api.CheckTokenResponse{}, echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized account")
|
||||||
|
}
|
||||||
|
|
||||||
|
if checkTokenResponse.AccountID == -1 {
|
||||||
|
slog.Error("Expired token")
|
||||||
|
return api.CheckTokenResponse{}, echo.NewHTTPError(http.StatusUnauthorized, "Expired token")
|
||||||
|
}
|
||||||
|
|
||||||
|
return checkTokenResponse, nil
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user