• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang goa.Context类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/raphael/goa.Context的典型用法代码示例。如果您正苦于以下问题:Golang Context类的具体用法?Golang Context怎么用?Golang Context使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Context类的19个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: NewListBottleContext

// NewListBottleContext parses the incoming request URL and body, performs validations and creates the
// context used by the bottle controller list action.
func NewListBottleContext(c *goa.Context) (*ListBottleContext, error) {
	var err error
	ctx := ListBottleContext{Context: c}
	rawAccountID, ok := c.Get("accountID")
	if ok {
		if accountID, err2 := strconv.Atoi(rawAccountID); err2 == nil {
			ctx.AccountID = int(accountID)
		} else {
			err = goa.InvalidParamTypeError("accountID", rawAccountID, "integer", err)
		}
	}
	rawYears, ok := c.Get("years")
	if ok {
		elemsYears := strings.Split(rawYears, ",")
		elemsYears2 := make([]int, len(elemsYears))
		for i, rawElem := range elemsYears {
			if elem, err2 := strconv.Atoi(rawElem); err2 == nil {
				elemsYears2[i] = int(elem)
			} else {
				err = goa.InvalidParamTypeError("elem", rawElem, "integer", err)
			}
		}
		ctx.Years = elemsYears2
		ctx.HasYears = true
	}
	return &ctx, err
}
开发者ID:cw2018,项目名称:goa,代码行数:29,代码来源:contexts.go


示例2: NewCreateAccountContext

// NewCreateAccountContext parses the incoming request URL and body, performs validations and creates the
// context used by the account controller create action.
func NewCreateAccountContext(c *goa.Context) (*CreateAccountContext, error) {
	var err error
	ctx := CreateAccountContext{Context: c}
	p, err := NewCreateAccountPayload(c.Payload())
	if err != nil {
		return nil, err
	}
	ctx.Payload = p
	return &ctx, err
}
开发者ID:cw2018,项目名称:goa,代码行数:12,代码来源:contexts.go


示例3: NewDeleteAccountContext

// NewDeleteAccountContext parses the incoming request URL and body, performs validations and creates the
// context used by the account controller delete action.
func NewDeleteAccountContext(c *goa.Context) (*DeleteAccountContext, error) {
	var err error
	ctx := DeleteAccountContext{Context: c}
	rawAccountID := c.Get("accountID")
	if accountID, err2 := strconv.Atoi(rawAccountID); err2 == nil {
		ctx.AccountID = int(accountID)
	} else {
		err = goa.InvalidParamTypeError("accountID", rawAccountID, "integer", err)
	}
	return &ctx, err
}
开发者ID:tylerb,项目名称:goa,代码行数:13,代码来源:contexts.go


示例4: RequestResource

// RequestResource returns the resource targeted by the CORS request defined in ctx.
func (v Specification) RequestResource(ctx *goa.Context, origin string) *ResourceDefinition {
	path := ctx.Request().URL.Path
	var match *ResourceDefinition
	for _, res := range v {
		if res.OriginAllowed(origin) && res.PathMatches(path) {
			if res.Check == nil || res.Check(ctx) {
				match = res
				break
			}
		}
	}
	return match
}
开发者ID:patrickToca,项目名称:goa,代码行数:14,代码来源:middleware.go


示例5: NewListSeriesContext

// NewListSeriesContext parses the incoming request URL and body, performs validations and creates the
// context used by the series controller list action.
func NewListSeriesContext(c *goa.Context) (*ListSeriesContext, error) {
	var err error
	ctx := ListSeriesContext{Context: c}
	rawAccountID, ok := c.Get("accountID")
	if ok {
		if accountID, err2 := strconv.Atoi(rawAccountID); err2 == nil {
			ctx.AccountID = int(accountID)
		} else {
			err = goa.InvalidParamTypeError("accountID", rawAccountID, "integer", err)
		}
	}
	return &ctx, err
}
开发者ID:AidHamza,项目名称:congo,代码行数:15,代码来源:contexts.go


示例6: NewCreateBottleContext

// NewCreateBottleContext parses the incoming request URL and body, performs validations and creates the
// context used by the bottle controller create action.
func NewCreateBottleContext(c *goa.Context) (*CreateBottleContext, error) {
	var err error
	ctx := CreateBottleContext{Context: c}
	rawAccountID := c.Get("accountID")
	if accountID, err2 := strconv.Atoi(rawAccountID); err2 == nil {
		ctx.AccountID = int(accountID)
	} else {
		err = goa.InvalidParamTypeError("accountID", rawAccountID, "integer", err)
	}
	p, err := NewCreateBottlePayload(c.Payload())
	if err != nil {
		return nil, err
	}
	ctx.Payload = p
	return &ctx, err
}
开发者ID:tylerb,项目名称:goa,代码行数:18,代码来源:contexts.go


