Example #1
0
func registHandler() {
	factory := Common.GetIOCFactory()

	iHandlerType := reflect.TypeOf((*Server.IHandler)(nil)).Elem()

	factory.RegistByName("normalhandler", iHandlerType, reflect.TypeOf(new(Handler.NormalHandler)), Common.InstanceType_Normal)
}
Example #2
0
func (this *Site) init() {
	iControllerType := reflect.TypeOf((*IHandler)(nil)).Elem()
	factory := Common.GetIOCFactory()

	this.defaultMux = http.NewServeMux()
	this.inst = &http.Server{Addr: ":" + this.Port, Handler: this.defaultMux}

	if this.SiteSetting == nil {
		this.SiteSetting = newSiteSetting()
	}

	for _, setting := range this.SiteSetting.Handlers {
		if obj, ok := factory.GetByName(strings.ToLower(setting.Name), iControllerType, nil); ok == nil && obj != nil {
			controller := obj.(IHandler)
			controller.SetSite(this)

			this.defaultMux.Handle(setting.Pattern, controller)
		} else {
			if obj == nil {
				fmt.Printf("handler named<%s> not exist\n", setting.Name)
			} else {
				fmt.Printf("%s\n", ok.Error())
			}
		}
	}
}
Example #3
0
func registController() {
	i := reflect.TypeOf((*Server.IController)(nil)).Elem()

	factory := Common.GetIOCFactory()

	factory.RegistByName("test", i, reflect.TypeOf(new(Controller.TestController)), Common.InstanceType_Normal)

}
Example #4
0
func registCommand() {
	iCmd := reflect.TypeOf((*Command.ICommand)(nil)).Elem()

	factory := Common.GetIOCFactory()

	factory.RegistByName("stop", iCmd, reflect.TypeOf(new(Command.Stop)), Common.InstanceType_Singleton)
	factory.RegistByName("echo", iCmd, reflect.TypeOf(new(Command.Echo)), Common.InstanceType_Singleton)
	factory.RegistByName("start", iCmd, reflect.TypeOf(new(Command.Start)), Common.InstanceType_Singleton)
	factory.RegistByName("list", iCmd, reflect.TypeOf(new(Command.List)), Common.InstanceType_Singleton)
	factory.RegistByName("exit", iCmd, reflect.TypeOf(new(Command.Exit)), Common.InstanceType_Singleton)
	factory.RegistByName("quit", iCmd, reflect.TypeOf(new(Command.Exit)), Common.InstanceType_Singleton)
	factory.RegistByName("restart", iCmd, reflect.TypeOf(new(Command.ServerReload)), Common.InstanceType_Singleton)
}
Example #5
0
func main() {

	factory := Common.GetIOCFactory()

	new(typeRegist).Regist()

	iCmd := reflect.TypeOf((*Command.ICommand)(nil)).Elem()

	fmt.Printf("ServerStart\n")
	reader := bufio.NewReader(os.Stdin)

	sc := Server.GetCurrentServer()

	sc.Start()

	for {
		fmt.Print("cmd:")
		line, err := reader.ReadBytes('\n')
		if err != nil {
			fmt.Printf("Error:%s", err.Error())
			continue
		}
		args := string(line[:len(line)-2])

		var strCommand string

		var strParam string

		if spaceIndex := strings.Index(args, " "); spaceIndex >= 0 {
			runeArgs := []rune(args)
			strCommand = string(runeArgs[:spaceIndex])
			strParam = string(runeArgs[spaceIndex:])
		} else {
			strCommand = args
		}

		strCommand = strings.ToLower(strCommand)

		strParam = strings.Trim(strParam, " ")

		if obj, err := factory.GetByName(strCommand, iCmd, nil); err != nil || obj == nil {
			fmt.Printf("command<%s> not exist in system\n", strCommand)
			continue
		} else {
			command := obj.(Command.ICommand)
			command.DoCommand(strParam)
		}
	}
}
Example #6
0
func (this *List) DoCommand(param string) {
	fmt.Printf("System contains blow Command\n")

	factory := Common.GetIOCFactory()
	iBaseType := reflect.TypeOf((*ICommand)(nil)).Elem()

	array := factory.GetRegistKeys(iBaseType)

	index := 0

	for _, key := range array {
		if index > 0 && index%4 == 0 {
			fmt.Println()
		}
		index++
		fmt.Printf("%s\t", key)
	}
	fmt.Println()
}
Example #7
0
func (this *NormalHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	iControllerType := reflect.TypeOf((*Server.IController)(nil)).Elem()
	requestPath := strings.TrimLeft(r.URL.Path, "/")
	args := strings.Split(requestPath, "/")

	factory := Common.GetIOCFactory()

	if len(args) < 2 {
		w.WriteHeader(404)
		fmt.Printf("params count:%d not right need at least 2 params\n", len(args))
		return
	}

	controllerName := args[0]

	methodName := args[1]

	tempParmas := args[2:]

	context := &Server.RequestContext{ControllerName: controllerName, MethodName: methodName}

	if controller, ok := factory.GetByName(strings.ToLower(controllerName),
		iControllerType, map[string]interface{}{"Rw": w, "Request": r,
			"Site": this.site, "Context": context}); ok != nil || controller == nil {
		w.WriteHeader(404)
		if ok != nil {
			fmt.Printf("view path %s failed:%s\n", requestPath, ok.Error())
		} else {
			fmt.Printf("view path %s failed:controller named <%s> not regist\n", requestPath, controllerName)
		}
	} else {

		var results []interface{}
		var err error

		rUtil := new(Util.ReflectUtil)

		querys := r.URL.Query()

		if len(r.Form) == 0 && len(querys) == 0 {

			params := make([]interface{}, len(tempParmas))

			for i, arg := range tempParmas {
				params[i] = interface{}(arg)
			}

			results, err = rUtil.RunObjMethod(controller, methodName, params)
		} else {
			tPrams := make(map[string]interface{})

			for key, value := range querys {
				tPrams[key] = value[len(value)-1]
			}

			for key, value := range r.Form {
				tPrams[key] = value[len(value)-1]
			}

			results, err = rUtil.RunObjMapMethod(controller, methodName, tPrams)
		}
		if err != nil {
			fmt.Printf("view path %s failed:%s\n", requestPath, err.Error())
			w.WriteHeader(404)
		} else {
			if len(results) > 0 {
				fmt.Fprint(w, results[0])
			}
		}

	}
}