removed dependencies
This commit is contained in:
@@ -12,6 +12,12 @@ const (
|
||||
notFound = `Source Not Found`
|
||||
)
|
||||
|
||||
const (
|
||||
internalErrorCode = "1"
|
||||
badRequestCode = "2"
|
||||
notFoundErrorCode = "3"
|
||||
)
|
||||
|
||||
type UnmarshalError struct {
|
||||
err error
|
||||
}
|
||||
|
@@ -4,10 +4,8 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
cmsstorage "go.unistack.org/cms-service/storage"
|
||||
httpsrv "go.unistack.org/micro-server-http/v4"
|
||||
"go.unistack.org/micro/v4"
|
||||
"go.unistack.org/unistack-org/pkgdash/config"
|
||||
pb "go.unistack.org/unistack-org/pkgdash/proto"
|
||||
cligit "go.unistack.org/unistack-org/pkgdash/service/client_git"
|
||||
"go.unistack.org/unistack-org/pkgdash/storage"
|
||||
@@ -20,7 +18,6 @@ type Handler struct {
|
||||
svc micro.Service
|
||||
store storage.Storage
|
||||
|
||||
writer writer
|
||||
protojson.MarshalOptions
|
||||
protojson.UnmarshalOptions
|
||||
|
||||
@@ -143,20 +140,15 @@ func NewHandler(svc micro.Service, client cligit.Client) *Handler {
|
||||
return h
|
||||
}
|
||||
|
||||
// TODO add conn db
|
||||
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)
|
||||
store, err := storage.FromContext(h.svc.Options().Context)
|
||||
if err != nil {
|
||||
return errors.New("missing storage")
|
||||
}
|
||||
|
||||
h.chanUrl = h.git.Run(ctx, st)
|
||||
h.chanUrl = h.git.Run(ctx, store)
|
||||
|
||||
h.store = st
|
||||
h.store = store
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@@ -1,34 +0,0 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
pb "go.unistack.org/unistack-org/pkgdash/proto"
|
||||
)
|
||||
|
||||
const (
|
||||
internalErrorCode = "1"
|
||||
badRequestCode = "2"
|
||||
notFoundErrorCode = "3"
|
||||
)
|
||||
|
||||
func mapError(ctx context.Context, err error) (result *pb.Error, status int) {
|
||||
status = http.StatusBadRequest
|
||||
|
||||
switch errors.Unwrap(err).(type) {
|
||||
case *UnmarshalError:
|
||||
result = &pb.Error{Code: badRequestCode, Title: "Bad request"}
|
||||
case *ParametersMissingError:
|
||||
result = &pb.Error{Code: badRequestCode, Title: "Required parameters are missing"}
|
||||
case *NotFoundError:
|
||||
result = &pb.Error{Code: notFoundErrorCode, Title: "Not found"}
|
||||
status = http.StatusNotFound
|
||||
default:
|
||||
status = http.StatusInternalServerError
|
||||
result = &pb.Error{Code: internalErrorCode, Title: "Internal error", Details: err.Error()}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
@@ -1,21 +0,0 @@
|
||||
package handler
|
||||
|
||||
import "net/http"
|
||||
|
||||
func Methods(m string, next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, req *http.Request) {
|
||||
if req.Method != m {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
switch req.Method {
|
||||
case http.MethodPost:
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
case http.MethodPut:
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, req)
|
||||
}
|
||||
}
|
@@ -1,77 +0,0 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"go.unistack.org/micro/v4/logger"
|
||||
pb "go.unistack.org/unistack-org/pkgdash/proto"
|
||||
)
|
||||
|
||||
type encoder interface {
|
||||
Success(rw http.ResponseWriter, response interface{}) error
|
||||
Error(rw http.ResponseWriter, err *pb.Error, status int) error
|
||||
}
|
||||
|
||||
type writer interface {
|
||||
Response(ctx context.Context, rw http.ResponseWriter, value interface{})
|
||||
}
|
||||
|
||||
// nolint
|
||||
type stackTracer interface {
|
||||
StackTrace() errors.StackTrace
|
||||
}
|
||||
|
||||
type Writer struct {
|
||||
encoder encoder
|
||||
}
|
||||
|
||||
func NewWriter(encoder encoder) (*Writer, error) {
|
||||
if encoder == nil {
|
||||
return nil, errors.New("empty encoder")
|
||||
}
|
||||
return &Writer{encoder: encoder}, nil
|
||||
}
|
||||
|
||||
func (w *Writer) Response(ctx context.Context, rw http.ResponseWriter, value interface{}) {
|
||||
var err error
|
||||
|
||||
if v, ok := value.(error); ok {
|
||||
err = w.error(ctx, rw, v)
|
||||
} else {
|
||||
err = w.success(rw, value)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
logger.Error(ctx, "Response writing error: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Writer) error(ctx context.Context, rw http.ResponseWriter, err error) error {
|
||||
e, status := mapError(ctx, err)
|
||||
/*
|
||||
switch {
|
||||
case status >= http.StatusInternalServerError:
|
||||
logger.Errorf(ctx, "error: %s, code: %s, http status: %d, uuid: %s", err, e.Code, status, e.Uuid)
|
||||
|
||||
if err, ok := err.(stackTracer); ok {
|
||||
logger.Errorf(ctx, "error stacktrace: %+v, uuid: %s", err.StackTrace(), e.Uuid)
|
||||
}
|
||||
|
||||
default:
|
||||
logger.Infof(ctx, "error: %s, code: %s, http status: %d, uuid: %s", err, e.Code, status, e.Uuid)
|
||||
|
||||
if err, ok := err.(stackTracer); ok {
|
||||
logger.Infof(ctx, "error stacktrace: %+v, uuid: %s", err.StackTrace(), e.Uuid)
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
return w.encoder.Error(rw, e, status)
|
||||
}
|
||||
|
||||
func (w *Writer) success(rw http.ResponseWriter, value interface{}) error {
|
||||
return w.encoder.Success(rw, value)
|
||||
}
|
Reference in New Issue
Block a user