Example #1
0
func EtagHandlerFunc(fn func(context.Context) (interface{}, string, error)) mohttp.Handler {
	return mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		d, etag, err := fn(c)

		if err != nil {
			return nil, err
		}

		var (
			found = mohttp.GetRequest(c).Header.Get("If-None-Match")
			rw    = mohttp.GetResponseWriter(c)
		)

		rw.Header().Set("ETag", etag)

		if etag == found {
			rw.WriteHeader(304)
			return mohttp.DataNoBody, nil
		}

		return d, nil
	})
}
Example #2
0
	hateoas.ServiceUse(api.JSON),
	hateoas.AddResource(root, cpuRsc, loadRsc, procRsc),
)

var root = hateoas.NewResource(
	hateoas.Path("/"),
	hateoas.AddLink("cpus", cpuRsc),
	hateoas.AddLink("load", loadRsc),
	hateoas.AddLink("processes", procRsc),
	hateoas.HEAD(mohttp.EmptyBodyHandler),
)

var cpuRsc = hateoas.NewResource(
	hateoas.Path("/cpus"),
	hateoas.GET(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		return cpu.CPUInfo()
	})),
)

var loadRsc = hateoas.NewResource(
	hateoas.Path("/load"),
	hateoas.GET(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		return load.LoadAvg()
	})),
)

var procRsc = hateoas.NewResource(
	hateoas.Path("/processes"),
	hateoas.GET(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		return process.Pids()
	})),
Example #3
0
		commands,
		command,
		stdout,
		stderr,
	),
)

var root = hateoas.NewResource(
	hateoas.Path("/"),
	hateoas.AddLink("commands", commands),
)

var commands = hateoas.NewResource(
	hateoas.Path("/commands"),
	hateoas.GET(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		commander := getCommander(c)
		return commander.History(), nil
	})),
	hateoas.POST(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		var (
			logger    = plugin.GetLogger(c)
			commander = getCommander(c)
		)

		var args struct {
			Args []string `json:"args"`
		}

		if err := json.NewDecoder(mohttp.GetRequest(c).Body).Decode(&args); err != nil {
			logger.Error("JSON decoding error", "error", err)
			return nil, err
		}
Example #4
0
			return
		}

		if body.Status == "inactive" {
			getServer(c).Close()
		}
	})),
)

var plugins = hateoas.NewResource(
	hateoas.Path("/plugins"),
	hateoas.GET(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		var (
			plugins = getServer(c).plugins
			resp    = make([]pl, len(plugins))
		)

		i := 0
		for _, p := range plugins {
			resp[i].Name = p.name
			i++
		}

		return resp, nil
	})),
)

type pl struct {
	Name string `json:"name"`
}
Example #5
0
		installedFormula,
		formula,
		search,
	),
)

var root = hateoas.NewResource(
	hateoas.Path("/"),
	hateoas.AddLink("version", version),
	hateoas.AddLink("formulae", formulae),
)

var version = hateoas.NewResource(
	hateoas.Path("/version"),
	hateoas.GET(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		return getBrew(c).Version()
	})),
)

var config = hateoas.NewResource(
	hateoas.Path("/config"),
	hateoas.GET(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		return getBrew(c).Config()
	})),
)

var env = hateoas.NewResource(
	hateoas.Path("/env"),
	hateoas.GET(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		return getBrew(c).Env()
	})),