Ejemplo n.º 1
0
func MustBuildTplOneFile(in []byte) (out []byte) {
	//把所有的`xxx` 转成 "xxx\xxx" 之类的,严格保留里面的所有字符串,暂时没有任何办法可以打出 `
	outbuf := &bytes.Buffer{}
	thisHereDocBuf := &bytes.Buffer{}
	isInHereDoc := false
	for i := range in {
		//进入heredoc
		if !isInHereDoc && in[i] == '`' {
			isInHereDoc = true
			continue
		}
		//出heredoc
		if isInHereDoc && in[i] == '`' {
			isInHereDoc = false
			outbuf.Write(kmgJson.MustMarshal(thisHereDocBuf.String()))
			thisHereDocBuf.Reset()
			continue
		}
		//不是heredoc的部分
		if !isInHereDoc {
			outbuf.WriteByte(in[i])
			continue
		}
		//是heredoc的部分
		if isInHereDoc {
			thisHereDocBuf.WriteByte(in[i])
			continue
		}
	}
	if isInHereDoc {
		panic("end with heredoc")
	}
	return outbuf.Bytes()
}
Ejemplo n.º 2
0
func Log(category string, msg string, obj interface{}) {
	logPath := defaultEnv.Env.LogPath
	toWrite := append(kmgJson.MustMarshal(logRow{
		Time: time.Now().Format(time.RFC3339),
		Msg:  msg,
		Obj:  obj,
	}), byte('\n'))
	err := kmgFile.AppendFile(filepath.Join(logPath, category+".log"), toWrite)
	if err != nil {
		panic(err)
	}
	return
}
Ejemplo n.º 3
0
func (c *Context) WriteToResponseWriter(w http.ResponseWriter, req *http.Request) {
	for key, value := range c.responseHeader {
		w.Header().Set(key, value)
	}
	if c.sessionMap != nil && c.sessionHasSet {
		http.SetCookie(w, &http.Cookie{
			Name:  SessionCookieName,
			Value: kmgCrypto.CompressAndEncryptBase64Encode(&SessionPsk, kmgJson.MustMarshal(c.sessionMap)),
		})
	}
	if c.redirectUrl != "" {
		http.Redirect(w, req, c.redirectUrl, c.responseCode)
		return
	}
	w.WriteHeader(c.responseCode)
	if c.responseBuffer.Len() > 0 {
		w.Write(c.responseBuffer.Bytes())
	}
}
Ejemplo n.º 4
0
func MustUpdateJson(key string, obj interface{}) {
	b := kmgJson.MustMarshal(obj)
	MustUpdate(key, string(b))
}
Ejemplo n.º 5
0
func MustInsertJson(key string, obj interface{}) {
	b := kmgJson.MustMarshal(obj)
	MustInsert(key, string(b))
}
Ejemplo n.º 6
0
func (c *Context) WriteJson(obj interface{}) {
	c.responseBuffer.Write(kmgJson.MustMarshal(obj))
}