Skip to content

Commit b580c98

Browse files
committed
all: reorganize files
1 parent 88df63a commit b580c98

File tree

9 files changed

+17
-15
lines changed

9 files changed

+17
-15
lines changed

β€ŽMakefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
all:
66
GOOS=linux go build -mod=vendor ./cmd/code2img
7-
docker build -t code2img .
7+
docker build -t code2img -f docker/Dockerfile .
88
up:
9-
docker-compose -f docker-compose.yml up -d
9+
docker-compose -f docker/docker-compose.yml up -d
1010
down:
11-
docker-compose -f docker-compose.yml down
11+
docker-compose -f docker/docker-compose.yml down
1212
clean:
1313
rm code2img
1414
docker rmi -f $(shell docker images -f "dangling=true" -q) 2> /dev/null; true

β€ŽREADME.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import "golang.design/x/code2img"
1111
Just one API `code2img.Render`, to use it:
1212

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

3333
Demo:
3434

35-
![](./demo.gif)
35+
![](./testdata/demo.gif)
3636

3737
### Server API
3838

@@ -64,4 +64,4 @@ make up
6464

6565
## License
6666

67-
© 2020 The golang.design Initiative Authors.
67+
© 2020-2021 The golang.design Initiative Authors.

β€Žcode2img.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"fmt"
1212
"math"
1313
"net/url"
14-
"time"
1514

1615
"github.com/chromedp/cdproto/cdp"
1716
"github.com/chromedp/cdproto/dom"
@@ -22,7 +21,7 @@ import (
2221

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

76-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
77-
defer cancel()
78-
ctx, cancel = chromedp.NewContext(ctx)
75+
ctx, cancel := chromedp.NewContext(ctx)
7976
defer cancel()
8077

8178
url := "https://carbon.now.sh/?" + values.Encode() + "&" + codeparam.Encode()

β€Žcode2img_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,21 @@ package code2img_test
88

99
import (
1010
"bytes"
11+
"context"
1112
"image/png"
1213
"math"
1314
"os"
1415
"testing"
16+
"time"
1517

1618
"golang.design/x/code2img"
1719
)
1820

1921
func TestRender(t *testing.T) {
20-
got, err := code2img.Render(`import "golang.design/x/code2img`)
22+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
23+
defer cancel()
24+
25+
got, err := code2img.Render(ctx, `import "golang.design/x/code2img`)
2126
if err != nil {
2227
t.Fatalf("render failed: %v", err)
2328
}
@@ -27,7 +32,7 @@ func TestRender(t *testing.T) {
2732
t.Fatalf("cannot read rendered image: %v", err)
2833
}
2934

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

43-
err := os.WriteFile("got.png", got, os.ModePerm)
48+
err := os.WriteFile("testdata/got.png", got, os.ModePerm)
4449
if err != nil {
4550
t.Errorf("failed to write image: %v", err)
4651
}
File renamed without changes.

β€Ždocker-compose.yml renamed to β€Ždocker/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ services:
99
container_name: code2img
1010
restart: always
1111
volumes:
12-
- ./data:/app/data
12+
- ../data:/app/data
1313
image: code2img
1414
cap_add:
1515
- SYS_PTRACE # for debugging
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
Β (0)