• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang xhandler.Chain类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/rs/xhandler.Chain的典型用法代码示例。如果您正苦于以下问题:Golang Chain类的具体用法?Golang Chain怎么用?Golang Chain使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Chain类的18个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: Example_handler

func Example_handler() {
	c := xhandler.Chain{}

	host, _ := os.Hostname()
	conf := xlog.Config{
		// Set some global env fields
		Fields: xlog.F{
			"role": "my-service",
			"host": host,
		},
	}

	// Install the logger handler with default output on the console
	c.UseC(xlog.NewHandler(conf))

	// Plug the xlog handler's input to Go's default logger
	log.SetFlags(0)
	log.SetOutput(xlog.New(conf))

	// Install some provided extra handler to set some request's context fields.
	// Thanks to those handler, all our logs will come with some pre-populated fields.
	c.UseC(xlog.RemoteAddrHandler("ip"))
	c.UseC(xlog.UserAgentHandler("user_agent"))
	c.UseC(xlog.RefererHandler("referer"))
	c.UseC(xlog.RequestIDHandler("req_id", "Request-Id"))

	// Here is your final handler
	h := c.Handler(xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
		// Get the logger from the context. You can safely assume it will be always there,
		// if the handler is removed, xlog.FromContext will return a NopLogger
		l := xlog.FromContext(ctx)

		// Then log some errors
		if err := errors.New("some error from elsewhere"); err != nil {
			l.Errorf("Here is an error: %v", err)
		}

		// Or some info with fields
		l.Info("Something happend", xlog.F{
			"user":   "current user id",
			"status": "ok",
		})
	}))
	http.Handle("/", h)

	if err := http.ListenAndServe(":8080", nil); err != nil {
		log.SetOutput(os.Stderr) // make sure we print to console
		log.Fatal(err)
	}
}
开发者ID:patrickToca,项目名称:xlog,代码行数:50,代码来源:handler_examples_test.go


示例2: ExampleNewHandler

func ExampleNewHandler() {
	c := xhandler.Chain{}

	// Install the metric handler with dogstatsd backend client and some env tags
	flushInterval := 5 * time.Second
	tags := []string{"role:my-service"}
	statsdWriter, err := net.Dial("udp", "127.0.0.1:8126")
	if err != nil {
		log.Fatal(err)
	}
	c.Use(xstats.NewHandler(dogstatsd.New(statsdWriter, flushInterval), tags))

	// Here is your handler
	h := c.HandlerH(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// Get the xstats request's instance from the context. You can safely assume it will
		// be always there, if the handler is removed, xstats.FromContext will return a nop
		// instance.
		m := xstats.FromRequest(r)

		// Count something
		m.Count("requests", 1, "route:index")
	}))

	http.Handle("/", h)

	if err := http.ListenAndServe(":8080", nil); err != nil {
		log.Fatal(err)
	}
}
开发者ID:rs,项目名称:xstats,代码行数:29,代码来源:handler_example_test.go


示例3: main

func main() {
	c := xhandler.Chain{}

	setupHandlerChain(&c)

	mux := xmux.New()

	mux.GET("/gamble", xhandler.HandlerFuncC(shittyHandler))

	log.Printf("Listening on http://localhost%v/gamble", port)
	http.ListenAndServe(port, c.Handler(mux))

}
开发者ID:esiqveland,项目名称:shittyservice,代码行数:13,代码来源:main.go


示例4: main

func main() {
	c := xhandler.Chain{}

	// Use default options
	c.UseC(cors.Default().HandlerC)

	mux := http.NewServeMux()
	mux.Handle("/", c.Handler(xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		w.Write([]byte("{\"hello\": \"world\"}"))
	})))

	http.ListenAndServe(":8080", mux)
}
开发者ID:ch3lo,项目名称:inspector,代码行数:14,代码来源:server.go


示例5: ExampleIf

func ExampleIf() {
	c := xhandler.Chain{}

	// Add a timeout handler only if the URL path matches a prefix
	c.UseC(xhandler.If(
		func(ctx context.Context, w http.ResponseWriter, r *http.Request) bool {
			return strings.HasPrefix(r.URL.Path, "/with-timeout/")
		},
		xhandler.TimeoutHandler(2*time.Second),
	))

	http.Handle("/", c.Handler(xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(w, "Welcome to the home page!")
	})))
}
开发者ID:stellar,项目名称:bridge-server,代码行数:15,代码来源:chain_example_test.go


