Exemplo n.º 1
0
func setupServerUser(db *sqlx.DB, tokenSecret []byte) (*httptest.Server, error) {
	r := router.New()

	r.Default(
		handlers.SetConfig(db, []byte(tokenSecret)),
	)

	r.POST("/signup", handlers.Signup)
	r.POST("/oauth/token", handlers.UserToken)

	return httptest.NewServer(r), nil
}
Exemplo n.º 2
0
func setupServerHub(db *sqlx.DB, tokenSecret []byte) (*httptest.Server, error) {
	r := router.New()

	r.Default(
		handlers.SetConfig(db, []byte(tokenSecret)),
	)

	r.GET("/api/v0/hub", handlers.Auth, handlers.AddHub)
	r.POST("/api/v0/hub", handlers.Auth, handlers.ShowHub)
	r.DELETE("/api/v0/hub", handlers.Auth, handlers.DeleteHub)

	return httptest.NewServer(r), nil
}
Exemplo n.º 3
0
func setupServer(db *sqlx.DB, tokenSecret []byte) (*httptest.Server, error) {
	r := router.New()

	r.Default(
		handlers.SetConfig(db, []byte(tokenSecret)),
		handlers.Auth,
	)

	r.GET("/api/v0/*endpoint", func(w http.ResponseWriter, r *http.Request, c router.Context) error {
		w.Header().Set("Content-Type", "application/json;charset=UTF-8")
		w.Write([]byte(`{"status":"ok"}`))
		return nil
	})

	return httptest.NewServer(r), nil
}
Exemplo n.º 4
0
Arquivo: cloud.go Projeto: jhbsz/cloud
func main() {
	db, err := sqlx.Open("postgres", dbURL)
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	r := router.New()

	// default handlers are applied to all routes
	r.Default(handlers.SetConfig(db, []byte(tokenSecret)))

	// unauthenticated routes
	r.POST("/signup", handlers.Signup)
	r.POST("/oauth/token", handlers.UserToken)

	// authenticated routes
	r.POST("/api/v0/hub", handlers.Auth, handlers.AddHub)
	r.GET("/api/v0/hub", handlers.Auth, handlers.ShowHub)
	r.DELETE("/api/v0/hub", handlers.Auth, handlers.DeleteHub)

	log.Print("[info] Starting server on ", addr)
	log.Fatal(http.ListenAndServe(addr, r))
}
Exemplo n.º 5
0
func setupServer(tmpDir string) (*httptest.Server, error) {
	r := router.New()

	// directly access an endpoint
	r.GET("/endpoint", func(w http.ResponseWriter, r *http.Request, c router.Context) error {
		_, err := w.Write([]byte("OK"))
		return err
	})

	// using handlers to modify the context
	r.GET("/with_modifiers",
		func(w http.ResponseWriter, r *http.Request, c router.Context) error {
			c.Meta["test"] = "OK"
			// call next handler
			return c.Next(w, r, c)
		},
		func(w http.ResponseWriter, r *http.Request, c router.Context) error {
			o := c.Meta["test"].(string)
			_, err := w.Write([]byte(o))
			return err
		})

	// check named params and request form params
	r.GET("/with_params/:param", func(w http.ResponseWriter, r *http.Request, c router.Context) error {
		_, err := fmt.Fprintf(w, "%s %s", c.Params.ByName("param"), r.FormValue("param"))
		return err
	})

	// check for idempotency
	r.GET("/idempotent",
		func(w http.ResponseWriter, r *http.Request, c router.Context) error {
			_, err := w.Write([]byte("foo"))
			return err
		},
		func(w http.ResponseWriter, r *http.Request, c router.Context) error {
			_, err := w.Write([]byte("bar"))
			return err
		})

	// default handlers
	r.Default(func(w http.ResponseWriter, r *http.Request, c router.Context) error {
		c.Meta["default"] = "hello world"
		// call next handler
		return c.Next(w, r, c)
	})

	r.GET("/use_default_handler",
		func(w http.ResponseWriter, r *http.Request, c router.Context) error {
			o := c.Meta["default"].(string)
			_, err := w.Write([]byte(o))
			return err
		})

	// handler returning an error
	r.GET("/error",
		func(w http.ResponseWriter, r *http.Request, c router.Context) error {
			return errors.New("backend failed")
		})

	// serving static files
	err := ioutil.WriteFile(tmpDir+"/test.txt", []byte("foo bar"), 0644)
	if err != nil {
		return nil, err
	}
	r.ServeFiles("/public/*filepath", http.Dir(tmpDir))

	return httptest.NewServer(r), nil
}