~mendelmaleh/dummy

83481f7c7d73a7fa9c54950c707f2aa9c7797d14 — Mendel E 3 months ago f7699b2
Move handlers to their own file
2 files changed, 63 insertions(+), 54 deletions(-)

A cmd/dummyserver/handlers.go
M cmd/dummyserver/main.go
A cmd/dummyserver/handlers.go => cmd/dummyserver/handlers.go +63 -0
@@ 0,0 1,63 @@
package main

import (
	"fmt"
	"html/template"
	"image/png"
	"net/http"
	"net/url"
	"strings"

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

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

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

	im := dummy.Image(opt.Options)
	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) {
	err := r.ParseForm()
	if err != nil {
		fmt.Fprintln(w, err)
		return
	}

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

	u := url.URL{
		Host:     r.Host,
		RawQuery: r.URL.RawQuery,
		Path:     config.Dummy.Pattern,
	}

	if v, ok := r.Header["X-Forwarded-Proto"]; ok {
		u.Scheme = strings.Join(v, "")
	} else {
		u.Scheme = "http"
	}

	tmpl.Execute(w, struct {
		Name, Desc string
		Image      template.URL
	}{"Dummy", opt.Desc, template.URL(u.String())})

}

M cmd/dummyserver/main.go => cmd/dummyserver/main.go +0 -54
@@ 3,14 3,11 @@ package main
import (
	"encoding/gob"
	"errors"
	"fmt"
	"html/template"
	"image/png"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
	"strings"


@@ 198,54 195,3 @@ func main() {
	listener = netutil.LimitListener(listener, config.Dummy.Conns)
	log.Panic(http.Serve(listener, nil))
}

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

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

	im := dummy.Image(opt.Options)
	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) {
	err := r.ParseForm()
	if err != nil {
		fmt.Fprintln(w, err)
		return
	}

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

	u := url.URL{
		Host:     r.Host,
		RawQuery: r.URL.RawQuery,
		Path:     config.Dummy.Pattern,
	}

	if v, ok := r.Header["X-Forwarded-Proto"]; ok {
		u.Scheme = strings.Join(v, "")
	} else {
		u.Scheme = "http"
	}

	tmpl.Execute(w, struct {
		Name, Desc string
		Image      template.URL
	}{"Dummy", opt.Desc, template.URL(u.String())})

}