Exemplo n.º 1
0
func Parse(frame string, cli *Client) {
	log.Yellowln(frame)
	json, err := com.JsonDecode(frame)
	if err != nil {
		log.Redln(err)
	} else {
		data := json.(map[string]interface{})

		actonName, ok := data["action"].(string)
		if !ok {
			cli.Write("invalid request, action name is not exist.")
			return
		}

		// 如果不是登录请求,并且用户处于未登录状态,禁止通行
		if actonName != "login" {
			if !cli.login {
				cli.Write("you have not login.")
				return
			}
		}

		RouterMap[actonName].Tcp(data, cli)
	}

}
Exemplo n.º 2
0
func (ela *Elaeagnus) servController(path string, ctx Context) {
	controller := getController(path)
	if controller == nil {
		servError(&ctx, "<h2>404, File Not Exist</h2>", 404, false)
		return
	}

	routerElement := controller.(uriMode)
	f := routerElement.fun
	params := routerElement.argsMap

	ctx.setURIParam(params)
	if f != nil {

		functions := f

		defer func() {
			if r := recover(); r != nil {

				var stack string
				for i := 1; ; i++ {
					_, file, line, ok := runtime.Caller(i)
					if !ok {
						break
					}
					stack = stack + fmt.Sprintln(fmt.Sprintf("%s:%d", file, line))
				}

				content := "<h2>500 Server Internal Error!</h2>\n\n" + fmt.Sprintf("%s", r) + "\n\n" + "<pre>" + stack + "</pre>"
				log.Redln(r)
				log.Yellowln(stack)

				servError(&ctx, content, 500, false)
				return
			}
		}()

		// execute before controllers
		if beforeController != nil && routerElement.withBefore {
			_, err := ela.injector.injectFuc(beforeController)
			if err != nil {
				log.Redf("injection failed: %s\n", err)
			}
		}

		// execute controllers
		for i := 0; i < len(functions); i++ {
			if !ctx.GetResponseWriter().HasFlushed() {
				function := functions[i]
				_, err := ela.injector.injectFuc(function)
				if err != nil {
					log.Redf("injection failed: %s\n", err)
				}
			}
		}

		// execute after controllers
		if afterController != nil && routerElement.withBefore {
			_, err := ela.injector.injectFuc(afterController)
			if err != nil {
				log.Redf("injection failed: %s\n", err)
			}
		}

	} else {
		// if static-alias does not exist, using default mode
		if staticExist(path) {
			staticServ(path, &ctx)
		} else {
			servError(&ctx, "<h2>404, File Not Exist</h2>", 404, false)
		}
	}
}