Example #1
0
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",
	}
}))
Example #2
0
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)
}))