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)
	}

}
func (c *GenericSQLBuilder) toSQL0(withGroup bool, withOrderBy bool, withLimit bool) string {
	var sql = string("select ")
	sql += c.toSelectColumns() + " from " + c.Table
	if len(c.conditions) > 0 {
		sql += " where "
		for i, condition := range c.conditions {
			if i > 0 {
				sql += " and "
			}
			sql += condition
		}
	}

	log.Redln("conditions", c.conditions, "parameters", c.parameters, "offset", c.Offset, "rowCount", c.RowCount)
	if withGroup {
		sql += c.toGroupBySql()
	}
	if withOrderBy {
		sql += c.toOrderBySQL()
	}
	if withLimit {
		sql += c.toLimitSQL()
	}
	return sql
}
Exemplo n.º 3
0
func (q *Querier) where(c Condition) *Querier {
	sql, err := c.ToSQL(q.SQLBuilder)
	log.Redln("sql", sql, "err", err, "Condition", c)
	if nil == err {
		q.SQLBuilder.Where(sql, c.Params)
	}
	return q
}
Exemplo n.º 4
0
func (c *ArticleController) New() {
	user := c.GetSession("user")
	log.Redln("*******************", user)
	if nil == user {
		c.Redirect("/login", 302)
	} else {
		c.TplNames = "article_create.html"
	}
}
Exemplo n.º 5
0
Arquivo: db.go Projeto: elago/elapen
func initDbEngine() {
	config := global.Config
	engineType, err := config.GetString("_", "engine")
	// if engine not define, it's install mode, skip database engine initial
	if err != nil || global.Install {
		return
	}

	if engineType == "mysql" {
		username := config.GetStringDefault("mysql", "username", "root")
		password := config.GetStringDefault("mysql", "password", "")
		host := config.GetStringDefault("mysql", "host", "127.0.0.1")
		port := config.GetIntDefault("mysql", "port", 3306)
		dbName := "elapen"
		dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8", username, password, host, port, dbName)

		engine, err = xorm.NewEngine("mysql", dsn)
		if err != nil {
			log.Redln(err)
		} else {
			log.Greenf("use mysql ")
			log.Bluef("%s\n", dsn)
		}
	} else {
		dsn := config.GetStringDefault("sqlite", "path", "./data.sqlite")

		engine, err = xorm.NewEngine("sqlite3", dsn)
		if err != nil {
			log.Redln(err)
		} else {
			log.Greenf("use sqlite ")
			log.Bluef("%s\n", dsn)
		}
	}

	runMode := config.GetStringDefault("_", "runmode", "prod")
	if runMode == "dev" {
		global.Engine.ShowSQL(true)
	}

	global.Engine = engine
}
Exemplo n.º 6
0
func (c *BaseController) NewPagination() *db.Pagination {

	page, err := c.GetInt("page")
	if nil != err {
		page = 1
	}
	log.Redln("page", page)
	pagination := db.NewPagination(page, 0, false)
	link, _ := url.ParseRequestURI(c.Ctx.Request.URL.String())
	pagination.SetUrl(link)
	return pagination
}
Exemplo n.º 7
0
func initRedis() {
	// cc = &cache.Cache{}
	var err error

	defer func() {
		if r := recover(); r != nil {
			log.Redf("initial redis error caught: %v\n", r)
			cc = nil
		}
	}()

	cc, err = cache.NewCache("redis", `{"conn":"`+beego.AppConfig.String("redis_host")+`"}`)

	if err != nil {
		log.Redln(err)
	}
}
Exemplo n.º 8
0
func (q *Querier) FillPagination(container interface{}, pagination *Pagination) (int64, error) {

	var err error
	var count int64
	var totalItem int
	var page = pagination.Page
	var pageSize = pagination.PerPage

	log.Redln("page", page, "pageSize", pageSize)
	q.Limit((page-1)*pageSize, pageSize)
	q.Raw(q.ToCountSql(), q.Parameters()).QueryRow(&totalItem)

	var sql = q.ToSql()
	count, err = q.Raw(sql, q.Parameters()).QueryRows(container)

	pagination.Total = totalItem
	pagination.hasNext = pagination.TotalPages() > page
	pagination.SetData(container)

	return count, err
}
Exemplo n.º 9
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)
		}
	}
}
Exemplo n.º 10
0
func TestConfig(t *testing.T) {
	Convey("Test Config sections", t, func() {
		conf := NewConfig("etc/test.ini")
		content, err := conf.readConfigFile()
		if err != nil {
			log.Redln(err)
		} else {
			log.Greenln("raw content")
			log.Pinkln("==============")

			log.Blueln(content)
			content = conf.filterComment()

			log.Greenln("filter comment")
			log.Pinkln("==============")

			log.Blueln(content)
			arraylize := conf.arraylize()

			log.Greenln("arraylize")
			log.Pinkln("==============")
			count := len(arraylize)
			log.Bluef("[%d]\n", count)
			for i := 0; i < count; i++ {
				log.Bluef("[%d]\t%s\n", i, arraylize[i])
			}

			log.Greenln("parse items")
			log.Pinkln("==============")
			conf.parseItems()

			log.Greenln("warning stack")
			log.Pinkln("==============")
			log.Blueln(conf.GetWarnings())

			log.Greenln("mistake value")
			log.Pinkln("==============")
			log.Blueln(conf.GetString("mysql", "host"))

			log.Greenln("about bool")
			log.Pinkln("==============")
			conf.SetBool("test", "dev", true)
			dev, _ := conf.GetBool("_", "dev")
			log.Blueln("%v", dev)

			content = conf.serialize()
			log.Greenln("serialize value")
			log.Pinkln("==============")
			log.Blueln(content)

			log.Greenln("hex")
			log.Pinkln("==============")
			hex, _ := conf.GetInt("_", "hex")
			log.Blueln(hex)

			log.Greenln("get empty")
			log.Pinkln("==============")
			_, err = conf.GetInt("_", "heloo")
			if err != nil {
				log.Blueln(err)
			}

			log.Greenln("save config file")
			log.Pinkln("==============")
			err := conf.Save("tmp/test.ini")
			if err == nil {
				log.Blueln("done!")
			}

		}

		val1, _ := conf.Get("_", "port")
		So(val1, ShouldEqual, 80)

		val2, _ := conf.GetString("_", "appname")
		So(val2, ShouldEqual, "my application")

		val3, _ := conf.GetString("mysql", "password")
		So(val3, ShouldEqual, "\"liju")

		val4, _ := conf.GetString("mysql", "host")
		So(val4, ShouldEqual, `"192.168.1.11" = GHJ`)

		val5, _ := conf.GetBool("_", "dev")
		So(val5, ShouldEqual, true)

		val6, _ := conf.GetFloat("_", "pi")
		So(val6, ShouldEqual, 3.14)

		val7, _ := conf.GetInt("_", "hex")
		So(val7, ShouldEqual, 0x24)

		val8 := conf.GetIntDefault("section", "key", 100)
		So(val8, ShouldEqual, 100)

		val9 := conf.GetStringDefault("mysql", "key", "")
		So(val9, ShouldEqual, "")
	})
}