本文整理匯總了Golang中github.com/micro/go-micro/errors.BadRequest函數的典型用法代碼示例。如果您正苦於以下問題:Golang BadRequest函數的具體用法?Golang BadRequest怎麽用?Golang BadRequest使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了BadRequest函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: authClient
func authClient(clientId, clientSecret string) error {
acc, err := db.Search(clientId, "", 1, 0)
if err != nil {
return errors.InternalServerError("go.micro.srv.auth", "server_error")
}
if len(acc) == 0 {
return errors.BadRequest("go.micro.srv.auth", "invalid_request")
}
// check the secret
salt, secret, err := db.SaltAndSecret(acc[0].Id)
if err != nil {
return errors.InternalServerError("go.micro.srv.auth", "server_error")
}
s, err := base64.StdEncoding.DecodeString(secret)
if err != nil {
return errors.InternalServerError("go.micro.srv.auth", "server_error")
}
// does it match?
if err := bcrypt.CompareHashAndPassword(s, []byte(x+salt+clientSecret)); err != nil {
return errors.BadRequest("go.micro.srv.auth", "access_denied")
}
return nil
}
示例2: Update
func (r *Rule) Update(ctx context.Context, req *proto.UpdateRequest, rsp *proto.UpdateResponse) error {
if req.Rule == nil {
return errors.BadRequest("go.micro.srv.router.Rule.Update", "invalid rule")
}
if len(req.Rule.Id) == 0 {
return errors.BadRequest("go.micro.srv.router.Rule.Update", "invalid id")
}
if len(req.Rule.Service) == 0 {
return errors.BadRequest("go.micro.srv.router.Rule.Update", "invalid service")
}
if len(req.Rule.Version) == 0 {
return errors.BadRequest("go.micro.srv.router.Rule.Update", "invalid version")
}
if req.Rule.Weight < 0 || req.Rule.Weight > 100 {
return errors.BadRequest("go.micro.srv.router.Rule.Update", "invalid weight, must be 0 to 100")
}
if err := rule.Update(req.Rule); err != nil {
return errors.InternalServerError("go.micro.srv.router.Rule.Update", err.Error())
}
return nil
}
示例3: Update
func (r *Label) Update(ctx context.Context, req *proto.UpdateRequest, rsp *proto.UpdateResponse) error {
if req.Label == nil {
return errors.BadRequest("go.micro.srv.router.Label.Update", "invalid label")
}
if len(req.Label.Id) == 0 {
return errors.BadRequest("go.micro.srv.router.Label.Update", "invalid id")
}
if len(req.Label.Service) == 0 {
return errors.BadRequest("go.micro.srv.router.Label.Update", "invalid service")
}
if len(req.Label.Key) == 0 {
return errors.BadRequest("go.micro.srv.router.Label.Update", "invalid key")
}
if req.Label.Weight < 0 || req.Label.Weight > 100 {
return errors.BadRequest("go.micro.srv.router.Label.Update", "invalid weight, must be 0 to 100")
}
if err := label.Update(req.Label); err != nil {
return errors.InternalServerError("go.micro.srv.router.Label.Update", err.Error())
}
return nil
}
示例4: Revoke
func (o *Oauth2) Revoke(ctx context.Context, req *oauth2.RevokeRequest, rsp *oauth2.RevokeResponse) error {
// Who should be allowed to do this?
if len(req.RefreshToken) > 0 {
token, _, err := db.ReadRefresh(req.RefreshToken)
if err != nil {
if err == db.ErrNotFound {
return errors.BadRequest("go.micro.srv.auth", "invalid_request")
}
return errors.InternalServerError("go.micro.srv.auth", "server_error")
}
if err := db.DeleteToken(req.AccessToken); err != nil {
return errors.InternalServerError("go.micro.srv.auth", "server_error")
}
req.AccessToken = token.AccessToken
}
if len(req.AccessToken) == 0 {
return errors.BadRequest("go.micro.srv.auth", "invalid_request")
}
if err := db.DeleteToken(req.AccessToken); err != nil {
return errors.InternalServerError("go.micro.srv.auth", "server_error")
}
return nil
}
示例5: DocRefFieldsExists
// DocRefFieldsExists returns an error if DocRef struct has zero value
func DocRefFieldsExists(dr *proto.DocRef) error {
if len(dr.Index) <= 0 {
return errors.BadRequest("go.micro.srv.elastic", "Index required")
}
if len(dr.Type) <= 0 {
return errors.BadRequest("go.micro.srv.elastic", "Type required")
}
return nil
}
示例6: Query
// Query API handler
func (es *Elastic) Query(ctx context.Context, req *api.Request, rsp *api.Response) error {
var err error
var input map[string]interface{}
var query []byte
// Unmarshal unknown JSON
if err = json.Unmarshal([]byte(req.Body), &input); err != nil {
return errors.BadRequest("go.micro.api.elastic", err.Error())
}
query, err = json.Marshal(input["query"])
srvReq := client.NewRequest(
"go.micro.srv.elastic",
"Elastic.Query",
&elastic.QueryRequest{
Index: fmt.Sprintf("%v", input["index"]),
Type: fmt.Sprintf("%v", input["type"]),
Query: string(query),
},
)
srvRsp := &elastic.SearchResponse{}
if err = client.Call(ctx, srvReq, srvRsp); err != nil {
return err
}
rsp.StatusCode = http.StatusOK
rsp.Body = srvRsp.Result
return nil
}
示例7: validateDB
func validateDB(method string, d *mdb.Database) error {
if d == nil {
return errors.BadRequest("go.micro.srv.db."+method, "invalid database")
}
if len(d.Name) == 0 {
return errors.BadRequest("go.micro.srv.db."+method, "database is blank")
}
if len(d.Table) == 0 {
return errors.BadRequest("go.micro.srv.db."+method, "table is blank")
}
// TODO: check exists
return nil
}
示例8: Hello
func (s *Say) Hello(ctx context.Context, req *api.Request, rsp *api.Response) error {
log.Info("Received Say.Hello API request")
name, ok := req.Get["name"]
if !ok || len(name.Values) == 0 {
return errors.BadRequest("go.micro.api.greeter", "Name cannot be blank")
}
request := client.NewRequest("go.micro.srv.greeter", "Say.Hello", &hello.Request{
Name: strings.Join(name.Values, " "),
})
response := &hello.Response{}
if err := client.Call(ctx, request, response); err != nil {
return err
}
rsp.StatusCode = 200
b, _ := json.Marshal(map[string]string{
"message": response.Msg,
})
rsp.Body = string(b)
return nil
}
示例9: SelectStream
func (r *Router) SelectStream(ctx context.Context, req *proto.SelectRequest, stream proto.Router_SelectStreamStream) error {
// TODO: process filters
if len(req.Service) == 0 {
return errors.BadRequest("go.micro.srv.router.Router.Select", "invalid service name")
}
t := time.NewTicker(time.Duration(router.DefaultExpiry) * time.Second)
defer t.Stop()
for {
services, err := router.Select(req.Service)
if err != nil && err == selector.ErrNotFound {
return errors.NotFound("go.micro.srv.router.Router.SelectStream", err.Error())
} else if err != nil {
return errors.InternalServerError("go.micro.srv.router.Router.SelectStream", err.Error())
}
if err := stream.Send(&proto.SelectResponse{
Services: services,
Expires: time.Now().Unix() + int64(router.DefaultExpiry),
}); err != nil {
return err
}
<-t.C
}
return nil
}
示例10: Introspect
func (o *Oauth2) Introspect(ctx context.Context, req *oauth2.IntrospectRequest, rsp *oauth2.IntrospectResponse) error {
// Who should be allowed to do this?
if len(req.AccessToken) == 0 {
return errors.BadRequest("go.micro.srv.auth", "invalid_request")
}
token, _, err := db.ReadToken(req.AccessToken)
if err != nil {
if err == db.ErrNotFound {
rsp.Active = false
return nil
}
return errors.InternalServerError("go.micro.srv.auth", "server_error")
}
if d := time.Now().Unix() - token.ExpiresAt; d > 0 {
rsp.Active = false
return nil
}
rsp.Token = token
rsp.Active = true
// should we really hand this over?
rsp.Token.RefreshToken = ""
return nil
}
示例11: Update
// Update API handler
func (es *Elastic) Update(ctx context.Context, req *api.Request, rsp *api.Response) error {
var err error
var input map[string]interface{}
var data []byte
// Unmarshal unknown JSON
if err = json.Unmarshal([]byte(req.Body), &input); err != nil {
return errors.BadRequest("go.micro.api.elastic", err.Error())
}
// Marshal unknown JSON (data)
data, err = json.Marshal(input["data"])
srvReq := client.NewRequest(
"go.micro.srv.elastic",
"Elastic.Update",
&elastic.UpdateRequest{
Index: fmt.Sprintf("%v", input["index"]),
Type: fmt.Sprintf("%v", input["type"]),
Id: fmt.Sprintf("%v", input["id"]),
Data: string(data),
},
)
srvRsp := &elastic.UpdateResponse{}
if err = client.Call(ctx, srvReq, srvRsp); err != nil {
return err
}
rsp.StatusCode = http.StatusOK
rsp.Body = `{}`
return nil
}
示例12: Authorize
func (o *Oauth2) Authorize(ctx context.Context, req *oauth2.AuthorizeRequest, rsp *oauth2.AuthorizeResponse) error {
// We may actually need to authenticate who can make this request.
// How should we do that?
switch req.ResponseType {
// requesting authorization code
case "code":
// check client id exists
if len(req.ClientId) == 0 {
return errors.BadRequest("go.micro.srv.auth", "invalid_request")
}
// if redirect uri exists and is not tls lets bail
if len(req.RedirectUri) > 0 && !strings.HasPrefix(req.RedirectUri, "https://") {
return errors.BadRequest("go.micro.srv.auth", "invalid_request")
}
// use default scope
if len(req.Scopes) == 0 {
req.Scopes = append(req.Scopes, DefaultScope)
}
// generate code
code := db.Code()
// store request; expire in 10 mins
if err := db.CreateRequest(code, req); err != nil {
return errors.InternalServerError("go.micro.srv.auth", "server_error")
}
// respond
rsp.Code = code
rsp.State = req.State
// we're done?!
// implicit token request
case "token":
// to be implemented
return errors.BadRequest("go.micro.srv.auth", "unsupported_response_type")
default:
return errors.BadRequest("go.micro.srv.auth", "unsupported_response_type")
}
return nil
}
示例13: Stream
func Stream(ctx context.Context, rec *event.Record) error {
if rec == nil {
return errors.BadRequest("go.micro.srv.event.Process", "invalid record")
}
if len(rec.Id) == 0 {
return errors.BadRequest("go.micro.srv.event.Process", "invalid id")
}
if len(rec.Type) == 0 {
return errors.BadRequest("go.micro.srv.event.Process", "invalid type")
}
if rec.Timestamp == 0 {
return errors.BadRequest("go.micro.srv.event.Process", "invalid timestamp")
}
mtx.RLock()
defer mtx.RUnlock()
gsubscribers := subs[all]
subscribers := subs[rec.Type]
// send to subscribers in a go channel
go func() {
// send to global subscribers
for _, sub := range gsubscribers {
select {
case sub.ch <- rec:
case <-time.After(time.Millisecond * 100):
}
}
// send to type subscribers
for _, sub := range subscribers {
select {
case sub.ch <- rec:
case <-time.After(time.Millisecond * 100):
}
}
}()
return nil
}
示例14: Update
func (e *Event) Update(ctx context.Context, req *event.UpdateRequest, rsp *event.UpdateResponse) error {
if req.Record == nil {
return errors.BadRequest("go.micro.srv.event.Update", "invalid record")
}
if len(req.Record.Id) == 0 {
return errors.BadRequest("go.micro.srv.event.Update", "invalid id")
}
if req.Record.Timestamp == 0 {
req.Record.Timestamp = time.Now().Unix()
}
if err := db.Update(req.Record); err != nil {
return errors.InternalServerError("go.micro.srv.event.Update", err.Error())
}
return nil
}
示例15: Delete
func (s *Account) Delete(ctx context.Context, req *account.DeleteRequest, rsp *account.DeleteResponse) error {
if len(req.Id) == 0 {
return errors.BadRequest("go.micro.srv.auth.Delete", "invalid id")
}
if err := db.Delete(req.Id); err != nil {
return errors.InternalServerError("go.micro.srv.auth.Delete", err.Error())
}
return nil
}