~mendelmaleh/dummy

55d7475293eaf60ed5d381c1efe1e9af58f98b78 — Mendel E 1 year, 1 month ago a396b15
Wrap dummy.Options
2 files changed, 18 insertions(+), 14 deletions(-)

M cmd/dummyserver/main.go
M cmd/dummyserver/options.go
M cmd/dummyserver/main.go => cmd/dummyserver/main.go +7 -7
@@ 90,46 90,46 @@ func main() {

	http.HandleFunc(config.Dummy.Pattern,
		func(w http.ResponseWriter, r *http.Request) {
			Dummy(w, r, dopt.Copy())
			Dummy(w, r, Options{dopt.Copy()})
		},
	)

	http.HandleFunc(config.Dummy.Pattern+"web/",
		func(w http.ResponseWriter, r *http.Request) {
			Web(w, r, dopt.Copy(), config, tmpl)
			Web(w, r, Options{dopt.Copy()}, config, tmpl)
		},
	)

	log.Panic(http.ListenAndServe(config.Dummy.Addr, nil))
}

func Dummy(w http.ResponseWriter, r *http.Request, opt dummy.Options) {
func Dummy(w http.ResponseWriter, r *http.Request, opt Options) {
	err := r.ParseForm()
	if err != nil {
		fmt.Fprintln(w, err)
		return
	}

	opt, err = FromForm(opt, r.Form)
	err = opt.FromForm(r.Form)
	if err != nil {
		fmt.Fprintln(w, err)
		return
	}

	im := dummy.Image(opt)
	im := dummy.Image(opt.Options)
	png.Encode(w, im)

	// fmt.Fprintf(w, "%+v\n", dopt)
}

func Web(w http.ResponseWriter, r *http.Request, opt dummy.Options, config Config, tmpl *template.Template) {
func Web(w http.ResponseWriter, r *http.Request, opt Options, config Config, tmpl *template.Template) {
	err := r.ParseForm()
	if err != nil {
		fmt.Fprintln(w, err)
		return
	}

	opt, err = FromForm(opt, r.Form)
	err = opt.FromForm(r.Form)
	if err != nil {
		fmt.Fprintln(w, err)
		return

M cmd/dummyserver/options.go => cmd/dummyserver/options.go +11 -7
@@ 7,14 7,18 @@ import (
	"git.sr.ht/~mendelmaleh/dummy"
)

type Options struct {
	dummy.Options
}

// FromForm gets values from the form (which includes the url encoded query)
// and applies them to our Options. To ne used with r.Form after r.ParseForm().
func FromForm(opt dummy.Options, form url.Values) (dummy.Options, error) {
func (opt *Options) FromForm(form url.Values) error {
	// image
	if v, ok := form["width"]; ok {
		i, err := strconv.Atoi(v[0])
		if err != nil {
			return opt, err
			return err
		}

		opt.Width = i


@@ 24,7 28,7 @@ func FromForm(opt dummy.Options, form url.Values) (dummy.Options, error) {
	if v, ok := form["height"]; ok {
		i, err := strconv.Atoi(v[0])
		if err != nil {
			return opt, err
			return err
		}

		opt.Height = i


@@ 38,7 42,7 @@ func FromForm(opt dummy.Options, form url.Values) (dummy.Options, error) {
		for i, h := range colors {
			c, err := dummy.Hex(h)
			if err != nil {
				return opt, err
				return err
			}

			opt.Colors[i] = c


@@ 60,7 64,7 @@ func FromForm(opt dummy.Options, form url.Values) (dummy.Options, error) {
	if v, ok := form["size"]; ok {
		f, err := strconv.ParseFloat(v[0], 64)
		if err != nil {
			return opt, err
			return err
		}

		opt.Size = f


@@ 69,7 73,7 @@ func FromForm(opt dummy.Options, form url.Values) (dummy.Options, error) {
	if v, ok := form["dpi"]; ok {
		f, err := strconv.ParseFloat(v[0], 64)
		if err != nil {
			return opt, err
			return err
		}

		opt.DPI = f


@@ 83,5 87,5 @@ func FromForm(opt dummy.Options, form url.Values) (dummy.Options, error) {
	// apply limits and hacks
	opt.Normalize()

	return opt, nil
	return nil
}