Пример #1
0
Файл: core.go Проект: z0rr0/luss
// HandlerIndex returns index web page.
func HandlerIndex(ctx context.Context, w http.ResponseWriter, r *http.Request) ErrHandler {
	data := map[string]string{}
	c, err := conf.FromContext(ctx)
	if err != nil {
		return ErrHandler{err, http.StatusInternalServerError}
	}
	tpl, err := c.CacheTpl("base", "base.html", "index.html")
	if err != nil {
		return ErrHandler{err, http.StatusInternalServerError}
	}
	if r.Method == "POST" {
		p, err := validateParams(r)
		if err != nil {
			c.L.Error.Println(err)
			data["Error"] = "Invalid data."
			err = tpl.ExecuteTemplate(w, "base", data)
			if err != nil {
				return ErrHandler{err, http.StatusInternalServerError}
			}
			return ErrHandler{nil, http.StatusOK}
		}
		params := []*trim.ReqParams{p}
		cus, err := trim.Shorten(ctx, params)
		if err != nil {
			return ErrHandler{err, http.StatusInternalServerError}
		}
		data["Result"] = c.Address(cus[0].String())
	}
	err = tpl.ExecuteTemplate(w, "base", data)
	if err != nil {
		return ErrHandler{err, http.StatusInternalServerError}
	}
	return ErrHandler{nil, http.StatusOK}
}
Пример #2
0
Файл: core.go Проект: z0rr0/luss
// HandlerNoWebIndex works like version but return only short link text.
func HandlerNoWebIndex(ctx context.Context, w http.ResponseWriter, r *http.Request) ErrHandler {
	c, err := conf.FromContext(ctx)
	if err != nil {
		fmt.Fprintf(w, "error: internal error\n")
		return ErrHandler{nil, http.StatusOK}
	}
	u := r.FormValue("u")
	if u == "" {
		fmt.Fprintf(w, "error: no data\n")
		return ErrHandler{nil, http.StatusOK}
	}
	param := &trim.ReqParams{
		Original:  u,
		Tag:       "",
		NotDirect: false,
		TTL:       nil,
	}
	err = param.Valid()
	if err != nil {
		fmt.Fprintf(w, "error: invalid url\n")
		return ErrHandler{nil, http.StatusOK}
	}
	params := []*trim.ReqParams{param}
	cus, err := trim.Shorten(ctx, params)
	if err != nil {
		fmt.Fprintf(w, "error: internal error\n")
		return ErrHandler{nil, http.StatusOK}
	}
	fmt.Fprintf(w, "%s\n", c.Address(cus[0].String()))
	return ErrHandler{nil, http.StatusOK}
}
Пример #3
0
Файл: api.go Проект: z0rr0/luss
// HandlerAdd creates new short URL.
func HandlerAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) core.ErrHandler {
	c, err := conf.FromContext(ctx)
	if err != nil {
		return core.ErrHandler{Err: err, Status: http.StatusInternalServerError}
	}
	defer r.Body.Close()
	params, err := validateAddParams(r)
	if err != nil {
		return core.ErrHandler{Err: err, Status: http.StatusBadRequest}
	}
	cus, err := trim.Shorten(ctx, params)
	if err != nil {
		return core.ErrHandler{Err: err, Status: http.StatusInternalServerError}
	}
	items := make([]addResponseItem, len(cus))
	for i, cu := range cus {
		id := cu.String()
		items[i] = addResponseItem{
			ID:       id,
			Short:    c.Address(id),
			Original: cu.Original,
		}
	}
	result := &addResponse{
		Err:    0,
		Msg:    "ok",
		Result: items,
	}
	b, err := json.Marshal(result)
	if err != nil {
		return core.ErrHandler{Err: err, Status: http.StatusInternalServerError}
	}
	w.Header().Set("Content-Type", "application/json")
	fmt.Fprintf(w, "%s", b)
	return core.ErrHandler{Err: nil, Status: http.StatusOK}
}