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