Exemplo n.º 1
0
// Implement a GET handler that panics
func (p *Panic) Get(c *yarf.Context) error {
	c.Render("I'm panicking!")

	panic("Totally panicking!")

	return nil
}
Exemplo n.º 2
0
// 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
}
Exemplo n.º 3
0
// 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
}
Exemplo n.º 4
0
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())
	}
}
Exemplo n.º 5
0
// 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
}
Exemplo n.º 6
0
// 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
}
Exemplo n.º 7
0
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())
	}
}
Exemplo n.º 8
0
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())
	}
}
Exemplo n.º 9
0
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
}
Exemplo n.º 10
0
// 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
}
Exemplo n.º 11
0
// PreDispatch performs the requests counting and handle blocks/
func (m *RateLimiter) PreDispatch(c *yarf.Context) error {
	// IP as key
	key := c.GetClientIP()

	// Count
	err := m.rl.Count(key)
	if err != nil {
		if _, ok := err.(RateLimitError); ok {
			return &YarfError{}
		}

		return err
	}

	// Set rate limit info on headers
	rate := m.rl.Get(key)
	c.Response.Header().Set("X-RateLimit-Limit", strconv.Itoa(int(rate.Limit)))
	c.Response.Header().Set("X-RateLimit-Remaining", strconv.Itoa(int(rate.Limit-rate.EventCount)))
	c.Response.Header().Set("X-RateLimit-Reset", strconv.Itoa(int(rate.Start.Add(time.Second*time.Duration(rate.Window)).Unix())))

	return nil
}
Exemplo n.º 12
0
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
}
Exemplo n.º 13
0
func (y *YarfParam) Get(c *yarf.Context) error {
	c.Render("Hello, " + c.Param("name"))

	return nil
}
Exemplo n.º 14
0
func (y *YarfHello) Get(c *yarf.Context) error {
	c.Render("Hello world!")

	return nil
}
Exemplo n.º 15
0
// 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
}
Exemplo n.º 16
0
func (m *ExtraMiddleware) PostDispatch(c *yarf.Context) error {
	c.Render("\n\nExtra from nested middleware!")

	return nil
}
Exemplo n.º 17
0
// PreDispatch renders a hardcoded string
func (m *HelloMiddleware) PreDispatch(c *yarf.Context) error {
	c.Render("Hello from middleware! \n\n")

	return nil
}
Exemplo n.º 18
0
// PostDispatch includes code to be executed after every Resource request.
func (m *HelloMiddleware) PostDispatch(c *yarf.Context) error {
	c.Render("\n\nGoodbye from middleware!")

	return nil
}