Skip to content

Commit 566fbf9

Browse files
committed
all: export Render API
1 parent a868885 commit 566fbf9

File tree

4 files changed

+38
-78
lines changed

4 files changed

+38
-78
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
# by a GNU GPL-3.0 license that can be found in the LICENSE file.
44

55
all:
6-
GOOS=linux go build -mod=vendor
6+
GOOS=linux go build -mod=vendor ./cmd/code2img
77
docker build -t code2img .
8-
up: down
8+
up:
99
docker-compose -f docker-compose.yml up -d
1010
down:
1111
docker-compose -f docker-compose.yml down

README.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
1-
# code2img
1+
# code2img [![PkgGoDev](https://pkg.go.dev/badge/golang.design/x/code2img)](https://pkg.go.dev/golang.design/x/code2img) ![](https://changkun.de/urlstat?mode=github&repo=golang-design/code2img)
22

33
a carbon service wrapper
44

5-
## iOS Shortcut
5+
```go
6+
import "golang.design/x/code2img"
7+
```
8+
9+
## API Usage
10+
11+
Just one API `code2img.Render`, to use it:
12+
13+
```go
14+
b, err := code2img.Render(`import "golang.design/x/code2img"`)
15+
if err != nil {
16+
panic(err)
17+
}
18+
19+
os.WriteFile("code.png", b, os.ModePerm)
20+
```
21+
22+
## Service Usage
23+
24+
### iOS Shortcut
625

726
Basic usage notes:
827

@@ -15,7 +34,7 @@ Demo:
1534

1635
![](./demo.gif)
1736

18-
## API
37+
### Server API
1938

2039
```
2140
POST golang.design/api/v1/code2img
@@ -36,6 +55,13 @@ You can also access the code text:
3655
https://golang.design/api/v1/code2img/data/code/06ad29c5-2989-4a8e-8cd2-1ce63e36167b.go
3756
```
3857

58+
### Deploy Instructions
59+
60+
```sh
61+
make
62+
make up
63+
```
64+
3965
## License
4066

4167
© 2020 The golang.design Initiative Authors.

main.go renamed to code2img.go

Lines changed: 6 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,86 +2,25 @@
22
// All rights reserved. Use of this source code is governed
33
// by a GNU GPL-3.0 license that can be found in the LICENSE file.
44

5-
package main
5+
package code2img
66

77
import (
88
"context"
99
"fmt"
10-
"io/ioutil"
1110
"math"
12-
"net/http"
1311
"net/url"
14-
"os"
15-
"os/signal"
16-
"syscall"
1712
"time"
1813

1914
"github.com/chromedp/cdproto/cdp"
2015
"github.com/chromedp/cdproto/dom"
2116
"github.com/chromedp/cdproto/emulation"
2217
"github.com/chromedp/cdproto/page"
2318
"github.com/chromedp/chromedp"
24-
"github.com/gin-gonic/gin"
25-
"github.com/google/uuid"
26-
"github.com/sirupsen/logrus"
2719
)
2820

29-
func main() {
30-
router := gin.Default()
31-
router.Static("/api/v1/code2img/data/code", "./data/code")
32-
router.Static("/api/v1/code2img/data/images", "./data/images")
33-
router.POST("/api/v1/code2img", code2img)
34-
s := &http.Server{Addr: ":8080", Handler: router}
35-
36-
go func() {
37-
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
38-
logrus.Fatalf("listen: %s\n", err)
39-
}
40-
}()
41-
42-
quit := make(chan os.Signal)
43-
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
44-
<-quit
45-
logrus.Println("shutting down...")
46-
47-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
48-
defer cancel()
49-
if err := s.Shutdown(ctx); err != nil {
50-
logrus.Fatal("forced to shutdown: ", err)
51-
}
52-
logrus.Println("server exiting, good bye!")
53-
}
54-
55-
type form struct {
56-
Code string `json:"code"`
57-
}
58-
59-
func code2img(c *gin.Context) {
60-
b := form{}
61-
if err := c.ShouldBindJSON(&b); err != nil {
62-
c.String(http.StatusBadRequest, fmt.Sprintf("Error: %s", err))
63-
return
64-
}
65-
id := uuid.New().String()
66-
gofile := "./data/code/" + id + ".go"
67-
68-
err := ioutil.WriteFile(gofile, []byte(b.Code), os.ModePerm)
69-
if err != nil {
70-
logrus.Errorf("[%s]: write file error %v", gofile, err)
71-
c.String(http.StatusBadRequest, fmt.Sprintf("Error: %s", err))
72-
return
73-
}
74-
75-
err = render("./data/images/"+id+".png", b.Code)
76-
if err != nil {
77-
c.String(http.StatusBadRequest, fmt.Sprintf("Error: %s", err))
78-
return
79-
}
80-
81-
c.String(http.StatusOK, "https://golang.design/api/v1/code2img/data/images/"+id+".png")
82-
}
83-
84-
func render(imgfile, code string) error {
21+
// Render renders the given code string and returns a binary buffer
22+
// that contains a carbon-now based image.
23+
func Render(code string) ([]byte, error) {
8524
// https://carbon.now.sh/?
8625
// bg=rgba(74%2C144%2C226%2C1)&
8726
// t=material&
@@ -147,15 +86,9 @@ func render(imgfile, code string) error {
14786
screenshot(sel, &picbuf, chromedp.NodeReady, chromedp.ByID),
14887
})
14988
if err != nil {
150-
return fmt.Errorf("render task error: %w", err)
151-
}
152-
153-
err = ioutil.WriteFile(imgfile, picbuf, os.ModePerm)
154-
if err != nil {
155-
logrus.Errorf("[%s]: write screenshot error %v", imgfile, err)
156-
return err
89+
return nil, fmt.Errorf("code2img: render task failed: %w", err)
15790
}
158-
return nil
91+
return picbuf, nil
15992
}
16093

16194
func screenshot(sel interface{}, picbuf *[]byte, opts ...chromedp.QueryOption) chromedp.QueryAction {

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
5151
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
5252
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
5353
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
54+
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
5455
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
5556
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
5657
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=

0 commit comments

Comments
 (0)