示例6: main

func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	env, err := getEnviron()
	if err != nil {
		log.Error(err)
		os.Exit(1)
	}

	l, err := log.ParseLevel(env.logLevel)
	if err != nil {
		l = log.ErrorLevel
	}
	log.SetLevel(l)

	log.Infof("Service %s started", serviceID)

	log.Infof("%s=%s", tmpDirEnvar, env.tmpDir)
	log.Infof("%s=%s", authServerEnvar, env.authServer)
	log.Infof("%s=%s", dataServerEnvar, env.dataServer)
	log.Infof("%s=%s", metaServerEnvar, env.metaServer)
	log.Infof("%s=%d\n", portEnvar, env.port)
	log.Infof("%s=%s\n", sharedSecretEnvar, "******")

	p := &newServerParams{}
	p.authServer = env.authServer
	p.dataServer = env.dataServer
	p.metaServer = env.metaServer
	p.sharedSecret = env.sharedSecret
	p.tmpDir = env.tmpDir

	// Create chunk tmp dir
	if err := os.MkdirAll(p.tmpDir, 0644); err != nil {
		log.Error(err)
		os.Exit(1)
	}

	srv, err := newServer(p)
	if err != nil {
		log.Error(err)
		os.Exit(1)
	}

	c := xhandler.Chain{}
	c.UseC(xhandler.CloseHandler)

	http.Handle(endPoint, c.Handler(srv))
	log.Error(http.ListenAndServe(fmt.Sprintf(":%d", env.port), nil))
}
开发者ID:clawio,项目名称:service-ocwebdav,代码行数:48,代码来源:main.go


示例7: main

func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	c := xhandler.Chain{}
	c.UseC(xhandler.CloseHandler)

	env, err := getEnviron()
	if err != nil {
		log.Error(err)
		os.Exit(1)
	}

	l, err := log.ParseLevel(env.logLevel)
	if err != nil {
		l = log.ErrorLevel
	}
	log.SetLevel(l)

	log.Infof("Service %s started", serviceID)

	printEnviron(env)

	p := &newServerParams{}
	p.dataDir = env.dataDir
	p.tmpDir = env.tmpDir
	p.checksum = env.checksum
	p.prop = env.prop
	p.sharedSecret = env.sharedSecret

	// Create data and tmp dirs
	if err := os.MkdirAll(p.dataDir, 0644); err != nil {
		log.Error(err)
		os.Exit(1)
	}
	if err := os.MkdirAll(p.tmpDir, 0644); err != nil {
		log.Error(err)
		os.Exit(1)
	}

	srv, err := newServer(p)
	if err != nil {
		log.Error(err)
		os.Exit(1)
	}

	http.Handle(endPoint, c.Handler(srv))
	log.Error(http.ListenAndServe(fmt.Sprintf(":%d", env.port), nil))
}
开发者ID:clawio,项目名称:service-localfs-data,代码行数:47,代码来源:main.go


示例8: ExampleAddChain

func ExampleAddChain() {
	c := xhandler.Chain{}

	close := xhandler.CloseHandler
	cors := cors.Default().Handler
	timeout := xhandler.TimeoutHandler(2 * time.Second)
	auth := func(next xhandler.HandlerC) xhandler.HandlerC {
		return xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
			if v := ctx.Value("Authorization"); v == nil {
				http.Error(w, "Not authorized", http.StatusUnauthorized)
				return
			}
			next.ServeHTTPC(ctx, w, r)
		})
	}

	c.Add(close, cors, timeout)

	mux := http.NewServeMux()

	// Use c.Handler to terminate the chain with your final handler
	mux.Handle("/", c.Handler(xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(w, "Welcome to the home page!")
	})))

	// Create a new chain from an existing one, and add route-specific middleware to it
	protected := c.With(auth)

	mux.Handle("/admin", protected.Handler(xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(w, "protected endpoint!")
	})))
}
开发者ID:stellar,项目名称:bridge-server,代码行数:32,代码来源:chain_example_test.go


示例9: ExampleMux

