pkgdash/internal/handler/handler.go

77 lines
1.6 KiB
Go
Raw Normal View History

package handler
import (
"context"
"errors"
"net/http"
"strconv"
"github.com/google/uuid"
"go.unistack.org/micro/v4"
cligit "git.unistack.org/unistack-org/pkgdash/internal/service/client_git"
"git.unistack.org/unistack-org/pkgdash/internal/storage"
pb "git.unistack.org/unistack-org/pkgdash/proto"
"google.golang.org/protobuf/encoding/protojson"
)
type Handler struct {
svc micro.Service
store storage.Storage
protojson.MarshalOptions
protojson.UnmarshalOptions
git cligit.Client
chanUrl chan *pb.PackagesCreateReq
}
func NewNotFoundError(err error) *pb.ErrorRsp {
return &pb.ErrorRsp{
Code: strconv.Itoa(http.StatusBadRequest),
Title: "NotFound",
Uuid: uuid.New().String(),
Details: err.Error(),
}
}
func NewInternalError(err error) *pb.ErrorRsp {
return &pb.ErrorRsp{
Code: strconv.Itoa(http.StatusInternalServerError),
Title: "InternalServerError",
Uuid: uuid.New().String(),
Details: err.Error(),
}
}
func NewValidationError(err error) *pb.ErrorRsp {
return &pb.ErrorRsp{
Code: strconv.Itoa(http.StatusBadRequest),
Title: "BadRequest",
Uuid: uuid.New().String(),
Details: err.Error(),
}
}
func NewHandler(svc micro.Service, client cligit.Client) *Handler {
h := &Handler{
svc: svc,
git: client,
}
h.EmitUnpopulated = true
h.UseProtoNames = false
return h
}
func (h *Handler) Init(ctx context.Context) error {
store, err := storage.FromContext(h.svc.Options().Context)
if err != nil {
return errors.New("missing storage")
}
h.chanUrl = h.git.Run(ctx, store)
h.store = store
return nil
}