} func (p *Plugin) RegisterRoutes(routes ...mohttp.Route) { p.routes = append(p.routes, routes...) } func (p *Plugin) StatePath(paths ...string) string { if len(paths) == 0 { return p.stateDir } paths = append([]string{p.stateDir}, paths...) return filepath.Join(paths...) } 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 }))
package server import ( "github.com/jonasi/mohttp" "github.com/jonasi/mohttp/hateoas" "github.com/jonasi/mohttp/middleware" "github.com/jonasi/project/server/api" "golang.org/x/net/context" ) var setSrv, getSrv = mohttp.ContextValueAccessors("github.com/jonasi/project/server.Server") func getServer(c context.Context) *Server { return getSrv(c).(*Server) } var apiService = hateoas.NewService( hateoas.AddResource(root, version, status, plugins), hateoas.ServiceUse(api.JSON, api.AddLinkHeaders), ) var root = hateoas.NewResource( hateoas.Path("/"), hateoas.AddLink("version", version), hateoas.AddLink("status", status), hateoas.AddLink("plugins", plugins), hateoas.HEAD(mohttp.EmptyBodyHandler), ) var version = hateoas.NewResource( hateoas.Path("/version"),
package main import ( "golang.org/x/net/context" "os" "github.com/jonasi/mohttp" "github.com/jonasi/project/server/plugin" ) const version = "0.0.1" var setCmd, getCmd = mohttp.ContextValueAccessors("github.com/jonasi/project/plugins/project-shell") func getCommander(c context.Context) *Commander { return getCmd(c).(*Commander) } func main() { pl := plugin.New("shell", version) pl.RegisterAPI(apiService) if code := pl.Parse(os.Args); code > 0 { os.Exit(code) } cmder := NewCommander(pl.StatePath("commands")) if err := cmder.Init(); err != nil { pl.Error("Commander init error", "error", err)
package hateoas import ( "github.com/jonasi/mohttp" "golang.org/x/net/context" ) var setService, getService = mohttp.ContextValueAccessors("github.com/jonasi/mohttp/hateoas.Service") type ServiceOption func(*Service) func AddResource(r ...*Resource) ServiceOption { return func(s *Service) { s.resources = append(s.resources, r...) } } func ServiceUse(h ...mohttp.Handler) ServiceOption { return func(s *Service) { s.Use(h...) } } func GetService(c context.Context) (*Service, bool) { svc, ok := getService(c).(*Service) return svc, ok } func NewService(opts ...ServiceOption) *Service { s := &Service{ resources: []*Resource{},
package main import ( "github.com/jonasi/mohttp" "github.com/jonasi/project/server/plugin" "golang.org/x/net/context" "os" ) const pluginVersion = "0.0.1" var setBrew, _getBrew = mohttp.ContextValueAccessors("github.com/jonasi/project/plugins/project-brew.Brew") func getBrew(c context.Context) *BrewServer { return _getBrew(c).(*BrewServer) } func main() { pl := plugin.New("brew", pluginVersion) pl.Use(setBrew(NewBrewServer(pl.Logger))) pl.RegisterAPI(apiService) os.Exit(pl.RunCmd(os.Args)) }