func ExampleMux() {
	c := xhandler.Chain{}

	// Append a context-aware middleware handler
	c.UseC(xhandler.CloseHandler)

	// Another context-aware middleware handler
	c.UseC(xhandler.TimeoutHandler(2 * time.Second))

	mux := xmux.New()

	// Use c.Handler to terminate the chain with your final handler
	mux.GET("/welcome/:name", xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(w, "Welcome %s!", xmux.Param(ctx, "name"))
	}))

	if err := http.ListenAndServe(":8080", c.Handler(mux)); err != nil {
		log.Fatal(err)
	}
}
开发者ID:patrickToca,项目名称:xmux,代码行数:20,代码来源:mux_example_test.go


示例10: main

func main() {
	c := xhandler.Chain{}
	c.Use(recoverMiddleware)
	c.Use(normalLoggingMiddleware)
	c.Use(log15LoggingMiddleware)
	c.Use(logrusLoggingMiddleware)

	simpleHandler := xhandler.HandlerFuncC(simple)
	accountHandler := xhandler.HandlerFuncC(account)
	noteHandler := xhandler.HandlerFuncC(note)

	mux := bone.New()
	mux.Get("/account/:id", c.Handler(accountHandler))
	mux.Get("/note/:id", c.Handler(noteHandler))
	mux.Get("/simple", c.Handler(simpleHandler))
	http.ListenAndServe(":8080", mux)
}
开发者ID:achiku,项目名称:sample-golang-logging,代码行数:17,代码来源:main.go


示例11: ExampleChain

func ExampleChain() {
	c := xhandler.Chain{}
	// Append a context-aware middleware handler
	c.UseC(xhandler.CloseHandler)

	// Mix it with a non-context-aware middleware handler
	c.Use(cors.Default().Handler)

	// Another context-aware middleware handler
	c.UseC(xhandler.TimeoutHandler(2 * time.Second))

	mux := http.NewServeMux()

	// Use c.Handler to terminate the chain with your final handler
	mux.Handle("/", c.Handler(xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(w, "Welcome to the home page!")
	})))

	// You can reuse the same chain for other handlers
	mux.Handle("/api", c.Handler(xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(w, "Welcome to the API!")
	})))
}
开发者ID:se77en,项目名称:xhandler,代码行数:23,代码来源:chain_example_test.go


示例12: main

