Skip to content

Commit

Permalink
all: add cmd/code2img
Browse files Browse the repository at this point in the history
  • Loading branch information
changkun committed Feb 27, 2021
1 parent b580c98 commit d85adb4
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 11 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@
# vendor/
data/code/*.go
data/images/*.png
code2img
.idea

81 changes: 81 additions & 0 deletions cmd/code2img/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2021 The golang.design Initiative authors.
// All rights reserved. Use of this source code is governed
// by a GNU GPL-3.0 license that can be found in the LICENSE file.
//
// Written by Changkun Ou <changkun.de>

package main

import (
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
"golang.design/x/code2img"
)

func main() {
router := gin.Default()
router.Static("/api/v1/code2img/data/code", "./data/code")
router.Static("/api/v1/code2img/data/images", "./data/images")
router.POST("/api/v1/code2img", func(c *gin.Context) {
b := struct {
Code string `json:"code"`
}{}
if err := c.ShouldBindJSON(&b); err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("Error: %s", err))
return
}
id := uuid.New().String()
gofile := "./data/code/" + id + ".go"

err := ioutil.WriteFile(gofile, []byte(b.Code), os.ModePerm)
if err != nil {
log.Printf("[%s]: write file error %v", gofile, err)
c.String(http.StatusBadRequest, fmt.Sprintf("Error: %s", err))
return
}

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

buf, err := code2img.Render(ctx, b.Code)
if err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("Error: %s", err))
return
}
imgfile := "./data/images/" + id + ".png"
if err := ioutil.WriteFile(imgfile, buf, os.ModePerm); err != nil {
log.Printf("[%s]: write screenshot error %v", imgfile, err)
return
}
c.String(http.StatusOK, "https://golang.design/api/v1/code2img/data/images/"+id+".png")
})

s := &http.Server{Addr: ":8080", Handler: router}
go func() {
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()

quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("shutting down...")

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := s.Shutdown(ctx); err != nil {
log.Fatal("forced to shutdown: ", err)
}
log.Println("server exiting, good bye!")
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ require (
github.com/chromedp/cdproto v0.0.0-20200116234248-4da64dd111ac
github.com/chromedp/chromedp v0.5.3
github.com/gin-gonic/gin v1.6.3
github.com/google/uuid v1.1.2
github.com/google/uuid v1.2.0
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/E
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/knq/sysutil v0.0.0-20191005231841-15668db23d08 h1:V0an7KRw92wmJysvFvtqtKMAPmvS5O0jtB0nYo6t+gs=
Expand Down
4 changes: 2 additions & 2 deletions vendor/github.com/google/uuid/hash.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/google/uuid/sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions vendor/github.com/google/uuid/uuid.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions vendor/github.com/google/uuid/version4.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ github.com/gobwas/ws
github.com/gobwas/ws/wsutil
# github.com/golang/protobuf v1.3.3
github.com/golang/protobuf/proto
# github.com/google/uuid v1.1.2
# github.com/google/uuid v1.2.0
## explicit
github.com/google/uuid
# github.com/json-iterator/go v1.1.9
Expand Down

0 comments on commit d85adb4

Please sign in to comment.