if len(args.Args) < 1 { return nil, fmt.Errorf("Invalid args") } return commander.Run(args.Args...) })), ) var command = hateoas.NewResource( hateoas.Path("/commands/:id"), hateoas.AddLink("stdout", stdout), hateoas.AddLink("stderr", stderr), hateoas.GET(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) { var ( id = mohttp.GetPathValues(c).Params.String("id") run = getCommander(c).GetRun(id) ) if run == nil { return nil, mohttp.HTTPError(404) } return run, nil })), ) var stdout = hateoas.NewResource( hateoas.Path("/commands/:id/stdout"), hateoas.GET( mohttp.DataResponderHandler(&middleware.DetectTypeResponder{}),
})), ) var formulae = hateoas.NewResource( hateoas.Path("/formulae"), hateoas.GET( middleware.EtagHandlerFunc(func(c context.Context) (interface{}, string, error) { return getBrew(c).ListAll() }), ), ) var formula = hateoas.NewResource( hateoas.Path("/formulae/:formula"), hateoas.GET(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) { name := mohttp.GetPathValues(c).Params.String("formula") return getBrew(c).Info(name) })), ) var installed = hateoas.NewResource( hateoas.Path("/installed"), hateoas.GET( middleware.EtagHandlerFunc(func(c context.Context) (interface{}, string, error) { return getBrew(c).ListInstalled() }), ), ) var installedFormula = hateoas.NewResource( hateoas.Path("/installed/:formula"),
var setPlugin, getPlugin = mohttp.ContextValueAccessors("github.com/jonasi/project/plugin.Plugin") func GetPlugin(c context.Context) *Plugin { return getPlugin(c).(*Plugin) } func GetLogger(c context.Context) log15.Logger { return GetPlugin(c).Logger } var getVersion = mohttp.GET("/version", middleware.JSONHandler(func(c context.Context) (interface{}, error) { return map[string]interface{}{ "version": GetPlugin(c).version, }, nil })) var getAsset = mohttp.GET("/assets/*asset", middleware.FileHandler(func(c context.Context) string { p := GetPlugin(c) return path.Join("plugins", "project-"+p.name, "web", "public", mohttp.GetPathValues(c).Params.String("asset")) })) var getIndex = mohttp.GET("/", mohttp.TemporaryRedirectHandler("web")) var getWeb = mohttp.GET("/web/*web", middleware.TemplateHandler(func(c context.Context) (string, map[string]interface{}) { p := GetPlugin(c) return "index.html", map[string]interface{}{ "script": "/plugins/" + p.name + "/assets/app.js", } }))
package server import ( "github.com/jonasi/mohttp" "github.com/jonasi/mohttp/middleware" "golang.org/x/net/context" "path" ) var webRoutes = []mohttp.Route{ ServeIndex, ServeWeb, ServeAssets, } var ServeIndex = mohttp.GET("/", mohttp.TemporaryRedirectHandler("/web")) var ServeWeb = mohttp.GET("/web/*splat", middleware.TemplateHandler(func(c context.Context) (string, map[string]interface{}) { return "index.html", map[string]interface{}{ "script": "/assets/app.js", } })) var ServeAssets = mohttp.GET("/assets/*asset", middleware.FileHandler(func(c context.Context) string { n := mohttp.GetPathValues(c).Params.String("asset") return path.Join("web", "app", "public", n) }))