From 57d91a19610d649e92095273724ec404ef74b30b Mon Sep 17 00:00:00 2001 From: Dmitry Pokidov Date: Mon, 25 Dec 2023 21:19:57 +1100 Subject: [PATCH] Added example and go report and package badges --- README.md | 8 ++++++++ example_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 example_test.go diff --git a/README.md b/README.md index 5c95e0c..ebff250 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ # TransformImgs +[![Go Reference](https://pkg.go.dev/badge/github.com/Pixboost/transformimgs/v8.svg)](https://pkg.go.dev/github.com/Pixboost/transformimgs/v8) +![Go Report Card](https://goreportcard.com/badge/github.com/Pixboost/transformimgs/v8) ![Build Status](https://github.com/Pixboost/transformimgs/actions/workflows/action.yml/badge.svg) [![codecov](https://codecov.io/gh/Pixboost/transformimgs/branch/main/graph/badge.svg)](https://codecov.io/gh/Pixboost/transformimgs) [![Docker Pulls](https://img.shields.io/docker/pulls/pixboost/transformimgs)](https://hub.docker.com/r/pixboost/transformimgs/) @@ -24,6 +26,7 @@ the latest image formats, such as WebP, AVIF, Jpeg XL, and network client hints. * [Docker](#docker) * [Options](#options) * [Running Locally From Source Code](#running-from-source-code) + * [Using from Go Web Application](#using-from-go-web-application) - [SaaS](#saas) - [Performance tests](#performance-tests) - [Opened tickets for images related features](#opened-tickets-for-images-related-features) @@ -120,6 +123,11 @@ $ cd transformimgs $ ./run.sh ``` +### Using from Go Web Application + +You could also easily plugin HTTP route into your existing web application +using service.GetRouter method. Here is a quick [example of how to do that](./example_test.go). + ## SaaS We run SaaS version at [pixboost.com](https://pixboost.com?source=github) with generous free tier. diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000..3f3e0c9 --- /dev/null +++ b/example_test.go @@ -0,0 +1,40 @@ +package transformimgs_test + +import ( + "fmt" + "github.com/Pixboost/transformimgs/v8/img" + "github.com/Pixboost/transformimgs/v8/img/loader" + "github.com/Pixboost/transformimgs/v8/img/processor" + "log" + "net/http" + "net/http/httptest" + "runtime" +) + +func Example() { + l := &loader.Http{} + p, err := processor.NewImageMagick("/usr/local/bin/convert", "/usr/local/bin/identify") + if err != nil { + log.Fatal(err) + } + + s, err := img.NewService(l, p, runtime.NumCPU()) + if err != nil { + log.Fatal(err) + } + + // s.GetRouter() is ready to use in your web server + server := httptest.NewServer(s.GetRouter()) + defer server.Close() + + resizeApi := fmt.Sprintf("%s/img/https://raw.githubusercontent.com/Pixboost/transformimgs/main/quickstart/site/img/parrot-lossy.jpg/resize?size=600", server.URL) + resp, err := http.Get(resizeApi) + if err != nil { + log.Fatal(err) + } + + fmt.Println(resp.Status) + + // Output: + // 200 OK +}