Ejemplo n.º 1
0
Archivo: db.go Proyecto: 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
}
Ejemplo n.º 2
0
func (c *BaseController) Prepare() {
	categories, _ := models.Categories()
	c.Data["categories"] = categories
	c.Data["showRightBar"] = true
	response := ResponseBody{Success: true}
	c.Data["response"] = response
	var args string
	method := c.Ctx.Request.Method
	if "GET" == method {
		args = c.Ctx.Request.RequestURI
	} else {
		args = c.Ctx.Request.Form.Encode()
	}
	log.Bluef(args)
}
Ejemplo n.º 3
0
Archivo: logger.go Proyecto: elago/ela
func responseLog(ctx Context) {
	content := "[%s] %s: complete %s %s %d\n"
	switch ctx.GetResponseWriter().Status() {
	case 301, 302:
		log.Bluef(content, tag, time.Now().Format(logTimeFormat), ctx.GetRequest().Method, ctx.GetRequest().RequestURI, ctx.GetResponseWriter().Status())
	case 304:
		log.Greenf(content, tag, time.Now().Format(logTimeFormat), ctx.GetRequest().Method, ctx.GetRequest().RequestURI, ctx.GetResponseWriter().Status())
	case 401, 403:
		log.Yellowf(content, tag, time.Now().Format(logTimeFormat), ctx.GetRequest().Method, ctx.GetRequest().RequestURI, ctx.GetResponseWriter().Status())
	case 404:
		log.Redf(content, tag, time.Now().Format(logTimeFormat), ctx.GetRequest().Method, ctx.GetRequest().RequestURI, ctx.GetResponseWriter().Status())
	case 500:
		log.Pinkf(content, tag, time.Now().Format(logTimeFormat), ctx.GetRequest().Method, ctx.GetRequest().RequestURI, ctx.GetResponseWriter().Status())
	default:
		log.Printf(content, tag, time.Now().Format(logTimeFormat), ctx.GetRequest().Method, ctx.GetRequest().RequestURI, ctx.GetResponseWriter().Status())
	}

}
Ejemplo n.º 4
0
// send request
func (this *JClient) Request(msg map[string]interface{}) (map[string]interface{}, error) {
	msgStr, err := com.JsonEncode(msg)

	if err != nil {
		return nil, err
	}

	if this.conn == nil {
		log.Warnln("Connection Not Exist")
		return nil, errors.New("Connection Not Exist")
	}

	_, err = this.conn.Write([]byte(msgStr + this.mark))
	if err != nil {
		log.Warnln("[Write Error]", err)
		this.conn.Close()
		return nil, err
	}

	content, err := this.read()

	if err != nil {
		return nil, err
	}

	// kick sep char
	reg := regexp.MustCompile(this.mark)
	content = reg.ReplaceAllString(content, "")

	if this.debug {
		log.Bluef("[judger/send:%s]\n%s\n", time.Now(), msgStr)
		log.Warnf("[judger/recv:%s]\n%s\n", time.Now(), content)
	}

	resp, err := com.JsonDecode(content)

	return resp.(map[string]interface{}), err
}
Ejemplo n.º 5
0
func Trace(format string, v ...interface{}) {
	log.Bluef(format, v...)
}
Ejemplo n.º 6
0
Archivo: model.go Proyecto: elago/orm
func PrintModels() {
	// log.Blueln(models["user_log"])
	log.Bluef("\n%s\n", models)
}
Ejemplo n.º 7
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, "")
	})
}