func main() {
	if !checkFlags() {
		os.Exit(1)
	}

	sessionStore := sessions.NewCookieStore([]byte(*sessionHashKey), []byte(*sessionBlockKey))

	// OpenID Connect Providers

	// Google
	oidcGoogleLoginRoute := "/logingoogle"
	oidcGoogleCBRoute := "/gcallback"
	oidcGoogle := &oidc.Google{
		ClientID:     *oidcGoogleClientID,
		ClientSecret: *oidcGoogleClientSecret,
		RedirectURI:  *publicURL + oidcGoogleCBRoute,
		SessionStore: sessionStore,
	}

	// PayPal
	oidcPaypalLoginRoute := "/loginpaypal"
	oidcPaypalCBRoute := "/pcallback"
	oidcPaypal := &oidc.Paypal{
		ClientID:     *oidcPaypalClientID,
		ClientSecret: *oidcPaypalClientSecret,
		RedirectURI:  *publicURL + oidcPaypalCBRoute,
		SessionStore: sessionStore,
	}

	// Dynamodb
	cfg := &aws.Config{}
	if *dynamodbEndpoint != "" {
		cfg.Endpoint = aws.String(*dynamodbEndpoint)
	}
	sess := session.New(cfg)
	if *debug {
		sess.Config.LogLevel = aws.LogLevel(aws.LogDebug)
	}

	// Model
	var m model.Model
	m = awsdynamo.NewModelFromSession(sess)

	// Controller
	// OAuth / OpenID Connect
	authCGoogle := controller.NewAuthController(m.UserPeer(), oidcGoogle, "google")
	authCPaypal := controller.NewAuthController(m.UserPeer(), oidcPaypal, "paypal")

	// Post Controller
	postContrData := &postDataProvider{
		PostPeer: m.PostPeer(),
		UserPeer: m.UserPeer(),
	}
	postController := &controller.PostController{
		Model: postContrData,
	}

	// Middleware
	baseChain := xhandler.Chain{}
	baseChain.UseC(xhandler.TimeoutHandler(2 * time.Second))

	// Session management
	sessionMiddleware := middleware.Session{}
	sessionMiddleware.Init([]byte(*sessionHashKey), []byte(*sessionBlockKey))
	baseChain.UseC(sessionMiddleware.Enable("posty-session"))

	// Chain for authenticated routes
	authedChain := xhandler.Chain{}
	authedChain = append(authedChain, baseChain...)
	authedChain.UseC(middleware.AuthenticatedFilter("/login"))
	authedChain.UseC(middleware.UserContext())

	// Chain for authenticated routes with json response
	jsonChain := xhandler.Chain{}
	jsonChain = append(jsonChain, authedChain...)
	jsonChain.UseC(middleware.JSONWrapper())

	// Chain for unauthenticated routes
	unauthedChain := xhandler.Chain{}
	unauthedChain = append(unauthedChain, baseChain...)
	unauthedChain.UseC(middleware.UnauthenticatedFilter("/"))

	// Main Context
	ctx := context.Background()
	route := func(chain xhandler.Chain, handler xhandler.HandlerC) web.Handler {
		return handle(ctx, chain.HandlerC(handler))
	}

	// Routes
	mux := web.New()
	mux.Get("/api/posts", route(jsonChain, xhandler.HandlerFuncC(postController.Posts)))
	mux.Post("/api/posts", route(jsonChain, xhandler.HandlerFuncC(postController.Create)))
	mux.Delete("/api/posts/:id", route(jsonChain, xhandler.HandlerFuncC(postController.Remove)))
	// OIDC Routes
	mux.Get(oidcGoogleLoginRoute, route(unauthedChain, authCGoogle.Login()))
	mux.Get(oidcGoogleCBRoute, route(unauthedChain, authCGoogle.Callback("/")))
	mux.Get(oidcPaypalLoginRoute, route(unauthedChain, authCPaypal.Login()))
	mux.Get(oidcPaypalCBRoute, route(unauthedChain, authCPaypal.Callback("/")))
	mux.Get("/logout", route(authedChain, authCGoogle.Logout("/login")))

//.........这里部分代码省略.........
开发者ID:blang,项目名称:posty,代码行数:101,代码来源:main.go


示例13: construct

func construct(o *webOptions) xhandler.HandlerC {
	var chain xhandler.Chain
	chain.UseC(xhandler.CloseHandler)
	chain.UseC(xhandler.TimeoutHandler(o.reqTimeout))
	chain.UseC(o.componentSetter)
	chain.UseC(o.templateCtxSetter)
	for _, m := range o.middlewares {
		chain.UseC(m)
	}
	chain.UseC(CompileInContext)
	chain.UseC(RenderInContext)
	if o.alwaysHTML {
		return chain.HandlerCF(WriteRenderedHTML)
	}
	return chain.HandlerCF(WriteRendered)
}
开发者ID:crackcomm,项目名称:renderer,代码行数:16,代码来源:server.go


示例14: router

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)
}
开发者ID:intfrr,项目名称:magopie,代码行数:15,代码来源:main.go


示例15: Example


