pkgdash/handler/handlers.go
Vasiliy Tolstov 2ef12956ac initial rewrite, use micro v4 and not cms-xxx stuff
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-08-11 21:27:38 +03:00

76 lines
1.6 KiB
Go

package handler
import (
"context"
"net/http"
cmsstorage "go.unistack.org/cms-service/storage"
"go.unistack.org/micro/v4"
"go.unistack.org/micro/v4/errors"
"go.unistack.org/unistack-org/pkgdash/config"
"go.unistack.org/unistack-org/pkgdash/models"
pb "go.unistack.org/unistack-org/pkgdash/proto"
"go.unistack.org/unistack-org/pkgdash/storage"
)
type Handler struct {
svc micro.Service
store storage.Storage
writer writer
}
func (h *Handler) ListPackage(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logger := h.svc.Logger()
logger.Debug(ctx, "Start getListPackage")
dbRsp, err := h.store.List(ctx)
if err != nil {
logger.Errorf(ctx, "error db response: %v", err)
h.writer.Response(ctx, w, err)
return
}
rsp := new(pb.ListPackageRsp)
rsp.Packages = models.ListPackage(dbRsp).Mapping()
logger.Debug(ctx, "Success finish getListPackage")
h.writer.Response(ctx, w, rsp)
}
func (h *Handler) UpdateInfo(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logger := h.svc.Logger()
logger.Debug(ctx, "Start UpdateInfo")
// TODO
}
func (h *Handler) AddComment(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logger := h.svc.Logger()
logger.Debug(ctx, "Start AddComment")
// TODO
}
func NewHandler(svc micro.Service, w writer) *Handler {
return &Handler{svc: svc, writer: w}
}
func (h *Handler) Init(ctx context.Context) error {
store := cmsstorage.InterfaceFromContext(h.svc.Options().Context)
if store == nil {
return cmsstorage.ErrMissingStorage
}
st, ok := store.(storage.Storage)
if !ok {
return errors.New(config.ServiceName, "error init storage", 1)
}
h.store = st
return nil
}