当前位置: 首页>>代码示例>>Golang>>正文


Golang iris.Context类代码示例

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


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

示例1: TestGetFromContextNoIp

func TestGetFromContextNoIp(t *testing.T) {
	var ctx iris.Context
	ctx.RequestCtx = &fasthttp.RequestCtx{}
	ip := GetFromContext(&ctx)

	assert.Equal(t, ip, "", "Should not find an ip address.")
}
开发者ID:vaot,项目名称:user_ip,代码行数:7,代码来源:user_ip_test.go

示例2: TestGetFromContextWithGlobalAndPrivateForwardedIps

func TestGetFromContextWithGlobalAndPrivateForwardedIps(t *testing.T) {
	var ctx iris.Context
	ctx.RequestCtx = &fasthttp.RequestCtx{}
	header := fasthttp.RequestHeader{}
	header.Add("X-Forwarded-For", "65.55.37.104, 100.64.0.0, 192.168.0.0, 192.0.0.0")
	ctx.RequestCtx.Request.Header = header
	ip := GetFromContext(&ctx)

	assert.Equal(t, ip, "65.55.37.104", "Should be the first ip, which is the global ip address.")
}
开发者ID:vaot,项目名称:user_ip,代码行数:10,代码来源:user_ip_test.go

示例3: TestGetFromContextWithPrivateForwadedIp

func TestGetFromContextWithPrivateForwadedIp(t *testing.T) {
	var ctx iris.Context
	ctx.RequestCtx = &fasthttp.RequestCtx{}
	header := fasthttp.RequestHeader{}
	header.Add("X-Forwarded-For", "100.64.0.0, 192.168.0.0")
	ctx.RequestCtx.Request.Header = header
	ip := GetFromContext(&ctx)

	assert.Equal(t, ip, "", "Should not find an ip address.")
}
开发者ID:vaot,项目名称:user_ip,代码行数:10,代码来源:user_ip_test.go

示例4: Serve

func (r recovery) Serve(ctx *iris.Context) {
	defer func() {
		if err := recover(); err != nil {
			r.out.Write([]byte("[" + time.Now().String() + "]Recovery from panic \n"))
			//ctx.Panic just sends  http status 500 by default, but you can change it by: iris.OnPanic(func( c *iris.Context){})
			ctx.Panic()
		}
	}()
	ctx.Next()
}
开发者ID:cokeboL,项目名称:iris,代码行数:10,代码来源:recovery.go

示例5: getMessage

func getMessage(c *iris.Context) {
	t := sb.NewTable(
		"chats",
		sb.StrColumn("roomid", sb.UTF8, sb.UTF8CaseSensitive, false),
		sb.StrColumn("text", sb.UTF8, sb.UTF8CaseSensitive, false),
	)
	query, _ := t.Select(t.C("roomid"), t.C("text")).String("chat")
	rows, err := db.Query(query)
	if err != nil {
		log.Fatal(err)
	}
	var messages []string
	for rows.Next() {
		var roomid string
		var text string
		if err := rows.Scan(&roomid, &text); err != nil {
			log.Fatal(err)
		}
		messages = append(messages, text)
	}
	c.JSON(iris.StatusOK, map[string][]string{"messages": messages})
}
开发者ID:yubaken,项目名称:GoWebSocketDockerSample,代码行数:22,代码来源:main.go

示例6: Serve

// Serve the actual middleware
func (b *basicAuthMiddleware) Serve(ctx *iris.Context) {

	if auth, found := b.findAuth(ctx.RequestHeader("Authorization")); !found {
		/* I spent time for nothing
		if b.banEnabled && auth != nil { // this propably never work

			if auth.tries == b.config.MaxTries {
				auth.bannedTime = time.Now()
				auth.unbanTime = time.Now().Add(b.config.BanDuration) // set the unban time
				auth.tries++                                          // we plus them in order to check if already banned later
				// client is banned send a forbidden status and don't continue
				ctx.SetStatusCode(iris.StatusForbidden)
				return
			} else if auth.tries > b.config.MaxTries { // it's already banned, so check the ban duration with the bannedTime
				if time.Now().After(auth.unbanTime) { // here we unban the client
					auth.tries = 0
					auth.bannedTime = config.CookieExpireNever
					auth.unbanTime = config.CookieExpireNever
					// continue and askCredentials as normal
				} else {
					// client is banned send a forbidden status and don't continue
					ctx.SetStatusCode(iris.StatusForbidden)
					return
				}

			}
		}
		if auth != nil {
			auth.tries++
		}*/

		b.askForCredentials(ctx)
		// don't continue to the next handler
	} else {
		// all ok set the context's value in order to be getable from the next handler
		ctx.Set(b.config.ContextKey, auth.Username)
		if b.expireEnabled {

			if auth.logged == false {
				auth.expires = time.Now().Add(b.config.Expires)
				auth.logged = true
			}

			if time.Now().After(auth.expires) {
				b.askForCredentials(ctx) // ask for authentication again
				return
			}

		}

		//auth.tries = 0
		ctx.Next() // continue
	}

}
开发者ID:cokeboL,项目名称:iris,代码行数:56,代码来源:basicauth.go

