예제 #1
0
파일: ctrl.go 프로젝트: zaolab/sunnified
func (this *ControllerHandler) GetControlManager(context *web.Context) (cm *controller.ControlManager) {
	var (
		act    string = this.action
		action string
		acterr error
	)

	if action, acterr = context.PData.String("action"); acterr == nil {
		act = action
	}
	if act == "" {
		act = "_"
	}

	if acterr == nil && act != "_" && !this.controlmeta.HasAction(act) && this.controlmeta.HasAction("index") {
		act = "_"
		lenupath := len(context.UPath) + 1
		tmpupath := make(web.UPath, lenupath)
		tmpupath[0] = action
		copy(tmpupath[1:lenupath], context.UPath)
		context.UPath = tmpupath
		delete(context.PData, "action")
	}

	cm = controller.NewControlManager(context, this.controlmeta, act)

	return
}
예제 #2
0
파일: handler.go 프로젝트: zaolab/sunnified
func (this *DynamicHandler) GetControlManager(context *web.Context) (cm *controller.ControlManager) {
	this.mutex.RLock()
	var mod, control, act string = this.module, this.controller, this.action
	this.mutex.RUnlock()

	var acterr error
	var action string

	// if mod is not found, the default mod will be used
	// this allows user to use shortcut path
	// especially when there is only one mod
	if s, err := context.PData.String("module"); err == nil {
		if this.ctrlgroup.HasModule(s) || !this.ctrlgroup.HasController(mod, s) {
			mod = s
		} else {
			// controller.HasController(mod, s) == true
			// move {action} to UPath, {controller} to {action}, {mod} to {controller}
			if tmpact, err := context.PData.String("action"); err == nil {
				lenupath := len(context.UPath) + 1
				tmpupath := make(web.UPath, lenupath)
				tmpupath[0] = tmpact
				copy(tmpupath[1:lenupath], context.UPath)
				context.UPath = tmpupath
			}
			if tmpctrl, err := context.PData.String("controller"); err == nil {
				context.PData["action"] = tmpctrl
			}

			context.PData["controller"] = s
			context.PData["module"] = mod
		}
	}
	if s, err := context.PData.String("controller"); err == nil {
		control = s
	}
	if action, acterr = context.PData.String("action"); acterr == nil {
		act = action
	}
	if act == "" {
		act = "_"
	}

	if ctrl := this.ctrlgroup.Controller(mod, control); ctrl != nil {
		// allows for shortcut action to index
		if acterr == nil && act != "_" && !ctrl.HasAction(act) && ctrl.HasAction("index") {
			act = "_"
			lenupath := len(context.UPath) + 1
			tmpupath := make(web.UPath, lenupath)
			tmpupath[0] = action
			copy(tmpupath[1:lenupath], context.UPath)
			context.UPath = tmpupath
			delete(context.PData, "action")
		}

		cm = controller.NewControlManager(context, ctrl, act)
	}

	return
}
예제 #3
0
func (this *WebSocketHandler) ServeContextHTTP(context *web.Context) {
	var ctrlmgr *controller.ControlManager = controller.NewControlManager(context, this.ctrl, "_")

	if err := context.ToWebSocket(nil, nil); err != nil {
		context.RaiseAppError("Unable to upgrade to websocket: " + err.Error())
	}

	defer func() {
		if err := recover(); err != nil {
			log.Println(err)
		}
	}()

	ctrlmgr.Prepare()

	var (
		ctrler  = ctrlmgr.Controller()
		listen  = &methMeta{method: ctrler.MethodByName("Listen_")}
		methods = make(map[string]*methMeta)
		meth    *methMeta
		ok      bool
		t       reflect.Type
	)

	if listen.method.IsValid() {
		t = listen.method.Type()
		if t.NumIn() != 2 || t.In(0) != intType || t.In(1) != retbType {
			listen.method = reflect.Value{}
		}
	}
	listen.rettype = getRetType(listen.method, t)
	listen.isvalid = listen.method.IsValid()

	for {
		msgT, p, err := context.WebSocket.ReadMessage()

		if err != nil {
			if m := ctrler.MethodByName("Error_"); m.IsValid() && m.Type().NumIn() == 1 &&
				m.Type().In(0) == errType {

				m.Call([]reflect.Value{reflect.ValueOf(err)})
			}

			break
		}

		if this.allowcmd && msgT == websocket.TextMessage && len(p) > 0 && p[0] == '/' {
			args := WSArgs(argSplit.Split(strings.TrimSpace(string(p[1:len(p)])), -1))
			cmd := strings.Replace(strings.Title(args[0]), "-", "_", -1)

			if cmd[len(cmd)-1] != '_' && validCmd.MatchString(cmd) {
				args = args[1:len(args)]

				if meth, ok = methods[cmd]; !ok {
					m := ctrler.MethodByName(cmd)

					if m.IsValid() {
						t = m.Type()
						if t.NumIn() != 1 || t.In(0) != argsType {
							m = reflect.Value{}
						}
						meth = &methMeta{
							method:  m,
							rettype: getRetType(m, t),
							isvalid: m.IsValid(),
						}
					} else {
						meth = &methMeta{
							method:  m,
							rettype: 0,
							isvalid: false,
						}
					}

					methods[cmd] = meth
				}

				if meth.isvalid {
					write(meth, context.WebSocket, meth.method.Call([]reflect.Value{reflect.ValueOf(args)}))
					continue
				}
			}
		}

		if listen.isvalid {
			write(listen, context.WebSocket,
				listen.method.Call([]reflect.Value{reflect.ValueOf(msgT), reflect.ValueOf(p)}))
		}
	}
}