本文整理汇总了Golang中github.com/pressly/chi.NewRouter函数的典型用法代码示例。如果您正苦于以下问题:Golang NewRouter函数的具体用法?Golang NewRouter怎么用?Golang NewRouter使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewRouter函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: TestThrottleClientTimeout
func TestThrottleClientTimeout(t *testing.T) {
r := chi.NewRouter()
r.Use(ThrottleBacklog(10, 50, time.Second*10))
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
time.Sleep(time.Second * 5) // Expensive operation.
w.Write(testContent)
})
server := httptest.NewServer(r)
client := http.Client{
Timeout: time.Second * 3, // Maximum waiting time.
}
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
_, err := client.Get(server.URL)
assert.Error(t, err)
}(i)
}
wg.Wait()
server.Close()
}
开发者ID:chubais,项目名称:chi,代码行数:32,代码来源:throttler_test.go
示例2: main
func main() {
// Setup the logger
logger := logrus.New()
logger.Formatter = &logrus.JSONFormatter{} // optional / configurable, see docs
// Routes
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(NewStructuredLogger(logger))
r.Use(middleware.Recoverer)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("welcome"))
})
r.Get("/wait", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(1 * time.Second)
LogEntrySetField(r, "wait", true)
w.Write([]byte("hi"))
})
r.Get("/panic", func(w http.ResponseWriter, r *http.Request) {
panic("oops")
})
http.ListenAndServe(":3333", r)
}
开发者ID:pressly,项目名称:chi,代码行数:25,代码来源:main.go
示例3: TestSimple
func TestSimple(t *testing.T) {
r := chi.NewRouter()
r.Use(TokenAuth.Verifier, jwtauth.Authenticator)
r.Get("/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte("welcome"))
})
ts := httptest.NewServer(r)
defer ts.Close()
// sending unauthorized requests
if status, resp := testRequest(t, ts, "GET", "/", nil, nil); status != 401 && resp != "Unauthorized\n" {
t.Fatalf(resp)
}
h := http.Header{}
h.Set("Authorization", "BEARER "+newJwtToken([]byte("wrong"), map[string]interface{}{}))
if status, resp := testRequest(t, ts, "GET", "/", h, nil); status != 401 && resp != "Unauthorized\n" {
t.Fatalf(resp)
}
h.Set("Authorization", "BEARER asdf")
if status, resp := testRequest(t, ts, "GET", "/", h, nil); status != 401 && resp != "Unauthorized\n" {
t.Fatalf(resp)
}
// sending authorized requests
if status, resp := testRequest(t, ts, "GET", "/", newAuthHeader(), nil); status != 200 && resp != "welcome" {
t.Fatalf(resp)
}
}
开发者ID:lacion,项目名称:jwtauth,代码行数:32,代码来源:jwtauth_test.go
示例4: accountsRouter
func accountsRouter() chi.Router {
r := chi.NewRouter()
r.Use(sup1)
r.Get("/", listAccounts)
r.Post("/", createAccount)
r.Get("/hi", hiAccounts)
r.Group(func(r chi.Router) {
r.Use(sup2)
r.Get("/hi2", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
v := ctx.Value("sup2").(string)
w.Write([]byte(fmt.Sprintf("hi2 - '%s'", v)))
})
})
r.Route("/:accountID", func(r chi.Router) {
r.Use(accountCtx)
r.Get("/", getAccount)
r.Put("/", updateAccount)
r.Get("/*", other)
})
return r
}
开发者ID:vladdy,项目名称:chi,代码行数:27,代码来源:main.go
示例5: router
func router() http.Handler {
r := chi.NewRouter()
// Protected routes
r.Group(func(r chi.Router) {
// Seek, verify and validate JWT tokens
r.Use(TokenAuth.Verifier)
// Handle valid / invalid tokens. In this example, we use
// the provided authenticator middleware, but you can write your
// own very easily, look at the Authenticator method in jwtauth.go
// and tweak it, its not scary.
r.Use(jwtauth.Authenticator)
r.Get("/admin", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
token := ctx.Value("jwt").(*jwt.Token)
claims := token.Claims
w.Write([]byte(fmt.Sprintf("protected area. hi %v", claims["user_id"])))
})
})
// Public routes
r.Group(func(r chi.Router) {
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("welcome anonymous"))
})
})
return r
}
开发者ID:goware,项目名称:jwtauth,代码行数:31,代码来源:main.go
示例6: New
// New returns a new blank Engine instance with no middleware attached
func New(opts Options) *Engine {
namespace := opts.Namespace
if len(namespace) == 0 {
namespace = "/"
}
engine := &Engine{}
engine.Router = &Router{
namespace: namespace,
engine: engine,
mux: chi.NewRouter(),
}
engine.options = opts
engine.pool.New = func() interface{} {
ctx := &Context{
Engine: engine,
render: render.New(render.Options{
Layout: "layout",
}),
}
return ctx
}
return engine
}
开发者ID:ustrajunior,项目名称:minion,代码行数:26,代码来源:minion.go
示例7: TestThrottleMaximum
func TestThrottleMaximum(t *testing.T) {
r := chi.NewRouter()
r.Use(ThrottleBacklog(50, 50, time.Second*5))
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
time.Sleep(time.Second * 2) // Expensive operation.
w.Write(testContent)
})
server := httptest.NewServer(r)
client := http.Client{
Timeout: time.Second * 60, // Maximum waiting time.
}
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
res, err := client.Get(server.URL)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
buf, err := ioutil.ReadAll(res.Body)
assert.NoError(t, err)
assert.Equal(t, testContent, buf)
}(i)
}
// Wait less time than what the server takes to reply.
time.Sleep(time.Second * 1)
// At this point the server is still processing, all the following request
// will be beyond the server capacity.
for i := 0; i < 100; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
res, err := client.Get(server.URL)
assert.NoError(t, err)
buf, err := ioutil.ReadAll(res.Body)
assert.Equal(t, http.StatusServiceUnavailable, res.StatusCode)
assert.Equal(t, errCapacityExceeded, strings.TrimSpace(string(buf)))
}(i)
}
wg.Wait()
server.Close()
}
开发者ID:chubais,项目名称:chi,代码行数:59,代码来源:throttler_test.go
示例8: New
//New create all routers under one Handler
func New() http.Handler {
r := chi.NewRouter()
r.Mount("/auth", auth.Routes())
r.Mount("/apps", apps.Routes())
return r
}
开发者ID:releasifier,项目名称:releasifier,代码行数:9,代码来源:web.go
示例9: Routes
//Routes returns chi's Router for Auth APIs
func Routes() chi.Router {
r := chi.NewRouter()
r.Post("/register", m.BodyParser(registerRequestBuilder, 512), register)
r.Post("/login", m.BodyParser(loginRequestBuilder, 512), login)
r.Head("/logout", logout)
return r
}
开发者ID:releasifier,项目名称:releasifier,代码行数:10,代码来源:routes.go
示例10: articleRouter
func articleRouter() http.Handler {
r := chi.NewRouter()
r.Get("/", listArticles)
r.Route("/:articleID", func(r chi.Router) {
r.Get("/", getArticle)
// r.Put("/", updateArticle)
// r.Delete("/", deleteArticle)
})
return r
}
开发者ID:pressly,项目名称:chi,代码行数:10,代码来源:main.go
示例11: TestThrottleTriggerGatewayTimeout
func TestThrottleTriggerGatewayTimeout(t *testing.T) {
r := chi.NewRouter()
r.Use(ThrottleBacklog(50, 100, time.Second*5))
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
time.Sleep(time.Second * 10) // Expensive operation.
w.Write(testContent)
})
server := httptest.NewServer(r)
client := http.Client{
Timeout: time.Second * 60, // Maximum waiting time.
}
var wg sync.WaitGroup
// These requests will be processed normally until they finish.
for i := 0; i < 50; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
res, err := client.Get(server.URL)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
}(i)
}
time.Sleep(time.Second * 1)
// These requests will wait for the first batch to complete but it will take
// too much time, so they will eventually receive a timeout error.
for i := 0; i < 50; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
res, err := client.Get(server.URL)
assert.NoError(t, err)
buf, err := ioutil.ReadAll(res.Body)
assert.Equal(t, http.StatusServiceUnavailable, res.StatusCode)
assert.Equal(t, errTimedOut, strings.TrimSpace(string(buf)))
}(i)
}
wg.Wait()
server.Close()
}
开发者ID:chubais,项目名称:chi,代码行数:55,代码来源:throttler_test.go
示例12: main
func main() {
r := chi.NewRouter()
// Limit to 5 requests globally.
r.Use(throttler.Limit(5))
r.Get("/*", handler)
admin := chi.NewRouter()
// Limit to 2 requests for admin route
admin.Use(throttler.Limit(2))
admin.Get("/", handler)
r.Mount("/admin/", admin)
fmt.Printf("Try running the following commands (in different terminal):\n\n")
fmt.Printf("for i in `seq 1 10`; do (curl 127.0.0.1:8000/ &); done\n\n")
fmt.Printf("for i in `seq 1 10`; do (curl 127.0.0.1:8000/admin/ &); done\n\n")
http.ListenAndServe(":8000", r)
}
开发者ID:DisruptiveMind,项目名称:throttler,代码行数:20,代码来源:main.go
示例13: initChi
func initChi() {
h := chi.NewRouter()
h.Get("/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
fmt.Fprintf(w, "Hello, World")
})
h.Get("/:name", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
fmt.Fprintf(w, "Hello, %s", chi.URLParams(ctx)["name"])
})
registerHandler("chi", h)
}
开发者ID:najeira,项目名称:go-frameworks-benchmark,代码行数:12,代码来源:chi.go
示例14: Profiler
func Profiler() http.Handler {
r := chi.NewRouter()
r.Handle("/vars", expVars)
r.Handle("/pprof/", pprof.Index)
r.Handle("/pprof/cmdline", pprof.Cmdline)
r.Handle("/pprof/profile", pprof.Profile)
r.Handle("/pprof/symbol", pprof.Symbol)
r.Handle("/pprof/block", pprof.Handler("block").ServeHTTP)
r.Handle("/pprof/heap", pprof.Handler("heap").ServeHTTP)
r.Handle("/pprof/goroutine", pprof.Handler("goroutine").ServeHTTP)
r.Handle("/pprof/threadcreate", pprof.Handler("threadcreate").ServeHTTP)
return r
}
开发者ID:palaiyacw,项目名称:imgry,代码行数:13,代码来源:middleware.go
示例15: main
func main() {
r := chi.NewRouter()
// use middleware
r.Use(middleware.Logger)
// add routing
r.Get("/", apiIndex)
r.Route("/todo", func(r chi.Router) {
r.Get("/", getTodoList)
})
http.ListenAndServe(":3333", r)
}
开发者ID:yudppp,项目名称:chi-sample,代码行数:14,代码来源:main.go
示例16: adminRouter
// A completely separate router for administrator routes
func adminRouter() http.Handler { // or chi.Router {
r := chi.NewRouter()
r.Use(AdminOnly)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("admin: index"))
})
r.Get("/accounts", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("admin: list accounts.."))
})
r.Get("/users/:userId", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte(fmt.Sprintf("admin: view user id %v", chi.URLParams(ctx)["userId"])))
})
return r
}
开发者ID:raj347,项目名称:chi,代码行数:15,代码来源:main.go
示例17: main
func main() {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(httpcoala.Route("HEAD", "GET")) // or, Route("*")
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(100 * time.Millisecond) // expensive op
w.WriteHeader(200)
w.Write([]byte("hi"))
})
http.ListenAndServe(":1111", r)
}
开发者ID:goware,项目名称:httpcoala,代码行数:14,代码来源:main.go
示例18: NewRouter
// NewRouter creates new chakra Mux and appends its route to acl.route, unless
// the passed route is chakra.UseParentRoute.
func NewRouter(route string) *Mux {
if route == "" {
panic("new router created without acl route")
}
if accessControl == nil {
panic("you have not defined access control function with SetAC")
}
r := &Mux{chi.NewRouter()}
if route != UseParentRoute {
r.Mux.Use(Route(route))
}
return r
}
开发者ID:c2h5oh,项目名称:chakra,代码行数:17,代码来源:mux.go
示例19: TestRedirectSlashes
func TestRedirectSlashes(t *testing.T) {
r := chi.NewRouter()
// This middleware must be mounted at the top level of the router, not at the end-handler
// because then it'll be too late and will end up in a 404
r.Use(RedirectSlashes)
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
w.Write([]byte("nothing here"))
})
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("root"))
})
r.Route("/accounts/:accountID", func(r chi.Router) {
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
accountID := chi.URLParam(r, "accountID")
w.Write([]byte(accountID))
})
})
ts := httptest.NewServer(r)
defer ts.Close()
if status, resp := testRequest(t, ts, "GET", "/", nil); resp != "root" && status != 200 {
t.Fatalf(resp)
}
// NOTE: the testRequest client will follow the redirection..
if status, resp := testRequest(t, ts, "GET", "//", nil); resp != "root" && status != 200 {
t.Fatalf(resp)
}
if status, resp := testRequest(t, ts, "GET", "/accounts/admin", nil); resp != "admin" && status != 200 {
t.Fatalf(resp)
}
// NOTE: the testRequest client will follow the redirection..
if status, resp := testRequest(t, ts, "GET", "/accounts/admin/", nil); resp != "admin" && status != 200 {
t.Fatalf(resp)
}
if status, resp := testRequest(t, ts, "GET", "/nothing-here", nil); resp != "nothing here" && status != 200 {
t.Fatalf(resp)
}
}
开发者ID:pressly,项目名称:chi,代码行数:48,代码来源:strip_test.go
示例20: StartAPI
// StartAPI Starts the API listener on port 8081
func StartAPI() {
r := chi.NewRouter()
cors := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"},
AllowCredentials: true,
})
r.Use(cors.Handler)
r.Use(middleware.Recoverer)
//r.Use(middleware.Logger)
r.Route("/api", func(r chi.Router) {
r.Route("/dashboard", func(r chi.Router) {
r.Route("/widgets", func(r chi.Router) {
r.Get("/", getWidgets)
r.Get("/events", getEvents)
r.Get("/feed/:feedName", getFeed)
r.Get("/options", getWidgetOptions)
r.Get("/names", getWidgetNames)
r.Get("/name/:widgetName", getWidgetByName)
r.Put("/name/:widgetName", putWidget)
r.Delete("/name/:widgetName", deleteWidget)
})
})
r.Route("/assets", func(r chi.Router) {
r.Route("/:assetType", func(r chi.Router) {
r.Get("/", getAssets)
})
})
r.Route("/classes", func(r chi.Router) {
r.Get("/export", exportClasses)
//r.Get("/import", importClasses) // TODO
r.Route("/:classType", func(r chi.Router) {
r.Get("/", getClasses)
r.Get("/options", getClassOptions)
r.Get("/names", getClassNames)
r.Get("/name/:className", getClassByName)
r.Put("/name/:className", putClass)
r.Delete("/name/:className", deleteClass)
})
})
})
http.ListenAndServe(":8081", r)
}
开发者ID:murdinc,项目名称:awsm,代码行数:48,代码来源:api.go
注:本文中的github.com/pressly/chi.NewRouter函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论