~mendelmaleh/dummy

499e61f9acfef8973b4c1e1be309f5d015193276 — Mendel E 1 year, 4 months ago 6cddb1e
Separate API from base package
7 files changed, 101 insertions(+), 92 deletions(-)

M .gitignore
R main.go => cmd/server/main.go
A cmd/server/options.go
M image.go
M options.go
M utils.go
M utils_test.go
M .gitignore => .gitignore +2 -1
@@ 1,2 1,3 @@
dummy
config.toml
cmd/server/server
cmd/server/config.toml

R main.go => cmd/server/main.go +7 -5
@@ 11,6 11,7 @@ import (
	"path/filepath"
	"strings"

	"git.sr.ht/~mendelmaleh/dummy"
	"git.sr.ht/~mendelmaleh/freetype"
	"git.sr.ht/~mendelmaleh/freetype/truetype"
	"github.com/pelletier/go-toml"


@@ 72,6 73,7 @@ func main() {
		log.Fatal("Default font not found")
	}

	dopt := dummy.DefaultOptions
	dopt.Font = font

	tmpl := template.New("web").Funcs(template.FuncMap{"title": strings.Title})


@@ 101,33 103,33 @@ func main() {
	log.Panic(http.ListenAndServe(config.Dummy.Addr, nil))
}

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

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

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

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

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

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

A cmd/server/options.go => cmd/server/options.go +87 -0
@@ 0,0 1,87 @@
package main

import (
	"net/url"
	"strconv"

	"git.sr.ht/~mendelmaleh/dummy"
)

// 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) {
	// image
	if v, ok := form["width"]; ok {
		i, err := strconv.Atoi(v[0])
		if err != nil {
			return opt, err
		}

		opt.Width = i
		opt.Height = i
	}

	if v, ok := form["height"]; ok {
		i, err := strconv.Atoi(v[0])
		if err != nil {
			return opt, err
		}

		opt.Height = i
	}

	if v, ok := form["mask"]; ok {
		opt.Mask = v[0]
	}

	if colors, ok := form["color"]; ok {
		for i, h := range colors {
			c, err := dummy.Hex(h)
			if err != nil {
				return opt, err
			}

			opt.Colors[i] = c
		}
	}

	// text
	if v, ok := form["text"]; ok {
		opt.Text = v[0]
	}

	if v, ok := form["font"]; ok {
		// TODO: less strict font matching
		if f, ok := fonts[v[0]]; ok {
			opt.Font = f
		}
	}

	if v, ok := form["size"]; ok {
		f, err := strconv.ParseFloat(v[0], 64)
		if err != nil {
			return opt, err
		}

		opt.Size = f
	}

	if v, ok := form["dpi"]; ok {
		f, err := strconv.ParseFloat(v[0], 64)
		if err != nil {
			return opt, err
		}

		opt.DPI = f
	}

	// web
	if v, ok := form["desc"]; ok {
		opt.Desc = v[0]
	}

	// apply limits and hacks
	opt.Normalize()

	return opt, nil
}

M image.go => image.go +1 -1
@@ 1,4 1,4 @@
package main
package dummy

import (
	"image"

M options.go => options.go +2 -83
@@ 1,9 1,7 @@
package main
package dummy

import (
	"image/color"
	"net/url"
	"strconv"

	"git.sr.ht/~mendelmaleh/freetype/truetype"
)


@@ 24,7 22,7 @@ type Options struct {
}

// to be used only with dopt.Copy(), otherwise the default colors can be changed.
var dopt = &Options{
var DefaultOptions = &Options{
	// image
	Width:  200,
	Height: 200,


@@ 42,85 40,6 @@ var dopt = &Options{
	Desc: "Dummy image generator",
}

// 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 (opt *Options) FromForm(form url.Values) error {
	// image
	if v, ok := form["width"]; ok {
		i, err := strconv.Atoi(v[0])
		if err != nil {
			return err
		}

		opt.Width = i
		opt.Height = i
	}

	if v, ok := form["height"]; ok {
		i, err := strconv.Atoi(v[0])
		if err != nil {
			return err
		}

		opt.Height = i
	}

	if v, ok := form["mask"]; ok {
		opt.Mask = v[0]
	}

	if colors, ok := form["color"]; ok {
		for i, h := range colors {
			c, err := Hex(h)
			if err != nil {
				return err
			}

			opt.Colors[i] = c
		}
	}

	// text
	if v, ok := form["text"]; ok {
		opt.Text = v[0]
	}

	if v, ok := form["font"]; ok {
		// TODO: less strict font matching
		if f, ok := fonts[v[0]]; ok {
			opt.Font = f
		}
	}

	if v, ok := form["size"]; ok {
		f, err := strconv.ParseFloat(v[0], 64)
		if err != nil {
			return err
		}

		opt.Size = f
	}

	if v, ok := form["dpi"]; ok {
		f, err := strconv.ParseFloat(v[0], 64)
		if err != nil {
			return err
		}

		opt.DPI = f
	}

	// web
	if v, ok := form["desc"]; ok {
		opt.Desc = v[0]
	}

	// apply limits and hacks
	opt.Normalize()

	return nil
}

// Copy so that the original options don't change (essentially a deep copy).
func (opt Options) Copy() (copt Options) {
	// copy all the fields

M utils.go => utils.go +1 -1
@@ 1,4 1,4 @@
package main
package dummy

import (
	"errors"

M utils_test.go => utils_test.go +1 -1
@@ 1,4 1,4 @@
package main
package dummy

import (
	"image/color"