Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 54 additions & 49 deletions controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"mime"
"path"
"net/http"
"strconv"
"strings"
Expand All @@ -12,19 +13,21 @@ import (
"github.com/h2non/filetype"
)

func indexController(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
ErrorReply(r, w, ErrNotFound, ServerOptions{})
return
}
func indexController(o ServerOptions) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != path.Join(o.PathPrefix, "/") {
ErrorReply(r, w, ErrNotFound, ServerOptions{})
return
}

body, _ := json.Marshal(Versions{
Version,
bimg.Version,
bimg.VipsVersion,
})
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(body)
body, _ := json.Marshal(Versions{
Version,
bimg.Version,
bimg.VipsVersion,
})
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(body)
}
}

func healthController(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -136,45 +139,47 @@ func imageHandler(w http.ResponseWriter, r *http.Request, buf []byte, operation
_, _ = w.Write(image.Body)
}

func formController(w http.ResponseWriter, r *http.Request) {
operations := []struct {
name string
method string
args string
}{
{"Resize", "resize", "width=300&height=200&type=jpeg"},
{"Force resize", "resize", "width=300&height=200&force=true"},
{"Crop", "crop", "width=300&quality=95"},
{"SmartCrop", "crop", "width=300&height=260&quality=95&gravity=smart"},
{"Extract", "extract", "top=100&left=100&areawidth=300&areaheight=150"},
{"Enlarge", "enlarge", "width=1440&height=900&quality=95"},
{"Rotate", "rotate", "rotate=180"},
{"AutoRotate", "autorotate", "quality=90"},
{"Flip", "flip", ""},
{"Flop", "flop", ""},
{"Thumbnail", "thumbnail", "width=100"},
{"Zoom", "zoom", "factor=2&areawidth=300&top=80&left=80"},
{"Color space (black&white)", "resize", "width=400&height=300&colorspace=bw"},
{"Add watermark", "watermark", "textwidth=100&text=Hello&font=sans%2012&opacity=0.5&color=255,200,50"},
{"Convert format", "convert", "type=png"},
{"Image metadata", "info", ""},
{"Gaussian blur", "blur", "sigma=15.0&minampl=0.2"},
{"Pipeline (image reduction via multiple transformations)", "pipeline", "operations=%5B%7B%22operation%22:%20%22crop%22,%20%22params%22:%20%7B%22width%22:%20300,%20%22height%22:%20260%7D%7D,%20%7B%22operation%22:%20%22convert%22,%20%22params%22:%20%7B%22type%22:%20%22webp%22%7D%7D%5D"},
}
func formController(o ServerOptions) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
operations := []struct {
name string
method string
args string
}{
{"Resize", "resize", "width=300&height=200&type=jpeg"},
{"Force resize", "resize", "width=300&height=200&force=true"},
{"Crop", "crop", "width=300&quality=95"},
{"SmartCrop", "crop", "width=300&height=260&quality=95&gravity=smart"},
{"Extract", "extract", "top=100&left=100&areawidth=300&areaheight=150"},
{"Enlarge", "enlarge", "width=1440&height=900&quality=95"},
{"Rotate", "rotate", "rotate=180"},
{"AutoRotate", "autorotate", "quality=90"},
{"Flip", "flip", ""},
{"Flop", "flop", ""},
{"Thumbnail", "thumbnail", "width=100"},
{"Zoom", "zoom", "factor=2&areawidth=300&top=80&left=80"},
{"Color space (black&white)", "resize", "width=400&height=300&colorspace=bw"},
{"Add watermark", "watermark", "textwidth=100&text=Hello&font=sans%2012&opacity=0.5&color=255,200,50"},
{"Convert format", "convert", "type=png"},
{"Image metadata", "info", ""},
{"Gaussian blur", "blur", "sigma=15.0&minampl=0.2"},
{"Pipeline (image reduction via multiple transformations)", "pipeline", "operations=%5B%7B%22operation%22:%20%22crop%22,%20%22params%22:%20%7B%22width%22:%20300,%20%22height%22:%20260%7D%7D,%20%7B%22operation%22:%20%22convert%22,%20%22params%22:%20%7B%22type%22:%20%22webp%22%7D%7D%5D"},
}

html := "<html><body>"
html := "<html><body>"

for _, form := range operations {
html += fmt.Sprintf(`
<h1>%s</h1>
<form method="POST" action="/%s?%s" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="Upload" />
</form>`, form.name, form.method, form.args)
}
for _, form := range operations {
html += fmt.Sprintf(`
<h1>%s</h1>
<form method="POST" action="%s?%s" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="Upload" />
</form>`, path.Join(o.PathPrefix, form.name), path.Join(o.PathPrefix, form.method), form.args)
}

html += "</body></html>"
html += "</body></html>"

w.Header().Set("Content-Type", "text/html")
_, _ = w.Write([]byte(html))
w.Header().Set("Content-Type", "text/html")
_, _ = w.Write([]byte(html))
}
}
4 changes: 2 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ func join(o ServerOptions, route string) string {
func NewServerMux(o ServerOptions) http.Handler {
mux := http.NewServeMux()

mux.Handle(join(o, "/"), Middleware(indexController, o))
mux.Handle(join(o, "/form"), Middleware(formController, o))
mux.Handle(join(o, "/"), Middleware(indexController(o), o))
mux.Handle(join(o, "/form"), Middleware(formController(o), o))
mux.Handle(join(o, "/health"), Middleware(healthController, o))

image := ImageMiddleware(o)
Expand Down
3 changes: 2 additions & 1 deletion server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
)

func TestIndex(t *testing.T) {
ts := testServer(indexController)
opts := ServerOptions{PathPrefix: "/"}
ts := testServer(indexController(opts))
defer ts.Close()

res, err := http.Get(ts.URL)
Expand Down