コード例 #1
0
ファイル: demo.go プロジェクト: marknewmail/gof
func main() {
	app := &HttpApp{}

	// MVC路由表
	routes := mvc.NewRoute(nil)

	// 注册控制器,test与/test/index中的test对应
	routes.NormalRegister("test", testControllerGenerator)

	// 注册单例控制器
	routes.Register("test_all", &testController{})

	// 除了控制器,可以添加自定义的路由规则(正则表达式)
	routes.Add("^/[0-9]$", func(ctx *web.Context) {
		// 直接输出内容
		ctx.Response.Write([]byte("数字路径"))
		return
	})

	// 默认页路由
	routes.Add("/", func(ctx *web.Context) {
		//		sysName := ctx.App.Config().GetString("SYS_NAME")
		//		ctx.ResponseWriter.Write([]byte("Hello," + sysName + "!"))
		//		return

		// 使用模板
		data := gof.TemplateDataMap{
			"变量名": "变量值",
		}
		ctx.App.Template().Execute(ctx.Response, data, "template.html")
		return

		// 使用会话
		ctx.Session().Set("user", "jarrysix")
		ctx.Session().Save()

		// 使用DB和ORM
		db := ctx.App.Db()
		orm := db.GetOrm()
		_ = orm.Version()
	})

	// 使用一个拦截器,来拦截请求。
	// 拦截器里可以决定,访问对应的路由表。
	// 一个系统通常有多个子系统,每个子系统对应一个路由表。
	var in = getInterceptor(app, routes)
	go http.ListenAndServe(":8080", in)

	log.Println("[ OK] - web is listening on port :8080.")
	var ch = make(chan int, 1)
	<-ch
}
コード例 #2
0
ファイル: route.go プロジェクト: jackyan/go2o
 * author : jarryliu
 * date : 2014-02-05 21:53
 * description :
 * history :
 */
package ols

import (
	"github.com/jsix/gof/web"
	"github.com/jsix/gof/web/mvc"
	"go2o/src/app/front/shop/ols/mos"
	"go2o/src/app/util"
)

var (
	routes *mvc.Route = mvc.NewRoute(nil)
)

//处理请求
func Handle(ctx *web.Context) {
	switch util.GetBrownerDevice(ctx) {
	default:
	case util.DevicePC:
		ctx.Items["device_view_dir"] = "pc"
		routes.Handle(ctx)
	case util.DeviceTouchPad, util.DeviceMobile:
		ctx.Items["device_view_dir"] = "touchpad"
		mos.Handle(ctx)
	case util.DeviceAppEmbed:
		ctx.Items["device_view_dir"] = "app_embed"
		routes.Handle(ctx)