示例#1
0
文件: tpl.go 项目: gernest/legend
func (r *Renderer) Serve(talk core.Talk) {
	var (
		name           = "name"
		templateContex = "data"
	)

	obj := util.NewJSONHelper()
	rst := util.NewJSONHelper()

	err := obj.Decode(talk.Request())
	if err != nil {
		//TODO?
	}
	if obj.HasKey(name) {
		tname := obj.GetString(name)
		data := obj.Get(templateContex)

		out := &bytes.Buffer{}
		rerr := r.tpl.ExecuteTemplate(out, tname, data)
		if rerr != nil {
			//TODO?
		}
		rst.Set("output", out.String())
	}

	rawOut, err := rst.Encode()
	if err != nil {
		// TODO handle error?
	}
	talk.Respond() <- bytes.NewReader(rawOut)
}
示例#2
0
文件: mdown.go 项目: gernest/legend
func (m *Markdown) Serve(talk core.Talk) {
	data, err := ioutil.ReadAll(talk.Request())
	if err != nil {
		//TODO Do error handling?
	}
	out := mark.Render(string(data))
	talk.Respond() <- strings.NewReader(out)
}
示例#3
0
文件: store.go 项目: gernest/legend
func (d *DB) Serve(talk core.Talk) {
	var (
		action  = "action"
		payload = "payload"
		keyID   = "id"
	)
	obj := util.NewJSONHelper()
	rst := util.NewJSONHelper()
	err := obj.Decode(talk.Request())
	if err != nil {
		// TODO handle error
	}
	id := uuid.NewRandom().String()

	if obj.HasKey(action) {
		switch obj.GetString(action) {
		case "save":
			if obj.HasKey(payload) {
				raw, err := json.Marshal(obj.Get(payload))
				if err != nil {
					// TODO handle error?
				}
				rst.Set("id", id)
				mu.Lock()
				data[id] = raw
				mu.Unlock()
			}
		case "get":
			if obj.HasKey(keyID) {
				id := obj.GetString(keyID)
				mu.Lock()
				out := data[id]
				mu.Unlock()
				rst.Set(payload, string(out))
				rst.Set(keyID, id)
			}
		}
	}

	rawOut, err := rst.Encode()
	if err != nil {
		// TODO handle error?
	}
	talk.Respond() <- bytes.NewReader(rawOut)
}
示例#4
0
func (s *SimpleServ) Serve(tal core.Talk) {
	tal.Respond() <- tal.Request()
}