本文整理匯總了Golang中github.com/yarf-framework/yarf.Context類的典型用法代碼示例。如果您正苦於以下問題:Golang Context類的具體用法?Golang Context怎麽用?Golang Context使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Context類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Get
// Implement a GET handler that panics
func (p *Panic) Get(c *yarf.Context) error {
c.Render("I'm panicking!")
panic("Totally panicking!")
return nil
}
示例2: Get
// Get implements a GET handler that panics
func (p *Panic) Get(c *yarf.Context) error {
c.Render("I'm panicking!")
panic("Totally panicking!")
// The next line is unreachable (govet)
//return nil
}
示例3: Render
// Render reads the content of the path built using the name param together with the object properties.
// It outputs the content of the readed file or returns an error if that fails.
func (l *LayoutMiddleware) Render(name string, c *yarf.Context) error {
content, err := l.Cached(l.TplPath + "/" + l.TplName + "/layout/" + name + ".tpl")
if err != nil {
return yarf.ErrorNotFound()
}
c.Render(content)
return nil
}
示例4: TestResourceHello
func TestResourceHello(t *testing.T) {
h := new(Hello)
c := new(yarf.Context)
c.Request, _ = http.NewRequest("GET", "/", nil)
c.Response = httptest.NewRecorder()
err := h.Get(c)
if err != nil {
t.Error(err.Error())
}
}
示例5: Get
// Get implements the GET handler with optional name parameter
func (h *HelloV2) Get(c *yarf.Context) error {
name := c.Param("name")
salute := "(v2) Hello"
if name != "" {
salute += ", " + name
}
salute += "!"
c.Render(salute)
return nil
}
示例6: PreDispatch
// PreDispatch wraps the http.ResponseWriter with a new LoggerWritter
// so we can log information about the response.
func (l *Logger) PreDispatch(c *yarf.Context) error {
c.Response = &LoggerWriter{
Writer: c.Response,
}
return nil
}
示例7: TestExtra
func TestExtra(t *testing.T) {
e := new(ExtraMiddleware)
c := new(yarf.Context)
c.Request, _ = http.NewRequest("GET", "/", nil)
c.Response = httptest.NewRecorder()
err := e.PreDispatch(c)
if err != nil {
t.Error(err.Error())
}
err = e.PostDispatch(c)
if err != nil {
t.Error(err.Error())
}
}
示例8: TestHello
func TestHello(t *testing.T) {
h := new(Hello)
c := new(yarf.Context)
c.Request, _ = http.NewRequest("GET", "/", nil)
c.Response = httptest.NewRecorder()
err := h.PreDispatch(c)
if err != nil {
t.Error(err.Error())
}
err = h.PostDispatch(c)
if err != nil {
t.Error(err.Error())
}
}
示例9: End
func (l *Logger) End(c *yarf.Context) error {
// If nobody sets the status code, it's a 200
var code int
if _, ok := c.Response.(*LoggerWriter); ok {
code = c.Response.(*LoggerWriter).StatusCode
}
if code == 0 {
code = 200
}
log.Printf(
"| %s | %s | %d | %s",
c.GetClientIP(),
c.Request.Method,
code,
c.Request.URL.String(),
)
return nil
}
示例10: Render
// Render reads the content of the path built using the c.URL.Path together with the object's TplPath.
// It outputs the content of the readed file or returns an error if that fails.
func (v *ViewResource) Render(c *yarf.Context) error {
pre, err := v.Cached(v.TplPath + "/" + v.TplName + "/layout/pre.tpl")
if err != nil {
return yarf.ErrorNotFound()
}
content, err := v.Cached(v.TplPath + "/" + v.TplName + "/view" + strings.TrimSuffix(c.Request.URL.EscapedPath(), "/") + ".tpl")
if err != nil {
// Try index.tpl
content, err = v.Cached(v.TplPath + "/" + v.TplName + "/view" + strings.TrimSuffix(c.Request.URL.EscapedPath(), "/") + "/index.tpl")
if err != nil {
return yarf.ErrorNotFound()
}
}
post, err := v.Cached(v.TplPath + "/" + v.TplName + "/layout/post.tpl")
if err != nil {
return yarf.ErrorNotFound()
}
c.Render(pre)
c.Render(content)
c.Render(post)
return nil
}
示例11: Get
func (y *YarfMulti) Get(c *yarf.Context) error {
c.Render("Hello " + c.Param("name1") + "," + c.Param("name2") + "," + c.Param("name3") + "," + c.Param("name4"))
return nil
}
示例12: Get
func (y *YarfParam) Get(c *yarf.Context) error {
c.Render("Hello, " + c.Param("name"))
return nil
}
示例13: Get
func (y *YarfHello) Get(c *yarf.Context) error {
c.Render("Hello world!")
return nil
}
示例14: PreDispatch
// PreDispatch creates the StrData object and sets it on the Context.Data property.
func (m *SetStrData) PreDispatch(c *yarf.Context) error {
c.Data = new(StrData)
return nil
}
示例15: PostDispatch
func (m *ExtraMiddleware) PostDispatch(c *yarf.Context) error {
c.Render("\n\nExtra from nested middleware!")
return nil
}