示例7: NewUpdateUserContext

// NewUpdateUserContext parses the incoming request URL and body, performs validations and creates the
// context used by the user controller update action.
func NewUpdateUserContext(c *goa.Context) (*UpdateUserContext, error) {
	var err error
	ctx := UpdateUserContext{Context: c}
	rawAccountID, ok := c.Get("accountID")
	if ok {
		if accountID, err2 := strconv.Atoi(rawAccountID); err2 == nil {
			ctx.AccountID = int(accountID)
		} else {
			err = goa.InvalidParamTypeError("accountID", rawAccountID, "integer", err)
		}
	}
	rawUserID, ok := c.Get("userID")
	if ok {
		if userID, err2 := strconv.Atoi(rawUserID); err2 == nil {
			ctx.UserID = int(userID)
		} else {
			err = goa.InvalidParamTypeError("userID", rawUserID, "integer", err)
		}
	}
	p, err := NewUpdateUserPayload(c.Payload())
	if err != nil {
		return nil, err
	}
	ctx.Payload = p
	return &ctx, err
}
开发者ID:AidHamza,项目名称:congo,代码行数:28,代码来源:contexts.go


示例8: GetToken

// GetToken extracts the JWT token from the request if there is one.
func GetToken(ctx *goa.Context, spec *Specification) (token *jwt.Token, err error) {
	var found bool
	var tok string
	header := ctx.Request().Header.Get(spec.TokenHeader)

	if header != "" {
		parts := strings.Split(header, " ")
		if len(parts) != 2 || strings.ToLower(parts[0]) != "bearer" {
			// This is an error
		}
		tok = parts[1]
		found = true
	}
	if !found && spec.AllowParam {
		tok = ctx.Request().URL.Query().Get(spec.TokenParam)

	}
	if tok == "" {
		err = fmt.Errorf("no token")
		return
	}
	token, err = jwt.Parse(tok, keyFuncWrapper(spec.ValidationFunc))
	return
}
开发者ID:tylerb,项目名称:goa-middleware,代码行数:25,代码来源:jwt.go


示例9:

	jwtg "github.com/dgrijalva/jwt-go"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/raphael/goa"
	"github.com/raphael/goa-middleware/jwt"
)

var signingKey = []byte("jwtsecretsauce")

// Sample data from http://tools.ietf.org/html/draft-jones-json-web-signature-04#appendix-A.1
var hmacTestKey, _ = ioutil.ReadFile("test/hmacTestKey")
var rsaSampleKey, _ = ioutil.ReadFile("test/sample_key")
var rsaSampleKeyPub, _ = ioutil.ReadFile("test/sample_key.pub")

