Пример #1
0
func considerTrids(q *http.Request) fttp.Responder {
	switch q.Method {
	case "GET":
		// let the range check panic indicate the problem
		trid, rx := checkTrid(q, q.URL.Path[len(tridPath):])
		if rx != nil {
			return rx
		}

		tags, err := getTags(trid)
		if err != nil {
			return fttp.ServerError(err)
		}

		jason, err := json.Marshal(tags)
		if err != nil {
			return fttp.ServerError(err)
		}

		r := &fttp.Response{}
		r.JSON()
		r.Set(jason)
		return r
	case "DELETE":
		// let the range check panic indicate the problem
		suffix := q.URL.Path[len(tridPath):]
		sep := strings.IndexByte(suffix, '/')
		if sep < 1 {
			// BUG: bad request
			return fttp.NotFound(q, errors.New("trid or tag not specified"))
		}
		trid, rx := checkTrid(q, suffix[:sep])
		if rx != nil {
			return rx
		}
		suffix = suffix[sep:]
		if !strings.HasPrefix(suffix, "/tag/") {
			// BUG: bad request
			return fttp.NotFound(q, errors.New("tag not specified"))
		}
		tag := suffix[len("/tag/"):]

		err := deleteTag(trid, tag)
		if err != nil {
			return fttp.ServerError(err)
		}

		r := &fttp.Response{}
		r.JSON()
		return r
	default:
		return fttp.BadMethod(q, "GET", "DELETE")
	}
}
Пример #2
0
func considerTags(q *http.Request) fttp.Responder {
	// let the range check panic indicate the problem
	tag := q.URL.Path[len(tagPath):]

	switch q.Method {
	case "GET":
		if len(tag) == 0 {
			tags, err := listTags()
			if err != nil {
				return fttp.ServerError(err)
			}

			jason, err := json.Marshal(tags)
			if err != nil {
				return fttp.ServerError(err)
			}

			r := &fttp.Response{}
			r.JSON()
			r.Set(jason)
			return r
		} else {
			trids, err := getTrids(tag)
			if err != nil {
				return fttp.ServerError(err)
			}

			jason, err := json.Marshal(trids)
			if err != nil {
				return fttp.ServerError(err)
			}

			r := &fttp.Response{}
			r.JSON()
			r.Set(jason)
			return r
		}
	case "POST":
		// BUG: limit size. perhaps see http.MaxBytesReader
		trid, err := ioutil.ReadAll(q.Body)
		if err != nil {
			return fttp.ServerError(err)
		}

		err = putTag(string(trid), tag)
		if err != nil {
			return fttp.ServerError(err)
		}

		// BUG: 204
		r := &fttp.Response{}
		r.JSON()
		return r
	default:
		return fttp.BadMethod(q, "GET", "POST", "DELETE")
	}
}