當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Context.Map方法代碼示例

本文整理匯總了Golang中github.com/go-martini/martini.Context.Map方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.Map方法的具體用法?Golang Context.Map怎麽用?Golang Context.Map使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/go-martini/martini.Context的用法示例。


在下文中一共展示了Context.Map方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: mapCart

func mapCart(c martini.Context, res http.ResponseWriter, r *http.Request) error {
	qs := r.URL.Query()
	var shopId string
	if qsId := qs.Get("shop"); qsId != "" {
		shopId = qsId
	} else if formId := r.FormValue("shop"); formId != "" {
		shopId = formId
	} else if headerId := r.Header.Get("shop"); headerId != "" {
		shopId = headerId
	}

	if shopId == "" {
		return fmt.Errorf("error: %s", "you must provide a shop identifier")
	}
	if !bson.IsObjectIdHex(shopId) {
		return fmt.Errorf("error: %s", "invalid shop identifier")
	}
	shop := cart.Shop{
		Id: bson.ObjectIdHex(shopId),
	}

	if shop.Id.Hex() == "" {
		return fmt.Errorf("error: %s", "invalid shop identifier")
	}

	if err := shop.Get(); err != nil {
		return err
	}
	if shop.Id.Hex() == "" {
		return fmt.Errorf("error: %s", "invalid shop identifier")
	}

	c.Map(&shop)
	return nil
}
開發者ID:ninnemana,項目名稱:API,代碼行數:35,代碼來源:middleware.go

示例2: mapCartAccount

func mapCartAccount(c martini.Context, res http.ResponseWriter, r *http.Request) error {

	auth := r.Header.Get("Authorization")
	token := strings.Replace(auth, "Bearer ", "", 1)

	cust, err := cart.AuthenticateAccount(token)
	if err != nil {
		return err
	}

	shop := cart.Shop{
		Id: cust.ShopId,
	}

	if shop.Id.Hex() == "" {
		return fmt.Errorf("error: %s", "invalid shop identifier")
	}

	if err := shop.Get(); err != nil {
		return err
	}
	if shop.Id.Hex() == "" {
		return fmt.Errorf("error: %s", "invalid shop identifier")
	}

	c.Map(&shop)
	c.Map(token)
	return nil
}
開發者ID:ninnemana,項目名稱:API,代碼行數:29,代碼來源:middleware.go

示例3: webAuth

func webAuth(
	lg *log.Logger,
	c martini.Context,
	routes martini.Routes,
	params martini.Params,
	r *http.Request,
	w http.ResponseWriter,
	s *sessions.Session,
	ren render.Render,
	dec formDecoder,
	mdec multiDecoder,
) {
	userId := sessGet(s, sessionUserId)
	if len(userId) == 0 {
		panic(ae(""))
	}
	state := &web{
		lg: lg, c: c, routes: routes, params: params,
		r: r, w: w, s: s, ren: ren,
		decode: dec, multiDecode: mdec,
		user: findUserById(userId),
	}
	ren.Template().Funcs(template.FuncMap{
		"url": state.url,
	})
	c.Map(state)
}
開發者ID:BurntSushi,項目名稱:lcmweb,代碼行數:27,代碼來源:web.go

示例4: RequestLocationProvider

func RequestLocationProvider(gaeContext appengine.Context, mContext martini.Context, request *http.Request) {
	locationCode := request.Header.Get("X-AppEngine-Country")

	location := locationFromCode(locationCode)

	gaeContext.Infof("Using location code: %v", location)
	mContext.Map(location)
}
開發者ID:BearchInc,項目名稱:fala-com-meu-carro,代碼行數:8,代碼來源:request_location_provider.go

示例5: validateAndMap

// Performs validation and combines errors from validation
// with errors from deserialization, then maps both the
// resulting struct and the errors to the context.
func validateAndMap(obj reflect.Value, context martini.Context, errors Errors, ifacePtr ...interface{}) {
	context.Invoke(Validate(obj.Interface()))
	errors = append(errors, getErrors(context)...)
	context.Map(errors)
	context.Map(obj.Elem().Interface())
	if len(ifacePtr) > 0 {
		context.MapTo(obj.Elem().Interface(), ifacePtr[0])
	}
}
開發者ID:joshrendek,項目名稱:pgdiagnose,代碼行數:12,代碼來源:binding.go

示例6: validateAndMap

// Performs validation and combines errors from validation
// with errors from deserialization, then maps both the
// resulting struct and the errors to the context.
func validateAndMap(obj reflect.Value, context martini.Context, errors *base.BindingErrors, ifacePtr ...interface{}) {
	context.Invoke(Validate(obj.Interface()))
	errors.Combine(getErrors(context))
	context.Map(*errors)
	context.Map(obj.Elem().Interface())
	if len(ifacePtr) > 0 {
		context.MapTo(obj.Elem().Interface(), ifacePtr[0])
	}
}
開發者ID:Julianzz,項目名稱:gogs,代碼行數:12,代碼來源:binding.go

示例7: Retrieve

func Retrieve(c martini.Context, params martini.Params, r render.Render) {
	id, _ := strconv.Atoi(params["id"])
	post, err := retrieve(id)
	if err != nil {
		r.Error(404)
		return
	}
	c.Map(post)
}
開發者ID:jf,項目名稱:gwp,代碼行數:9,代碼來源:post.go

示例8: AppengineContextProvider

// AppengineContextProvider provides an injectable and namespaced
// instance of appengine.Context
func AppengineContextProvider(c martini.Context, req *http.Request) {
	gae := appengine.NewContext(req)
	namespace := appengine.ModuleName(gae)
	context, err := appengine.Namespace(gae, namespace)
	if err != nil {
		panic(err)
	}
	c.Map(context)
}
開發者ID:BearchInc,項目名稱:trails-api,代碼行數:11,代碼來源:appengine_context_provider.go