var _ = Describe("JWT Middleware", func() {
	var ctx *goa.Context
	var spec *jwt.Specification
	var req *http.Request
	var err error
	var token *jwtg.Token
	var tokenString string
	params := url.Values{"param": []string{"value"}, "query": []string{"qvalue"}}
	payload := map[string]interface{}{"payload": 42}
	validFunc := func(token *jwtg.Token) (interface{}, error) {
		return signingKey, nil
	}

	BeforeEach(func() {
		req, err = http.NewRequest("POST", "/goo", strings.NewReader(`{"payload":42}`))
		Ω(err).ShouldNot(HaveOccurred())
		rw := new(TestResponseWriter)
开发者ID:derekperkins,项目名称:goa-middleware,代码行数:31,代码来源:jwt_test.go


示例10:

	Context("using a goa middleware func", func() {
		var goaMiddlewareFunc func(goa.Handler) goa.Handler

		BeforeEach(func() {
			goaMiddlewareFunc = func(h goa.Handler) goa.Handler { return h }
			input = goaMiddlewareFunc
		})

		It("returns the middleware", func() {
			Ω(fmt.Sprintf("%#v", middleware)).Should(Equal(fmt.Sprintf("%#v", goa.Middleware(goaMiddlewareFunc))))
			Ω(mErr).ShouldNot(HaveOccurred())
		})
	})

	Context("with a context", func() {
		var ctx *goa.Context

		BeforeEach(func() {
			req, err := http.NewRequest("GET", "/goo", nil)
			Ω(err).ShouldNot(HaveOccurred())
			rw := new(TestResponseWriter)
			params := url.Values{"foo": []string{"bar"}}
			ctx = goa.NewContext(nil, nil, req, rw, params)
			Ω(ctx.ResponseStatus()).Should(Equal(0))
		})

		Context("using a goa handler", func() {
			BeforeEach(func() {
				var goaHandler goa.Handler = func(ctx *goa.Context) error {
					ctx.JSON(200, "ok")
					return nil
开发者ID:yonglehou,项目名称:goa,代码行数:31,代码来源:middleware_test.go


示例11:

				ctrl := s.NewController("test")
				Ω(ctrl.MiddlewareChain()).Should(HaveLen(1))
				Ω(ctrl.MiddlewareChain()[0]).Should(BeAssignableToTypeOf(middleware.RequestID()))
			})
		})
	})

	Describe("HandleFunc", func() {
		const resName = "res"
		const actName = "act"
		var handler, unmarshaler goa.Handler
		const respStatus = 200
		var respContent = []byte("response")

		var handleFunc goa.HandleFunc
		var ctx *goa.Context

		JustBeforeEach(func() {
			ctrl := s.NewController("test")
			handleFunc = ctrl.HandleFunc(actName, handler, unmarshaler)
		})

		BeforeEach(func() {
			handler = func(c *goa.Context) error {
				ctx = c
				c.RespondBytes(respStatus, respContent)
				return nil
			}
			unmarshaler = func(c *goa.Context) error {
				ctx = c
				req := c.Request()
开发者ID:minloved,项目名称:goa,代码行数:31,代码来源:service_test.go


示例12: getSwagger

// getSwagger is the httprouter handle that returns the Swagger spec.
// func getSwagger(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
func getSwagger(ctx *goa.Context) error {
	ctx.Header().Set("Content-Type", "application/swagger+json")
	ctx.Header().Set("Cache-Control", "public, max-age=3600")
	return ctx.Respond(200, []byte(spec))
}
开发者ID:patrickToca,项目名称:goa,代码行数:7,代码来源:swagger.go


示例13:

				ctrl := s.NewController("test")
				Ω(ctrl.MiddlewareChain()).Should(HaveLen(1))
				Ω(ctrl.MiddlewareChain()[0]).Should(BeAssignableToTypeOf(goa.RequestID()))
			})
		})
	})

	Describe("NewHTTPRouterHandle", func() {
		const resName = "res"
		const actName = "act"
		var handler goa.Handler
		const respStatus = 200
		var respContent = []byte("response")

		var httpHandle httprouter.Handle
		var ctx *goa.Context

		JustBeforeEach(func() {
			ctrl := s.NewController("test")
			httpHandle = ctrl.NewHTTPRouterHandle(actName, handler)
		})

		BeforeEach(func() {
			handler = func(c *goa.Context) error {
				ctx = c
				c.Respond(respStatus, respContent)
				return nil
			}
		})

		It("creates a handle", func() {
开发者ID:cw2018,项目名称:goa,代码行数:31,代码来源:service_test.go


示例14: Header

func (t *TestResponseWriter) Header() http.Header {
	return t.ParentHeader
}

func (t *TestResponseWriter) Write(b []byte) (int, error) {
	t.Body = append(t.Body, b...)
	return len(b), nil
}

func (t *TestResponseWriter) WriteHeader(s int) {
	t.Status = s
}

var _ = Describe("Gzip", func() {
	var handler *testHandler
	var ctx *goa.Context
	var req *http.Request
	var rw *TestResponseWriter
	params := url.Values{"param": []string{"value"}}
	payload := map[string]interface{}{"payload": 42}

	BeforeEach(func() {
		var err error
		req, err = http.NewRequest("POST", "/foo/bar", strings.NewReader(`{"payload":42}`))
		req.Header.Set("Accept-Encoding", "gzip")
		Ω(err).ShouldNot(HaveOccurred())
		rw = &TestResponseWriter{
			ParentHeader: http.Header{},
		}

		ctx = goa.NewContext(nil, goa.New("test"), req, rw, params)
开发者ID:derekperkins,项目名称:goa-middleware,代码行数:31,代码来源:middleware_test.go


示例15:

	"net/url"

	"github.com/raphael/goa"
	"github.com/tscolari/cf-broker-api/common/repository"
	"github.com/tscolari/cf-broker-api/common/repository/fakes"
	"github.com/tscolari/memcached-broker/app"
	"github.com/tscolari/memcached-broker/controllers"
	"golang.org/x/net/context"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Provisioning", func() {
	var provisioningController *controllers.Provisioning
	var goaContext *goa.Context
	var responseWriter *httptest.ResponseRecorder
	var state *fakes.FakeState

	BeforeEach(func() {
		state = new(fakes.FakeState)
		provisioningController = controllers.NewProvisioning(state)

		gctx := context.Background()
		req := http.Request{}
		responseWriter = httptest.NewRecorder()
		params := url.Values{}
		payload := map[string]string{}

		goaContext = goa.NewContext(gctx, &req, responseWriter, params, payload)
	})
开发者ID:tscolari,项目名称:memcached-broker,代码行数:31,代码来源:provisioning_test.go


示例16:

	"encoding/json"
	"fmt"
	"net/http"
	"net/url"
	"strings"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/raphael/goa"
	"golang.org/x/net/context"
	"gopkg.in/inconshreveable/log15.v2"
)

var _ = Describe("Context", func() {
	var logger log15.Logger
	var ctx *goa.Context

	BeforeEach(func() {
		gctx := context.Background()
		ctx = &goa.Context{Context: gctx, Logger: logger}
	})

	Describe("SetValue", func() {
		key := "answer"
		val := 42

		BeforeEach(func() {
			ctx.SetValue(key, val)
		})

		It("sets the value in the context.Context", func() {
开发者ID:yonglehou,项目名称:goa,代码行数:31,代码来源:context_test.go


示例17:

				ctrl := s.NewController("test")
				Ω(ctrl.MiddlewareChain()).Should(HaveLen(1))
				Ω(ctrl.MiddlewareChain()[0]).Should(BeAssignableToTypeOf(goa.RequestID()))
			})
		})
	})

	Describe("HandleFunc", func() {
		const resName = "res"
		const actName = "act"
		var handler, unmarshaler goa.Handler
		const respStatus = 200
		var respContent = []byte("response")

		var handleFunc goa.HandleFunc
		var ctx *goa.Context

		JustBeforeEach(func() {
			ctrl := s.NewController("test")
			handleFunc = ctrl.HandleFunc(actName, handler, unmarshaler)
		})

		BeforeEach(func() {
			handler = func(c *goa.Context) error {
				ctx = c
				c.Respond(respStatus, respContent)
				return nil
			}
			unmarshaler = func(c *goa.Context) error {
				ctx = c
				req := c.Request()
开发者ID:yonglehou,项目名称:goa,代码行数:31,代码来源:service_test.go


示例18:

	Context("using a goa middleware func", func() {
		var goaMiddlewareFunc func(goa.Handler) goa.Handler

		BeforeEach(func() {
			goaMiddlewareFunc = func(h goa.Handler) goa.Handler { return h }
			input = goaMiddlewareFunc
		})

		It("returns the middleware", func() {
			Ω(fmt.Sprintf("%#v", middleware)).Should(Equal(fmt.Sprintf("%#v", goa.Middleware(goaMiddlewareFunc))))
			Ω(mErr).ShouldNot(HaveOccurred())
		})
	})

	Context("with a context", func() {
		var ctx *goa.Context

		BeforeEach(func() {
			req, err := http.NewRequest("GET", "/goo", nil)
			Ω(err).ShouldNot(HaveOccurred())
			rw := new(TestResponseWriter)
			params := url.Values{"foo": []string{"bar"}}
			ctx = goa.NewContext(nil, req, rw, params, nil)
			Ω(ctx.ResponseStatus()).Should(Equal(0))
		})

		Context("using a goa handler", func() {
			BeforeEach(func() {
				var goaHandler goa.Handler = func(ctx *goa.Context) error {
					ctx.JSON(200, "ok")
					return nil
开发者ID:tashanji,项目名称:goa,代码行数:31,代码来源:middleware_test.go


示例19:

		var goaMiddlewareFunc func(goa.Handler) goa.Handler

		BeforeEach(func() {
			goaMiddlewareFunc = func(h goa.Handler) goa.Handler { return h }
			input = goaMiddlewareFunc
		})

		It("returns the middleware", func() {
			Ω(fmt.Sprintf("%#v", middleware)).Should(Equal(fmt.Sprintf("%#v", goa.Middleware(goaMiddlewareFunc))))
			Ω(mErr).ShouldNot(HaveOccurred())
		})
	})

	Context("with a context", func() {
		var service goa.Service
		var ctx *goa.Context

		BeforeEach(func() {
			service = goa.New("test")
			req, err := http.NewRequest("GET", "/goo", nil)
			Ω(err).ShouldNot(HaveOccurred())
			rw := new(TestResponseWriter)
			params := url.Values{"foo": []string{"bar"}}
			ctx = goa.NewContext(nil, service, req, rw, params)
			Ω(ctx.ResponseStatus()).Should(Equal(0))
		})

		Context("using a goa handler", func() {
			BeforeEach(func() {
				var goaHandler goa.Handler = func(ctx *goa.Context) error {
					ctx.Respond(200, "ok")
开发者ID:minloved,项目名称:goa,代码行数:31,代码来源:middleware_test.go



注:本文中的github.com/raphael/goa.Context类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang goa.Service类代码示例发布时间:2022-05-28
下一篇:
Golang goa.ValidateFormat函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap