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


Golang iris.Get函数代码示例

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


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

示例1: TestCache

func TestCache(t *testing.T) {

	iris.ResetDefault()

	expectedBodyStr := "Imagine it as a big message to achieve x20 response performance!"
	var textCounter, htmlCounter uint32

	iris.Get("/text", iris.Cache(func(ctx *iris.Context) {
		atomic.AddUint32(&textCounter, 1)
		ctx.Text(iris.StatusOK, expectedBodyStr)
	}, cacheDuration))

	iris.Get("/html", iris.Cache(func(ctx *iris.Context) {
		atomic.AddUint32(&htmlCounter, 1)
		ctx.HTML(iris.StatusOK, expectedBodyStr)
	}, cacheDuration))

	e := httptest.New(iris.Default, t)

	// test cache on text/plain
	if err := runCacheTest(e, "/text", &textCounter, expectedBodyStr, "text/plain"); err != nil {
		t.Fatal(err)
	}

	// text cache on text/html
	if err := runCacheTest(e, "/html", &htmlCounter, expectedBodyStr, "text/html"); err != nil {
		t.Fatal(err)
	}
}
开发者ID:kataras,项目名称:iris,代码行数:29,代码来源:http_test.go

示例2: TestContextRedirectTo

// TestContextRedirectTo tests the named route redirect action
func TestContextRedirectTo(t *testing.T) {
	iris.ResetDefault()
	h := func(ctx *iris.Context) { ctx.Write(ctx.PathString()) }
	iris.Get("/mypath", h)("my-path")
	iris.Get("/mypostpath", h)("my-post-path")
	iris.Get("mypath/with/params/:param1/:param2", func(ctx *iris.Context) {
		if l := ctx.ParamsLen(); l != 2 {
			t.Fatalf("Strange error, expecting parameters to be two but we got: %d", l)
		}
		ctx.Write(ctx.PathString())
	})("my-path-with-params")

	iris.Get("/redirect/to/:routeName/*anyparams", func(ctx *iris.Context) {
		routeName := ctx.Param("routeName")
		var args []interface{}
		anyparams := ctx.Param("anyparams")
		if anyparams != "" && anyparams != "/" {
			params := strings.Split(anyparams[1:], "/") // firstparam/secondparam
			for _, s := range params {
				args = append(args, s)
			}
		}
		//println("Redirecting to: " + routeName + " with path: " + Path(routeName, args...))
		ctx.RedirectTo(routeName, args...)
	})

	e := httptest.New(iris.Default, t)

	e.GET("/redirect/to/my-path/").Expect().Status(iris.StatusOK).Body().Equal("/mypath")
	e.GET("/redirect/to/my-post-path/").Expect().Status(iris.StatusOK).Body().Equal("/mypostpath")
	e.GET("/redirect/to/my-path-with-params/firstparam/secondparam").Expect().Status(iris.StatusOK).Body().Equal("/mypath/with/params/firstparam/secondparam")
}
开发者ID:kataras,项目名称:iris,代码行数:33,代码来源:context_test.go

示例3: main

func main() {
	flag.Parse()

	iris.Use(logger.New(iris.Logger()))
	iris.Use(cors.DefaultCors())

	iris.Get("/", controller.Index)
	iris.Get("/course/:courseID/:lessonID", controller.GetCourse)

	iris.Post("/submit/:classID/:courseID/:lessonID/:qn", controller.SubmitCode)

	fmt.Printf("listen on %s\n", *port)
	iris.Listen(*port)
}
开发者ID:tungshuan,项目名称:plweb-api-server,代码行数:14,代码来源:server.go

示例4: TestContextHostString

// hoststring returns the full host, will return the HOST:IP
func TestContextHostString(t *testing.T) {
	iris.ResetDefault()
	iris.Default.Config.VHost = "0.0.0.0:8080"
	iris.Get("/", func(ctx *iris.Context) {
		ctx.Write(ctx.HostString())
	})

	iris.Get("/wrong", func(ctx *iris.Context) {
		ctx.Write(ctx.HostString() + "w")
	})

	e := httptest.New(iris.Default, t)
	e.GET("/").Expect().Status(iris.StatusOK).Body().Equal(iris.Default.Config.VHost)
	e.GET("/wrong").Expect().Body().NotEqual(iris.Default.Config.VHost)
}
开发者ID:kataras,项目名称:iris,代码行数:16,代码来源:context_test.go

示例5: main

func main() {
	dbinfo := fmt.Sprintf("port=32768 user=%s password=%s dbname=%s sslmode=disable", "postgres", "pass123", "postgres")
	db, err := sql.Open("postgres", dbinfo)
	if err != nil {
		fmt.Printf("err: %+v ", err)
	}
	defer db.Close()

	iris.Get("/people", GetAll(db))
	iris.Get("/people/:id", Get(db))
	iris.Post("/people/:id", Post(db))
	iris.Delete("/people/:id", Delete(db))

	iris.Listen(":8000")
}
开发者ID:gowroc,项目名称:meetups,代码行数:15,代码来源:main.go

示例6: TestContextVirtualHostName

// VirtualHostname returns the hostname only,
// if the host starts with 127.0.0.1 or localhost it gives the registered hostname part of the listening addr
func TestContextVirtualHostName(t *testing.T) {
	iris.ResetDefault()
	vhost := "mycustomvirtualname.com"
	iris.Default.Config.VHost = vhost + ":8080"
	iris.Get("/", func(ctx *iris.Context) {
		ctx.Write(ctx.VirtualHostname())
	})

	iris.Get("/wrong", func(ctx *iris.Context) {
		ctx.Write(ctx.VirtualHostname() + "w")
	})

	e := httptest.New(iris.Default, t)
	e.GET("/").Expect().Status(iris.StatusOK).Body().Equal(vhost)
	e.GET("/wrong").Expect().Body().NotEqual(vhost)
}
开发者ID:kataras,项目名称:iris,代码行数:18,代码来源:context_test.go

示例7: main

func main() {
	iris.Get("/hello/:name", func(c *iris.Context) {
		name := c.Param("name")
		c.Write("Hello " + name)
	})
	iris.Listen(":8080")
}
开发者ID:exu,项目名称:go-workshops,代码行数:7,代码来源:iris.go

示例8: main

func main() {

	// register global middleware, you can pass more than one handler comma separated
	iris.UseFunc(func(c *iris.Context) {
		fmt.Println("(1)Global logger: %s", c.PathString())
		c.Next()
	})

	// register a global structed iris.Handler as middleware
	myglobal := MyGlobalMiddlewareStructed{loggerId: "my logger id"}
	iris.Use(myglobal)

	// register route's middleware
	iris.Get("/home", func(c *iris.Context) {
		fmt.Println("(1)HOME logger for /home")
		c.Next()
	}, func(c *iris.Context) {
		fmt.Println("(2)HOME logger for /home")
		c.Next()
	}, func(c *iris.Context) {
		c.Write("Hello from /home")
	})

	println("Iris is listening on :8080")
	iris.Listen("8080")
}
开发者ID:cyy0523xc,项目名称:code,代码行数:26,代码来源:iris-test2.go

示例9: main

func main() {
	iris.Get("/hi", func(ctx *iris.Context) {
		ctx.Write("Hi %s", "iris")
	})
	iris.Listen(":8080")
	//err := iris.ListenWithErr(":8080")
}
开发者ID:cyy0523xc,项目名称:code,代码行数:7,代码来源:iris-test.go

示例10: main

func main() {
	mongoSession, pastas, err := database.NewPastasConnection()
	if err != nil {
		log.Fatal(err)
	}

	iris.UseTemplate(html.New(html.Config{
		Layout: "layout.html",
	})).Directory("./templates", ".html")

	iris.Static("/public", "./static", 1)

	iris.Get("/", func(ctx *iris.Context) {
		ctx.Render("home.html", Page{"Tonnarello", Pasta{"null", "null", "null"}}, iris.RenderOptions{"gzip": true})
	})

	iris.Post("/insert", func(ctx *iris.Context) {
		pasta := Pasta{}
		err := ctx.ReadForm(&pasta)
		if err != nil {
			fmt.Printf("ERR")
		}

		pasta.Id = bson.NewObjectId()

		err = pastas.Insert(pasta)
		if err != nil {
			log.Fatal(err)
		}

		ctx.Redirect("pasta/"+pasta.Id.Hex(), http.StatusSeeOther)
	})

	iris.Get("/pasta/:id", func(ctx *iris.Context) {
		objId := bson.ObjectIdHex(ctx.Param("id"))
		pasta := &Pasta{}

		pastas.FindId(objId).One(pasta)
		ctx.Render("pasta.html", Page{"Tonnarello", pasta}, iris.RenderOptions{"gzip": true})
	})

	iris.Listen(":4000")

	defer mongoSession.Close()
}
开发者ID:dottorblaster,项目名称:tonnarello,代码行数:45,代码来源:main.go

示例11: TestContextCookieSetGetRemove

func TestContextCookieSetGetRemove(t *testing.T) {
	iris.ResetDefault()
	key := "mykey"
	value := "myvalue"
	iris.Get("/set", func(ctx *iris.Context) {
		ctx.SetCookieKV(key, value) // should return non empty cookies
	})

	iris.Get("/set_advanced", func(ctx *iris.Context) {
		c := fasthttp.AcquireCookie()
		c.SetKey(key)
		c.SetValue(value)
		c.SetHTTPOnly(true)
		c.SetExpire(time.Now().Add(time.Duration((60 * 60 * 24 * 7 * 4)) * time.Second))
		ctx.SetCookie(c)
		fasthttp.ReleaseCookie(c)
	})

	iris.Get("/get", func(ctx *iris.Context) {
		ctx.Write(ctx.GetCookie(key)) // should return my value
	})

	iris.Get("/remove", func(ctx *iris.Context) {
		ctx.RemoveCookie(key)
		cookieFound := false
		ctx.VisitAllCookies(func(k, v string) {
			cookieFound = true
		})
		if cookieFound {
			t.Fatalf("Cookie has been found, when it shouldn't!")
		}
		ctx.Write(ctx.GetCookie(key)) // should return ""
	})

	e := httptest.New(iris.Default, t)
	e.GET("/set").Expect().Status(iris.StatusOK).Cookies().NotEmpty()
	e.GET("/get").Expect().Status(iris.StatusOK).Body().Equal(value)
	e.GET("/remove").Expect().Status(iris.StatusOK).Body().Equal("")
	// test again with advanced set
	e.GET("/set_advanced").Expect().Status(iris.StatusOK).Cookies().NotEmpty()
	e.GET("/get").Expect().Status(iris.StatusOK).Body().Equal(value)
	e.GET("/remove").Expect().Status(iris.StatusOK).Body().Equal("")
}
开发者ID:kataras,项目名称:iris,代码行数:43,代码来源:context_test.go

示例12: TestContextURLParams

func TestContextURLParams(t *testing.T) {
	iris.ResetDefault()
	passedParams := map[string]string{"param1": "value1", "param2": "value2"}
	iris.Get("/", func(ctx *iris.Context) {
		params := ctx.URLParams()
		ctx.JSON(iris.StatusOK, params)
	})
	e := httptest.New(iris.Default, t)

	e.GET("/").WithQueryObject(passedParams).Expect().Status(iris.StatusOK).JSON().Equal(passedParams)
}
开发者ID:kataras,项目名称:iris,代码行数:11,代码来源:context_test.go

示例13: main

func main() {
	fmt.Println("Hello World!")
	iris.Get("/hi_json", func(c *iris.Context) {
		c.JSON(200, iris.Map{
			"Name": "Iris",
			"Age":  2,
			"名称":   "产品名称",
		})
	})
	/iris.Post()
	//iris.ListenTLS(":8080", "server.crt", "server.key")
}
开发者ID:kazarus,项目名称:GoDemo,代码行数:12,代码来源:main.go

示例14: TestMuxDecodeURL

func TestMuxDecodeURL(t *testing.T) {
	iris.ResetDefault()

	iris.Get("/encoding/:url", func(ctx *iris.Context) {
		url := iris.DecodeURL(ctx.Param("url"))
		ctx.SetStatusCode(iris.StatusOK)
		ctx.Write(url)
	})

	e := httptest.New(iris.Default, t)

	e.GET("/encoding/http%3A%2F%2Fsome-url.com").Expect().Status(iris.StatusOK).Body().Equal("http://some-url.com")
}
开发者ID:kataras,项目名称:iris,代码行数:13,代码来源:http_test.go

示例15: TestMultiRunningServers_v1_PROXY

// Contains the server test for multi running servers
func TestMultiRunningServers_v1_PROXY(t *testing.T) {
	defer iris.Close()
	host := "localhost" // you have to add it to your hosts file( for windows, as 127.0.0.1 mydomain.com)
	hostTLS := "localhost:9999"
	iris.Close()
	defer iris.Close()
	iris.ResetDefault()
	iris.Default.Config.DisableBanner = true
	// create the key and cert files on the fly, and delete them when this test finished
	certFile, ferr := ioutil.TempFile("", "cert")

	if ferr != nil {
		t.Fatal(ferr.Error())
	}

	keyFile, ferr := ioutil.TempFile("", "key")
	if ferr != nil {
		t.Fatal(ferr.Error())
	}

	defer func() {
		certFile.Close()
		time.Sleep(350 * time.Millisecond)
		os.Remove(certFile.Name())

		keyFile.Close()
		time.Sleep(350 * time.Millisecond)
		os.Remove(keyFile.Name())
	}()

	certFile.WriteString(testTLSCert)
	keyFile.WriteString(testTLSKey)

	iris.Get("/", func(ctx *iris.Context) {
		ctx.Write("Hello from %s", hostTLS)
	})

	go iris.ListenTLS(hostTLS, certFile.Name(), keyFile.Name())
	if ok := <-iris.Default.Available; !ok {
		t.Fatal("Unexpected error: server cannot start, please report this as bug!!")
	}

	closeProxy := iris.Proxy("localhost:8080", "https://"+hostTLS)
	defer closeProxy()

	e := httptest.New(iris.Default, t, httptest.ExplicitURL(true))

	e.Request("GET", "http://"+host+":8080").Expect().Status(iris.StatusOK).Body().Equal("Hello from " + hostTLS)
	e.Request("GET", "https://"+hostTLS).Expect().Status(iris.StatusOK).Body().Equal("Hello from " + hostTLS)

}
开发者ID:kataras,项目名称:iris,代码行数:52,代码来源:http_test.go


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