func handleClient(conn net.Conn, dict dictionary.Dictionary) { defer conn.Close() msg, ok := protocol.ReadMessage(conn) if !ok { return } fmt.Println(msg) errorMsg := "7: Error" successMsg := "6: Success" var retMsg string switch msg[0] { case '1': // get key := msg[3:] value, ok := dict.Get(key) if ok { retMsg = "5: " + value } else { retMsg = errorMsg } case '2': // add split := strings.Split(msg[3:], "\t") key, value := split[0], split[1] ok := dict.Add(key, value) if ok { retMsg = successMsg } else { retMsg = errorMsg } case '3': // remove key := msg[3:] ok := dict.Remove(key) if ok { retMsg = successMsg } else { retMsg = errorMsg } case '4': // update split := strings.Split(msg[3:], "\t") key, value := split[0], split[1] ok := dict.Update(key, value) if ok { retMsg = successMsg } else { retMsg = errorMsg } case '8': // server get } protocol.WriteMessage(conn, retMsg) }
func searchWord(rw http.ResponseWriter, req *http.Request) { word := req.FormValue("word") searchType := req.FormValue("searchtype") cardName := req.FormValue("cardname") var words *dictionary.Dictionary var dp []DictPlus if searchType == "english" { words = d.LookupEnglish(word) d1 := DictPlus{Dictionary: words, Word: word, CardName: cardName} dp = make([]DictPlus, 1) dp[0] = d1 } else { words = d.LookupPinyin(word) numTrans := 0 for _, entry := range words.Entries { numTrans += len(entry.Translations) } dp = make([]DictPlus, numTrans) idx := 0 for _, entry := range words.Entries { for _, trans := range entry.Translations { dict := new(dictionary.Dictionary) dict.Entries = make([]*dictionary.Entry, 1) dict.Entries[0] = entry dp[idx] = DictPlus{ Dictionary: dict, Word: trans, CardName: cardName} idx++ } } } //t := template.New("PinyinTemplate") t := template.New("ChooseDictionaryEntry.html") t = t.Funcs(template.FuncMap{"pinyin": templatefuncs.PinyinFormatter}) t, err := t.ParseFiles("html/ChooseDictionaryEntry.html") if err != nil { fmt.Println(err.Error()) http.Error(rw, err.Error(), http.StatusInternalServerError) return } t.Execute(rw, dp) }