Skip to content

Commit

Permalink
all: reorganize files
Browse files Browse the repository at this point in the history
  • Loading branch information
changkun committed Feb 27, 2021
1 parent 88df63a commit b580c98
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 15 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

all:
GOOS=linux go build -mod=vendor ./cmd/code2img
docker build -t code2img .
docker build -t code2img -f docker/Dockerfile .
up:
docker-compose -f docker-compose.yml up -d
docker-compose -f docker/docker-compose.yml up -d
down:
docker-compose -f docker-compose.yml down
docker-compose -f docker/docker-compose.yml down
clean:
rm code2img
docker rmi -f $(shell docker images -f "dangling=true" -q) 2> /dev/null; true
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import "golang.design/x/code2img"
Just one API `code2img.Render`, to use it:

```go
b, err := code2img.Render(`import "golang.design/x/code2img"`)
b, err := code2img.Render(context.TODO(), `import "golang.design/x/code2img"`)
if err != nil {
panic(err)
}
Expand All @@ -32,7 +32,7 @@ Basic usage notes:

Demo:

![](./demo.gif)
![](./testdata/demo.gif)

### Server API

Expand Down Expand Up @@ -64,4 +64,4 @@ make up

## License

© 2020 The golang.design Initiative Authors.
© 2020-2021 The golang.design Initiative Authors.
7 changes: 2 additions & 5 deletions code2img.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"fmt"
"math"
"net/url"
"time"

"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/dom"
Expand All @@ -22,7 +21,7 @@ import (

// Render renders the given code string and returns a binary buffer
// that contains a carbon-now based image.
func Render(code string) ([]byte, error) {
func Render(ctx context.Context, code string) ([]byte, error) {
// https://carbon.now.sh/?
// bg=rgba(74%2C144%2C226%2C1)&
// t=material&
Expand Down Expand Up @@ -73,9 +72,7 @@ func Render(code string) ([]byte, error) {
codeparam := url.Values{}
codeparam.Set("code", url.PathEscape(code))

ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
ctx, cancel = chromedp.NewContext(ctx)
ctx, cancel := chromedp.NewContext(ctx)
defer cancel()

url := "https://carbon.now.sh/?" + values.Encode() + "&" + codeparam.Encode()
Expand Down
11 changes: 8 additions & 3 deletions code2img_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@ package code2img_test

import (
"bytes"
"context"
"image/png"
"math"
"os"
"testing"
"time"

"golang.design/x/code2img"
)

func TestRender(t *testing.T) {
got, err := code2img.Render(`import "golang.design/x/code2img`)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

got, err := code2img.Render(ctx, `import "golang.design/x/code2img`)
if err != nil {
t.Fatalf("render failed: %v", err)
}
Expand All @@ -27,7 +32,7 @@ func TestRender(t *testing.T) {
t.Fatalf("cannot read rendered image: %v", err)
}

want, err := os.ReadFile("testdata.png")
want, err := os.ReadFile("testdata/want.png")
if err != nil {
t.Fatalf("cannot read gold test file: %v", err)
}
Expand All @@ -40,7 +45,7 @@ func TestRender(t *testing.T) {
if math.Abs(float64(imgGot.Bounds().Dx()-imgWant.Bounds().Dx())) > 5 ||
math.Abs(float64(imgGot.Bounds().Dy()-imgWant.Bounds().Dy())) > 5 {

err := os.WriteFile("got.png", got, os.ModePerm)
err := os.WriteFile("testdata/got.png", got, os.ModePerm)
if err != nil {
t.Errorf("failed to write image: %v", err)
}
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion docker-compose.yml β†’ docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ services:
container_name: code2img
restart: always
volumes:
- ./data:/app/data
- ../data:/app/data
image: code2img
cap_add:
- SYS_PTRACE # for debugging
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes

0 comments on commit b580c98

Please sign in to comment.