示例9: gitHubAuthMiddleware

func gitHubAuthMiddleware(req *http.Request, res http.ResponseWriter, r render.Render, c martini.Context) {
	// Verify origin is GH
	template := make(map[string]string)
	template["contactUrl"] = os.Getenv("CONTACT_URL")
	template["contactValue"] = os.Getenv("CONTACT_VALUE")
	template["message"] = "There was an authenticating your account."
	err := req.ParseForm()
	if err != nil {
		log.Println(err)
		r.HTML(http.StatusBadRequest, "error", template)
		return
	}
	if len(req.Form["code"]) != 1 {
		r.HTML(http.StatusUnauthorized, "error", template)
		return
	}
	// If legit, attempt to get token
	payload := make(map[string]string)
	payload["client_id"] = os.Getenv("GITHUB_CLIENT_ID")
	payload["client_secret"] = os.Getenv("GITHUB_CLIENT_SECRET")
	payload["code"] = req.Form["code"][0]
	body, _ := json.Marshal(payload)
	ghReq, _ := http.NewRequest("POST", "https://github.com/login/oauth/access_token", bytes.NewReader(body))
	ghReq.Header.Add("Content-Type", acceptHeader)
	ghReq.Header.Add("Accept", acceptHeader)
	ghReq.Header.Add("User-Agent", userAgent)
	ghRes, err := http.DefaultClient.Do(ghReq)

	// check status code
	if err != nil {
		log.Println(err)
		r.HTML(http.StatusServiceUnavailable, "error", template)
		return
	}
	ghPayload, err := ioutil.ReadAll(ghRes.Body)
	if err != nil {
		log.Println(err)
		r.HTML(http.StatusInternalServerError, "error", template)
		return
	}
	var ghJSON map[string]interface{}
	err = json.Unmarshal(ghPayload, &ghJSON)
	if err != nil {
		log.Println(err)
		r.HTML(http.StatusInternalServerError, "error", template)
		return
	}
	token, ok := ghJSON["access_token"].(string)
	if !ok {
		r.HTML(http.StatusOK, "error", template)
		return
	}
	c.Map(token)
	c.Next()
	http.Redirect(res, req, "/award", http.StatusFound)
}
開發者ID:nquinlan,項目名稱:contribot,代碼行數:56,代碼來源:github.go

示例10: ExternalServiceAuthorizationProvider

func ExternalServiceAuthorizationProvider(ds *appx.Datastore, martiniContext martini.Context, account *models.Account) {
	authorization := &models.ExternalServiceAuthorization{}
	authorization.SetParentKey(account.Key())

	if err := ds.Load(authorization); err != nil {
		panic(err)
	}

	martiniContext.Map(authorization)
}
開發者ID:BearchInc,項目名稱:trails-api,代碼行數:10,代碼來源:external_service_authorization_provider.go

示例11: authorize

//The authorize middleware will search the session for a username
//if it doesnt find it, it will redirect to login
func authorize(w http.ResponseWriter, r *http.Request, session sessions.Session, c martini.Context) {
	username := session.Get("username")
	if username == nil {
		http.Redirect(w, r, "/login", http.StatusFound)
	}
	//if we found the user, let's create a new user struct and map it into the request context
	user := &User{}
	user.Username = username.(string)
	c.Map(user)
}
開發者ID:kvannotten,項目名稱:projects,代碼行數:12,代碼來源:server.go

示例12: Authenticate

func Authenticate(w http.ResponseWriter, r *http.Request, c martini.Context, enc encoder.Encoder) {
	db := GetDbSession()
	token := r.Header.Get("X-API-TOKEN")
	user := User{}
	err := db.SelectOne(&user, "select * from users where token=?", token)
	if err != nil {
		http.Error(w, "Auth Error", http.StatusUnauthorized)
	}
	c.Map(user)
}
開發者ID:geoah,項目名稱:42minutes-server-api,代碼行數:10,代碼來源:main.go

示例13: RequireLogin

/*
 * Web functions
 */
func RequireLogin(rw http.ResponseWriter, req *http.Request, s sessions.Session,
	db_ *mgo.Database, c martini.Context) {
	user, err := db.GetUserById(bson.ObjectIdHex(s.Get("userId").(string)), db_)
	if err != nil {
		fmt.Println(err)
		http.Redirect(rw, req, PAGE_LOGIN, http.StatusFound)
		return
	}
	c.Map(user)
}
開發者ID:serash,項目名稱:YunYun,代碼行數:13,代碼來源:auth.go

示例14: Middleware

// Middleware ...
func Middleware(ctx martini.Context, r *http.Request, w http.ResponseWriter) {
	sessionID := ensureCookie(r, w)
	session := sessionStore.Get(sessionID)

	ctx.Map(session)

	ctx.Next()

	sessionStore.Set(session)
}
開發者ID:batazor-tutorial,項目名稱:go-blog-example,代碼行數:11,代碼來源:session.go

示例15: filter

func filter(req *http.Request, c martini.Context) {
	req.ParseForm()
	m := make(FilterInfo)
	for k, v := range req.Form {
		if strings.HasPrefix(k, "f_") {
			field := strings.Split(k, "f_")[1]
			m[field] = v[0]
		}
	}
	c.Map(m)
}
開發者ID:ngaut,項目名稱:tyrant,代碼行數:11,代碼來源:rest_api.go


注:本文中的github.com/go-martini/martini.Context.Map方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。