Skip to content

Commit d85adb4

Browse files
committed
all: add cmd/code2img
1 parent b580c98 commit d85adb4

File tree

9 files changed

+105
-11
lines changed

9 files changed

+105
-11
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,4 @@
1515
# vendor/
1616
data/code/*.go
1717
data/images/*.png
18-
code2img
19-
.idea
18+

cmd/code2img/main.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2021 The golang.design Initiative authors.
2+
// All rights reserved. Use of this source code is governed
3+
// by a GNU GPL-3.0 license that can be found in the LICENSE file.
4+
//
5+
// Written by Changkun Ou <changkun.de>
6+
7+
package main
8+
9+
import (
10+
"context"
11+
"fmt"
12+
"io/ioutil"
13+
"log"
14+
"net/http"
15+
"os"
16+
"os/signal"
17+
"syscall"
18+
"time"
19+
20+
"github.com/gin-gonic/gin"
21+
"github.com/google/uuid"
22+
"golang.design/x/code2img"
23+
)
24+
25+
func main() {
26+
router := gin.Default()
27+
router.Static("/api/v1/code2img/data/code", "./data/code")
28+
router.Static("/api/v1/code2img/data/images", "./data/images")
29+
router.POST("/api/v1/code2img", func(c *gin.Context) {
30+
b := struct {
31+
Code string `json:"code"`
32+
}{}
33+
if err := c.ShouldBindJSON(&b); err != nil {
34+
c.String(http.StatusBadRequest, fmt.Sprintf("Error: %s", err))
35+
return
36+
}
37+
id := uuid.New().String()
38+
gofile := "./data/code/" + id + ".go"
39+
40+
err := ioutil.WriteFile(gofile, []byte(b.Code), os.ModePerm)
41+
if err != nil {
42+
log.Printf("[%s]: write file error %v", gofile, err)
43+
c.String(http.StatusBadRequest, fmt.Sprintf("Error: %s", err))
44+
return
45+
}
46+
47+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
48+
defer cancel()
49+
50+
buf, err := code2img.Render(ctx, b.Code)
51+
if err != nil {
52+
c.String(http.StatusBadRequest, fmt.Sprintf("Error: %s", err))
53+
return
54+
}
55+
imgfile := "./data/images/" + id + ".png"
56+
if err := ioutil.WriteFile(imgfile, buf, os.ModePerm); err != nil {
57+
log.Printf("[%s]: write screenshot error %v", imgfile, err)
58+
return
59+
}
60+
c.String(http.StatusOK, "https://golang.design/api/v1/code2img/data/images/"+id+".png")
61+
})
62+
63+
s := &http.Server{Addr: ":8080", Handler: router}
64+
go func() {
65+
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
66+
log.Fatalf("listen: %s\n", err)
67+
}
68+
}()
69+
70+
quit := make(chan os.Signal)
71+
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
72+
<-quit
73+
log.Println("shutting down...")
74+
75+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
76+
defer cancel()
77+
if err := s.Shutdown(ctx); err != nil {
78+
log.Fatal("forced to shutdown: ", err)
79+
}
80+
log.Println("server exiting, good bye!")
81+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ require (
66
github.com/chromedp/cdproto v0.0.0-20200116234248-4da64dd111ac
77
github.com/chromedp/chromedp v0.5.3
88
github.com/gin-gonic/gin v1.6.3
9-
github.com/google/uuid v1.1.2
9+
github.com/google/uuid v1.2.0
1010
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/E
2626
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
2727
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
2828
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
29-
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
30-
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
29+
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
30+
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
3131
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
3232
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
3333
github.com/knq/sysutil v0.0.0-20191005231841-15668db23d08 h1:V0an7KRw92wmJysvFvtqtKMAPmvS5O0jtB0nYo6t+gs=

vendor/github.com/google/uuid/hash.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/google/uuid/sql.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/google/uuid/uuid.go

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/google/uuid/version4.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ github.com/gobwas/ws
7878
github.com/gobwas/ws/wsutil
7979
# github.com/golang/protobuf v1.3.3
8080
github.com/golang/protobuf/proto
81-
# github.com/google/uuid v1.1.2
81+
# github.com/google/uuid v1.2.0
8282
## explicit
8383
github.com/google/uuid
8484
# github.com/json-iterator/go v1.1.9

0 commit comments

Comments
 (0)