Пример #1
0
// API: POST /braille
func brailleHandler(w http.ResponseWriter, r *http.Request) {
	src := r.FormValue("input")
	lang := r.FormValue("lang")
	if lang == "" {
		// TODO: lang이 없거나 auto이면 언어 판단해야함
		lang = "ko"
	}
	format := r.FormValue("format")
	if format == "" {
		format = "svg"
	}

	w.Header().Set("Content-Type", "text/html; charset=utf-8")

	var bStr string

	if lang == "ko" {
		bStr, _ = brl_ko.Encode(src)
	} else if lang == "en" {
		bStr, _ = brl_en.Encode(src)
	}

	if format == "svg" {
		canvas := svg.New(w)
		defer canvas.End()
		brl_svg.DrawPage30(canvas, bStr)
	} else {
		fmt.Fprint(w, bStr)
	}
}
Пример #2
0
// API: POST /printq/add
//   input: text to translation
//   lang: auto|ko|en
//   key: examplekey (TODO: OAuth implementation)
func printqAddHandler(w http.ResponseWriter, r *http.Request) {
	if strings.ToUpper(r.Method) != "POST" {
		http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
		return
	}

	/*
		var authKey string
		if strings.Contains(r.Referer(), "http://localhost") ||
			strings.Contains(r.Referer(), "http://braille-printer.appspot.com") {
			authKey = EXAMPLE_AUTHKEY
		} else {
			http.Error(w, "Unauthorized", http.StatusUnauthorized)
			return
		}
	*/

	authKey := r.FormValue("key")
	if authKey == "" {
		authKey = EXAMPLE_AUTHKEY
	}
	input := r.FormValue("input")
	lang := r.FormValue("lang")
	if lang == "" {
		// TODO: lang이 없거나 auto이면 언어 판단해야함
		lang = "ko"
	}

	label := "label"

	if strings.Contains(input, "\n") {
		label = "paper"
	}

	var bStr string

	if lang == "ko" {
		bStr, _ = brl_ko.Encode(input)
	} else if lang == "en" {
		bStr, _ = brl_en.Encode(input)
	}

	buf := bytes.NewBuffer(make([]byte, 24288))

	canvas := svg.New(buf)
	defer canvas.End()
	brl_svg.DrawPage30(canvas, bStr)

	printq := PrintQ{
		Type:       label,
		Key:        authKey,
		Origin:     input,
		ResultText: bStr,
		ResultSVG:  buf.Bytes(),
		Status:     0,
		CTime:      time.Now(),
	}

	c := appengine.NewContext(r)
	_, err := datastore.Put(c, datastore.NewIncompleteKey(c, "PrintQ", nil), &printq)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}