示例7: Serve

// Serve serves the middleware
func (l *LoggerMiddleware) Serve(ctx *iris.Context) {
	//all except latency to string
	var date, status, ip, method, path string
	var latency time.Duration
	var startTime, endTime time.Time

	status = strconv.Itoa(ctx.Response.StatusCode())
	ip = ctx.RemoteAddr()
	path = ctx.PathString()
	method = ctx.MethodString()

	startTime = time.Now()
	ctx.Next()
	//no time.Since in order to format it well after
	endTime = time.Now()
	date = endTime.Format("01/02 - 15:04:05")
	latency = endTime.Sub(startTime)

	//finally print the logs
	l.printf("%s %v %4v %s %s %s", date, status, latency, ip, method, path)
}
开发者ID:sillydong,项目名称:goczd,代码行数:22,代码来源:logger.go

示例8: Serve

func (m *myTestCustomHandler) Serve(ctx *iris.Context) {
	data := &m.data
	data.DynamicPathParameter = ctx.Param("myparam")
	ctx.JSON(iris.StatusOK, data)
}
开发者ID:kataras,项目名称:iris,代码行数:5,代码来源:http_test.go

示例9: irisHandler

// iris
func irisHandler(c *iris.Context) {
	if sleepTime > 0 {
		time.Sleep(sleepTimeDuration)
	}
	c.SetBody(message)
}
开发者ID:cokeboL,项目名称:go-web-framework-benchmark,代码行数:7,代码来源:server.go

示例10: TestContextDoNextStop

// White-box testing *
func TestContextDoNextStop(t *testing.T) {
	var context iris.Context
	ok := false
	afterStop := false
	context.Middleware = iris.Middleware{iris.HandlerFunc(func(*iris.Context) {
		ok = true
	}), iris.HandlerFunc(func(*iris.Context) {
		ok = true
	}), iris.HandlerFunc(func(*iris.Context) {
		// this will never execute
		afterStop = true
	})}
	context.Do()
	if context.Pos != 0 {
		t.Fatalf("Expecting position 0 for context's middleware but we got: %d", context.Pos)
	}
	if !ok {
		t.Fatalf("Unexpected behavior, first context's middleware didn't executed")
	}
	ok = false

	context.Next()

	if int(context.Pos) != 1 {
		t.Fatalf("Expecting to have position %d but we got: %d", 1, context.Pos)
	}
	if !ok {
		t.Fatalf("Next context's middleware didn't executed")
	}

	context.StopExecution()
	if context.Pos != 255 {
		t.Fatalf("Context's StopExecution didn't worked, we expected to have position %d but we got %d", 255, context.Pos)
	}

	if !context.IsStopped() {
		t.Fatalf("Should be stopped")
	}

	context.Next()

	if afterStop {
		t.Fatalf("We stopped the execution but the next handler was executed")
	}
}
开发者ID:kataras,项目名称:iris,代码行数:46,代码来源:context_test.go

示例11: askForCredentials

func (b *basicAuthMiddleware) askForCredentials(ctx *iris.Context) {
	ctx.SetHeader("WWW-Authenticate", b.realmHeaderValue)
	ctx.SetStatusCode(iris.StatusUnauthorized)
}
开发者ID:cokeboL,项目名称:iris,代码行数:4,代码来源:basicauth.go

示例12: Serve

//Important staff, iris middleware must implement the iris.Handler interface which is:
func (m MyGlobalMiddlewareStructed) Serve(c *iris.Context) {
	fmt.Println("Hello from logger with id: ", m.loggerId)
	c.Next()
}
开发者ID:cyy0523xc,项目名称:code,代码行数:5,代码来源:iris-test2.go

示例13: handleError

func handleError(ctx *iris.Context, message string, err error) {
	fmt.Printf("%v", err)
	ctx.Error(message, 503)
}
开发者ID:gowroc,项目名称:meetups,代码行数:4,代码来源:main.go


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