//.........这里部分代码省略.........
					MaxLen: 150,
				},
			},
		}

		// Define a post resource schema
		post = schema.Schema{
			// schema.*Field are shortcuts for common fields (identical to users' same fields)
			"id":      schema.IDField,
			"created": schema.CreatedField,
			"updated": schema.UpdatedField,
			// Define a user field which references the user owning the post.
			// See bellow, the content of this field is enforced by the fact
			// that posts is a sub-resource of users.
			"user": schema.Field{
				Required:   true,
				Filterable: true,
				Validator: &schema.Reference{
					Path: "users",
				},
			},
			"public": schema.Field{
				Filterable: true,
				Validator:  &schema.Bool{},
			},
			// Sub-documents are handled via a sub-schema
			"meta": schema.Field{
				Schema: &schema.Schema{
					"title": schema.Field{
						Required: true,
						Validator: &schema.String{
							MaxLen: 150,
						},
					},
					"body": schema.Field{
						Validator: &schema.String{
							MaxLen: 100000,
						},
					},
				},
			},
		}
	)

	// Create a REST API root resource
	index := resource.NewIndex()

	// Add a resource on /users[/:user_id]
	users := index.Bind("users", resource.New(user, mem.NewHandler(), resource.Conf{
		// We allow all REST methods
		// (rest.ReadWrite is a shortcut for []rest.Mode{Create, Read, Update, Delete, List})
		AllowedModes: resource.ReadWrite,
	}))

	// Bind a sub resource on /users/:user_id/posts[/:post_id]
	// and reference the user on each post using the "user" field of the posts resource.
	posts := users.Bind("posts", "user", resource.New(post, mem.NewHandler(), resource.Conf{
		// Posts can only be read, created and deleted, not updated
		AllowedModes: []resource.Mode{resource.Read, resource.List, resource.Create, resource.Delete},
	}))

	// Add a friendly alias to public posts
	// (equivalent to /users/:user_id/posts?filter={"public":true})
	posts.Alias("public", url.Values{"filter": []string{"{\"public\"=true}"}})

	// Create API HTTP handler for the resource graph
	api, err := rest.NewHandler(index)
	if err != nil {
		log.Fatalf("Invalid API configuration: %s", err)
	}

	// Init a xhandler chain (see https://github.com/rs/xhandler)
	c := xhandler.Chain{}

	// Add close notifier handler so context is cancelled when the client closes
	// the connection
	c.UseC(xhandler.CloseHandler)

	// Add timeout handler
	c.UseC(xhandler.TimeoutHandler(2 * time.Second))

	// Install a logger (see https://github.com/rs/xlog)
	c.UseC(xlog.NewHandler(xlog.Config{}))

	// Log API access
	c.UseC(xaccess.NewHandler())

	// Add CORS support with passthrough option on so rest-layer can still
	// handle OPTIONS method
	c.UseC(cors.New(cors.Options{OptionsPassthrough: true}).HandlerC)

	// Bind the API under /api/ path
	http.Handle("/api/", http.StripPrefix("/api/", c.Handler(api)))

	// Serve it
	log.Print("Serving API on http://localhost:8080")
	if err := http.ListenAndServe(":8080", nil); err != nil {
		log.Fatal(err)
	}
}
开发者ID:apuigsech,项目名称:rest-layer,代码行数:101,代码来源:example_test.go


示例16: setupHandlerChain

// setupHandlerChain does plumbing for logging and such.
func setupHandlerChain(c *xhandler.Chain) {
	host, _ := os.Hostname()
	conf := xlog.Config{
		// Log info level and higher
		Level: xlog.LevelInfo,
		// Set some global env fields
		Fields: xlog.F{
			"role": "my-shitty-service",
			"host": host,
		},
		// Output everything on console
		Output: xlog.NewOutputChannel(xlog.NewConsoleOutput()),
	}

	// Add close notifier handler so context is cancelled when the client closes
	// the connection
	c.UseC(xhandler.CloseHandler)

	// Install the logger handler
	c.UseC(xlog.NewHandler(conf))

	// Add timeout handler (HAHA)
	//c.UseC(xhandler.TimeoutHandler(2 * time.Second))

	// Install some provided extra handler to set some request's context fields.
	// Thanks to those handler, all our logs will come with some pre-populated fields.
	c.UseC(xlog.MethodHandler("method"))
	c.UseC(xlog.URLHandler("url"))
	c.UseC(xlog.RemoteAddrHandler("ip"))
	c.UseC(xlog.UserAgentHandler("user_agent"))
	c.UseC(xlog.RefererHandler("referer"))
	c.UseC(xlog.RequestIDHandler("req_id", "Request-Id"))
}
开发者ID:esiqveland,项目名称:shittyservice,代码行数:34,代码来源:main.go


示例17: main

