Ejemplo n.º 1
0
func main() {

	l := lars.New()
	l.Use(Logger)

	l.Get("/", HelloWorld)

	http.ListenAndServe(":3007", l.Serve())
}
Ejemplo n.º 2
0
func main() {

	l := lars.New()
	l.RegisterContext(newContext) // all gets cached in pools for you
	l.RegisterCustomHandler(func(*MyContext) {}, castCustomContext)
	l.Use(Logger)

	l.Get("/", Home)

	http.ListenAndServe(":3007", l.Serve())
}
Ejemplo n.º 3
0
func main() {

	var err error

	t, err = template.New("foo").Parse(`{{define "T"}}<html><body><p>Hello, {{.}}!</p></body></html>{{end}}`)
	if err != nil {
		log.Fatal("Error Creating Template")
	}

	fs := http.StripPrefix("/assets/", http.FileServer(http.Dir("assets")))

	l := lars.New()
	l.RegisterGlobalsFunc(func() lars.IGlobals { return new(Globals) })
	// nc := func() l.Context {
	// 	return &CustomContext{rum.NewContext(nil, rum.NewResponse(nil), r)}
	// }

	// r.SetContextNew(nc)

	l.Use(Logger())
	l.Use(Gzip())
	// l.Use(func(h rum.HandlerFunc) rum.HandlerFunc {
	// 	return func(ctx rum.Context) {
	// 		h(&CustomContext{ctx})
	// 	}
	// })
	l.Get("/", root)
	l.Get("/teachers/list", route1)
	l.Get("/teachers/:id/profile", route2)
	// l.Get("/assets/*filepath", func(c *lars.Context) {
	// 	fs.ServeHTTP(c.Response, c.Request)
	// })

	l.Get("/assets/*filepath", fs)

	// g1 := l.Group("/teachers/")
	// // g1.Use(Logger2())
	// g1.Get("list/", route1)
	// // g1.Get("a/", route1a)
	// g1.Get(":id/profile", route2)

	// g2 := l.Group("/route2/")
	// // g2.Use(Logger2())
	// g2.Get("", route2)
	// g2.Get(":name/", route2a)

	http.ListenAndServe(":3006", l)
}
Ejemplo n.º 4
0
func main() {

	l := lars.New()
	l.RegisterContext(newContext) // all gets cached in pools for you
	l.Use(Logger)

	l.Get("/", Home)

	users := l.Group("/users")
	users.Get("", Users)

	// you can break it up however you with, just demonstrating that you can
	// have groups of group
	user := users.Group("/:id")
	user.Get("", User)
	user.Get("/profile", UserProfile)

	http.ListenAndServe(":3007", l.Serve())
}
Ejemplo n.º 5
0
func TestGzipLevel(t *testing.T) {

	// bad gzip level
	PanicMatches(t, func() { GzipLevel(999) }, "gzip: invalid compression level: 999")

	l := lars.New()
	l.Use(GzipLevel(flate.BestCompression))
	l.Get("/test", func(c lars.Context) {
		c.Response().Write([]byte("test"))
	})

	server := httptest.NewServer(l.Serve())
	defer server.Close()

	req, _ := http.NewRequest(lars.GET, server.URL+"/test", nil)

	client := &http.Client{}

	resp, err := client.Do(req)
	Equal(t, err, nil)
	Equal(t, resp.StatusCode, http.StatusOK)

	b, err := ioutil.ReadAll(resp.Body)
	Equal(t, err, nil)
	Equal(t, string(b), "test")

	req, _ = http.NewRequest(lars.GET, server.URL+"/test", nil)
	req.Header.Set(lars.AcceptEncoding, "gzip")

	resp, err = client.Do(req)
	Equal(t, err, nil)
	Equal(t, resp.StatusCode, http.StatusOK)
	Equal(t, resp.Header.Get(lars.ContentEncoding), lars.Gzip)
	Equal(t, resp.Header.Get(lars.ContentType), lars.TextPlainCharsetUTF8)

	r, err := gzip.NewReader(resp.Body)
	Equal(t, err, nil)
	defer r.Close()

	b, err = ioutil.ReadAll(r)
	Equal(t, err, nil)
	Equal(t, string(b), "test")
}
Ejemplo n.º 6
0
func main() {

	l := lars.New()
	l.Use(Logger)

	l.Get("/", Home)

	users := l.Group("/users")
	users.Get("", Users)
	users.Get("/:id", User)
	users.Get("/:id/profile", UserProfile)

	admins := l.Group("/admins")
	admins.Get("", Admins)
	admins.Get("/:id", Admin)
	admins.Get("/:id/profile", AdminProfile)

	http.ListenAndServe(":3007", l.Serve())
}
Ejemplo n.º 7
0
func TestGzip(t *testing.T) {
	l := lars.New()
	l.Use(Gzip)
	l.Get("/test", func(c lars.Context) {
		c.Response().Write([]byte("test"))
	})

	server := httptest.NewServer(l.Serve())
	defer server.Close()

	req, _ := http.NewRequest(lars.GET, server.URL+"/test", nil)

	client := &http.Client{}

	resp, err := client.Do(req)
	Equal(t, err, nil)
	Equal(t, resp.StatusCode, http.StatusOK)

	b, err := ioutil.ReadAll(resp.Body)
	Equal(t, err, nil)
	Equal(t, string(b), "test")

	req, _ = http.NewRequest(lars.GET, server.URL+"/test", nil)
	req.Header.Set(lars.AcceptEncoding, "gzip")

	resp, err = client.Do(req)
	Equal(t, err, nil)
	Equal(t, resp.StatusCode, http.StatusOK)
	Equal(t, resp.Header.Get(lars.ContentEncoding), lars.Gzip)
	Equal(t, resp.Header.Get(lars.ContentType), lars.TextPlainCharsetUTF8)

	r, err := gzip.NewReader(resp.Body)
	Equal(t, err, nil)
	defer r.Close()

	b, err = ioutil.ReadAll(r)
	Equal(t, err, nil)
	Equal(t, string(b), "test")
}
Ejemplo n.º 8
0
func startLars() {
	mux := lars.New()
	mux.Get("/hello", larsHandler)
	http.ListenAndServe(":"+strconv.Itoa(port), mux.Serve())
}