Example #1
0
func adminGet(ctx *context.Context, w http.ResponseWriter, r *http.Request) {
	p := parseName("/admin/", r.URL.Path)

	if p == "" {
		writeJSONOk(w)
		return
	}

	if p == "dumps" {
		if golinks, err := ctx.GetAll(); err != nil {
			writeJSONBackendError(w, err)
			return
		} else {
			writeJSON(w, golinks, http.StatusOK)
		}
	}

}
Example #2
0
File: web.go Project: kellegous/go
// Handle a GET request to the API.
func apiGet(ctx *context.Context, w http.ResponseWriter, r *http.Request) {
	p := parseName("/api/url/", r.URL.Path)

	if p == "" {
		writeJSONOk(w)
		return
	}

	rt, err := ctx.Get(p)
	if err == leveldb.ErrNotFound {
		writeJSONOk(w)
		return
	} else if err != nil {
		writeJSONBackendError(w, err)
		return
	}

	writeJSONRoute(w, p, rt)
}
Example #3
0
File: web.go Project: kellegous/go
// Handle a POST request to the API.
func apiPost(ctx *context.Context, w http.ResponseWriter, r *http.Request) {
	p := parseName("/api/url/", r.URL.Path)

	var req struct {
		URL string `json:"url"`
	}

	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		writeJSONError(w, "invalid json")
		return
	}

	// Handle delete requests
	if req.URL == "" {
		if p == "" {
			writeJSONError(w, "url required")
			return
		}

		if err := ctx.Del(p); err != nil {
			writeJSONBackendError(w, err)
			return
		}

		writeJSONOk(w)
		return
	}

	if err := validateURL(r, req.URL); err != nil {
		writeJSONError(w, err.Error())
		return
	}

	// If no name is specified, an ID must be generate.
	if p == "" {
		id, err := ctx.NextID()
		if err != nil {
			writeJSONBackendError(w, err)
			return
		}
		p = encodeID(id)
	}

	rt := context.Route{
		URL:  req.URL,
		Time: time.Now(),
	}

	if err := ctx.Put(p, &rt); err != nil {
		writeJSONBackendError(w, err)
		return
	}

	writeJSONRoute(w, p, &rt)
}