func main() {
	// Create a REST API resource index
	index := resource.NewIndex()

	// Add a resource on /users[/:user_id]
	users := index.Bind("users", resource.New(user, mem.NewHandler(), resource.Conf{
		// We allow all REST methods
		// (rest.ReadWrite is a shortcut for []resource.Mode{resource.Create, resource.Read, resource.Update, resource.Delete, resource,List})
		AllowedModes: resource.ReadWrite,
	}))

	// Bind a sub resource on /users/:user_id/posts[/:post_id]
	// and reference the user on each post using the "user" field of the posts resource.
	posts := users.Bind("posts", "user", resource.New(post, mem.NewHandler(), resource.Conf{
		// Posts can only be read, created and deleted, not updated
		AllowedModes: []resource.Mode{resource.Read, resource.List, resource.Create, resource.Delete},
	}))

	// Add a friendly alias to public posts
	// (equivalent to /users/:user_id/posts?filter={"published":true})
	posts.Alias("public", url.Values{"filter": []string{"{\"published\":true}"}})

	// Create API HTTP handler for the resource graph
	api, err := rest.NewHandler(index)
	if err != nil {
		log.Fatalf("Invalid API configuration: %s", err)
	}

	// Init a xhandler chain (see https://github.com/rs/xhandler)
	c := xhandler.Chain{}

	// Add close notifier handler so context is cancelled when the client closes
	// the connection
	c.UseC(xhandler.CloseHandler)

	// Add timeout handler
	c.UseC(xhandler.TimeoutHandler(2 * time.Second))

	// Install a logger (see https://github.com/rs/xlog)
	c.UseC(xlog.NewHandler(xlog.Config{}))

	// Log API access
	c.UseC(xaccess.NewHandler())

	// Add CORS support with passthrough option on so rest-layer can still
	// handle OPTIONS method
	c.UseC(cors.New(cors.Options{OptionsPassthrough: true}).HandlerC)

	// Bind the API under /api/ path
	http.Handle("/api/", http.StripPrefix("/api/", c.Handler(api)))

	// Serve it
	log.Print("Serving API on http://localhost:8080")
	if err := http.ListenAndServe(":8080", nil); err != nil {
		log.Fatal(err)
	}
}
开发者ID:apuigsech,项目名称:rest-layer,代码行数:57,代码来源:main.go


示例18: main

func main() {
	auth.Config(*domain, *port, *client, *secret)
	tpl := templates.New("templates")

	// chain authenticated middleware
	c := xhandler.Chain{}
	c.UseC(func(next xhandler.HandlerC) xhandler.HandlerC {
		return auth.NewMiddleware(next)
	})

	// server static assets files
	fs := http.FileServer(http.Dir("assets"))
	http.Handle("/assets/", http.StripPrefix("/assets/", fs))

	http.HandleFunc("/auth", func(w http.ResponseWriter, r *http.Request) {
		code := r.URL.Query().Get("code")
		token, err := auth.GetToken(code)
		if err != nil {
			http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
		} else {
			id, err := db.CreateUser(token)
			if err != nil {
				tpl.Error(w, err)
			} else {
				auth.SaveSession(w, id)
				http.Redirect(w, r, "/entries/new", http.StatusFound)
			}
		}
	})

	http.Handle("/entries/", c.Handler(
		xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
			u := ctx.Value("user").(*user.User)

			switch r.Method {
			case "GET":
				switch r.URL.Path[len("/entries/"):] {
				case "":
					entries, err := db.FindEntries(u.ID, 200)
					if err != nil {
						tpl.Error(w, err)
					} else {
						tpl.Render(w, "entries", entries)
					}
				case "new":
					tpl.Render(w, "new_entry", u)
				default:
					tpl.NotFound(w)
				}
			case "POST":
				rate := r.FormValue("rate")
				desc := r.FormValue("description")
				_, err := db.CreateEntry(u.ID, rate, desc)
				if err != nil {
					tpl.Error(w, err)
				} else {
					http.Redirect(w, r, "/entries", http.StatusFound)
				}
			default:
				http.Error(w, "", http.StatusMethodNotAllowed)
			}
		})))

	http.HandleFunc("/logout", func(w http.ResponseWriter, r *http.Request) {
		auth.DestroySession(w)
		http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
	})

	http.HandleFunc("/about", func(w http.ResponseWriter, r *http.Request) {
		tpl.Render(w, "about", nil)
	})

	http.HandleFunc("/demo", func(w http.ResponseWriter, r *http.Request) {
		auth.SaveSession(w, demo_id)
		http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
	})

	http.Handle("/stats/", c.Handler(
		xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
			u := ctx.Value("user").(*user.User)

			switch r.Method {
			case "GET":
				entries, err := db.FindEntries(u.ID, 10)
				if err == nil {
					p := struct {
						Distribution map[string]int
						Rate         entry.RateStats
					}{
						entry.FeelingsDistribution(entries),
						entry.RateByDay(entries),
					}
					if len(entries) == 0 {
						tpl.Render(w, "entries", entries)
						return
					}
					json, err := json.Marshal(p)
					if err == nil {
						tpl.Render(w, "stats", template.JS(json))
						return
//.........这里部分代码省略.........
开发者ID:dementiahackers,项目名称:attentif,代码行数:101,代码来源:server.go



注:本文中的github.com/rs/xhandler.Chain类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang htmltest.Curl函数代码示例发布时间:2022-05-28
下一篇:
Golang xhandler.HandlerFuncC函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap