func (handler *atomicHandler) construct() (_ http.Handler, err error) { h, err := constructHandler(handler.Routes, handler.Options) if err != nil { return } return xhandler.New(handler.Context, h), nil }
func router(a *server) http.Handler { mux := xmux.New() c := xhandler.Chain{} c.Use(mwLogger) c.Use(mwAuthenticationCheck(a.key)) mux.GET("/sites", c.HandlerCF(xhandler.HandlerFuncC(a.handleAllSites))) mux.GET("/sites/:id", c.HandlerCF(xhandler.HandlerFuncC(a.handleSingleSite))) mux.GET("/torrents", c.HandlerCF(xhandler.HandlerFuncC(a.handleTorrents))) mux.POST("/download/:hash", c.HandlerCF(xhandler.HandlerFuncC(a.handleDownload))) return xhandler.New(context.Background(), mux) }
func ExampleMux_NewGroup() { mux := xmux.New() api := mux.NewGroup("/api") api.GET("/users/:name", xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "GET /api/users/%s", xmux.Param(ctx, "name")) })) api.POST("/users/:name", xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "POST /api/users/%s", xmux.Param(ctx, "name")) })) if err := http.ListenAndServe(":8080", xhandler.New(context.Background(), mux)); err != nil { log.Fatal(err) } }
func ExampleHandleTimeout() { var xh xhandler.HandlerC xh = xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello World")) if _, ok := ctx.Deadline(); ok { w.Write([]byte(" with deadline")) } }) // This handler adds a timeout to the handler xh = xhandler.TimeoutHandler(5 * time.Second)(xh) ctx := context.Background() // Bridge context aware handlers with http.Handler using xhandler.Handle() http.Handle("/", xhandler.New(ctx, xh)) if err := http.ListenAndServe(":8080", nil); err != nil { log.Fatal(err) } }
func ExampleHandle() { var xh xhandler.HandlerC xh = xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { value, _ := fromContext(ctx) w.Write([]byte("Hello " + value)) }) xh = (func(next xhandler.HandlerC) xhandler.HandlerC { return xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { ctx = newContext(ctx, "World") next.ServeHTTPC(ctx, w, r) }) })(xh) ctx := context.Background() // Bridge context aware handlers with http.Handler using xhandler.Handle() http.Handle("/", xhandler.New(ctx, xh)) if err := http.ListenAndServe(":8080", nil); err != nil { log.Fatal(err) } }
ctx := compiler.NewContext(context.Background(), comp) if c.Bool("tracing") { DefaultWebOptions = append(DefaultWebOptions, renderer.WithTracing()) } // Turn routes into HTTP handler api, err := constructHandler(c.StringSlice("routes"), DefaultWebOptions) if err != nil { return fmt.Errorf("[routes] %v", err) } // Construct API handler handler := &atomicHandler{ Context: ctx, Current: xhandler.New(ctx, api), Options: DefaultWebOptions, Watching: c.Bool("watch"), Routes: c.StringSlice("routes"), Mutex: new(sync.RWMutex), } if c.Bool("watch") { // Start watching for changes in components directory var w *watcher.Watcher w, err = watcher.Start(c.String("components"), storage) if err != nil { return } defer w.Stop()