// API returns a handler for a set of routes.
API returns a handler for a set of routes.
// API returns a handler for a set of routes .
func API(log *log.Logger, masterDB *db.DB, authenticator *auth.Authenticator) http.Handler {
	authmw := mid.Auth{
		Authenticator: authenticator,
	}
	app := web.New(log, mid.RequestLogger, mid.Metrics, mid.ErrorHandler)
	h := Health{
		MasterDB: masterDB,
	}
	app.Handle("GET", "/v1/health", h.Check)
	u := User{
		MasterDB:       masterDB,
		TokenGenerator: authenticator,
	}
	app.Handle("GET", "/v1/users", u.List, authmw.Authenticate, authmw.HasRole(auth.RoleAdmin))
	app.Handle("POST", "/v1/users", u.Create, authmw.Authenticate, authmw.HasRole(auth.RoleAdmin))
	app.Handle("GET", "/v1/users/:id", u.Retrieve, authmw.Authenticate)
	app.Handle("PUT", "/v1/users/:id", u.Update, authmw.Authenticate, authmw.HasRole(auth.RoleAdmin))
	app.Handle("DELETE", "/v1/users/:id", u.Delete, authmw.Authenticate, authmw.HasRole(auth.RoleAdmin))
	app.Handle("GET", "/v1/users/token", u.Token)
	p := Product{
		MasterDB: masterDB,
	}
	app.Handle("GET", "/v1/products", p.List, authmw.Authenticate)
	app.Handle("POST", "/v1/products", p.Create, authmw.Authenticate)
	app.Handle("GET", "/v1/products/:id", p.Retrieve, authmw.Authenticate)
	app.Handle("PUT", "/v1/products/:id", p.Update, authmw.Authenticate)
	app.Handle("DELETE", "/v1/products/:id", p.Delete, authmw.Authenticate)
	return app
}
func API ( log * log . Logger , masterDB * db . DB , authenticator * auth . Authenticator ) http . Handler { authmw := mid . Auth { Authenticator : authenticator , } app := web . New ( log , mid . RequestLogger , mid . Metrics , mid . ErrorHandler ) h := Health { MasterDB : masterDB , } app . Handle ( " " , " " , h . Check ) u := User { MasterDB : masterDB , TokenGenerator : authenticator , } app . Handle ( " " , " " , u . List , authmw . Authenticate , authmw . HasRole ( auth . RoleAdmin ) ) app . Handle ( " " , " " , u . Create , authmw . Authenticate , authmw . HasRole ( auth . RoleAdmin ) ) app . Handle ( " " , " " , u . Retrieve , authmw . Authenticate ) app . Handle ( " " , " " , u . Update , authmw . Authenticate , authmw . HasRole ( auth . RoleAdmin ) ) app . Handle ( " " , " " , u . Delete , authmw . Authenticate , authmw . HasRole ( auth . RoleAdmin ) ) app . Handle ( " " , " " , u . Token ) p := Product { MasterDB : masterDB , } app . Handle ( " " , " " , p . List , authmw . Authenticate ) app . Handle ( " " , " " , p . Create , authmw . Authenticate ) app . Handle ( " " , " " , p . Retrieve , authmw . Authenticate ) app . Handle ( " " , " " , p . Update , authmw . Authenticate ) app . Handle ( " " , " " , p . Delete , authmw . Authenticate ) return app }
API returns a handler for a set of routes.
API returns a handler for a set of routes .
// authmw is used for authentication/authorization middleware. // TODO(jlw) Figure out why the order of these was reversed and maybe reverse it back. // Register health check endpoint. This route is not authenticated. // Register user management and authentication endpoints. // This route is not authenticated // Register product and sale endpoints.