Esempio n. 1
0
// RequestHandle servers the purge page.
func (ph *Handler) RequestHandle(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	//!TODO authentication
	if r.Method != "POST" {
		httputils.Error(w, http.StatusMethodNotAllowed)
		return
	}

	var pr = new(purgeRequest)
	if err := json.NewDecoder(r.Body).Decode(pr); err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		ph.logger.Errorf("[%p] error on parsing request %s", ph, err)
		return
	}

	var app, ok = contexts.GetApp(ctx)
	if !ok {
		httputils.Error(w, http.StatusInternalServerError)
		ph.logger.Errorf("[%p] no app in context", ph)
		return
	}
	var res, err = ph.purgeAll(app, *pr)
	if err != nil {
		httputils.Error(w, http.StatusInternalServerError)
		// previosly logged
		return
	}
	if err := json.NewEncoder(w).Encode(res); err != nil {
		ph.logger.Errorf(
			"[%p] error while encoding response %s", ph, err)
	}
}
Esempio n. 2
0
func getUpstreamFromContext(ctx context.Context, upstream string) types.Upstream {
	app, ok := contexts.GetApp(ctx)
	if !ok {
		panic("no app in context") // seriosly ?
	}

	return app.GetUpstream(upstream)
}
// RequestHandle servers the status page.
func (ssh *ServerStatusHandler) RequestHandle(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	app, ok := contexts.GetApp(ctx)
	if !ok {
		err := "Error: could not get the App from the context!"
		if _, writeErr := w.Write([]byte(err)); writeErr != nil {
			ssh.loc.Logger.Errorf("error while writing error to client: `%s`; Original error `%s`", writeErr, err)
		} else {
			ssh.loc.Logger.Error(err)
		}
		return
	}

	cacheZones, ok := contexts.GetCacheZones(ctx)
	if !ok {
		err := "Error: could not get the cache zones from the context!"
		if _, writeErr := w.Write([]byte(err)); writeErr != nil {
			ssh.loc.Logger.Errorf("error while writing error to client: `%s`; Original error `%s`", writeErr, err)
		} else {
			ssh.loc.Logger.Error(err)
		}
		return
	}

	var stats = newStatistics(app, cacheZones)
	sort.Sort(stats.CacheZones)
	var err error
	if strings.HasSuffix(r.URL.Path, jsonSuffix) {
		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		err = json.NewEncoder(w).Encode(stats)
	} else {
		err = ssh.tmpl.Execute(w, stats)
	}

	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		if _, writeErr := w.Write([]byte(err.Error())); writeErr != nil {
			ssh.loc.Logger.Errorf("error while writing error to client: `%s`; Original error `%s`", writeErr, err)
		}
	}

	return
}