Skip to content

Commit ab5ce2d

Browse files
authored
route: support Go Modules (#2)
Fixes #1
1 parent 776b2fa commit ab5ce2d

File tree

4 files changed

+51
-9
lines changed

4 files changed

+51
-9
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ docker:
1010
run:
1111
docker ps -a | grep $(NAME) && ([ $$? -eq 0 ] && (docker stop $(NAME) && docker rm -f $(NAME))) || echo "no running container."
1212
docker run -itd -v $(shell pwd)/data:/app/public/buildbox -p 6789:6789 --name $(NAME) ssaplayground:latest
13-
tidy-docker:
13+
tidy:
1414
docker ps -a | grep $(NAME)
1515
[ $$? -eq 0 ] && docker stop $(NAME) && docker rm -f $(NAME)
1616
docker images -f "dangling=true" -q | xargs docker rmi -f
1717
docker image prune -f
1818
clean:
1919
rm -rf $(BUILD)
20-
.PHONY: all start docker update clean
20+
.PHONY: all docker run tidy clean

TODO.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99
- [x] ~~Use short link instead of UUID?~~ seems no need
1010
- [x] share with /gossa?id=uuid -> access /gossa/uuid/main.go or /gossa/uuid/main_test.go, access /gossa/uuid/ssa.html
1111
- [x] ~~Better Editor: Monaco Editor~~ maybe not?
12-
- [ ] Containerized deployment
12+
- [x] Containerized deployment
13+
- [x] Support Go Modules

docker/Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
FROM golang:1.14-alpine
22
WORKDIR /app
33
ADD . /app
4+
# required for runtime/cgo
5+
RUN apk add g++
46
RUN go get -u golang.org/x/tools/cmd/goimports && go mod tidy
5-
RUN go build -o ssaplayground.service -mod vendor
7+
RUN go build -o ssaplayground.service -mod=vendor
68
EXPOSE 6789
79
ENTRYPOINT [ "/app/ssaplayground.service", "-conf=/app/configs/docker.yaml"]

src/route/api.go

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ type BuildSSAOutput struct {
4646
Msg string `json:"msg"`
4747
}
4848

49+
// BuildSSA serves the code send by user and builds its SSA IR into html.
50+
// TODO: speedup for request response, e.g. as async rest api.
4951
func BuildSSA(c *gin.Context) {
5052
// 1. create a folder in config.Get().Static/buildbox
5153
out := BuildSSAOutput{
@@ -65,7 +67,7 @@ func BuildSSA(c *gin.Context) {
6567
err = c.BindJSON(&in)
6668
if err != nil {
6769
os.Remove(path)
68-
out.Msg = fmt.Sprintf("cannot bind input params, err: %v", err)
70+
out.Msg = fmt.Sprintf("cannot bind input params, err: \n%v", err)
6971
c.JSON(http.StatusInternalServerError, out)
7072
return
7173
}
@@ -86,24 +88,35 @@ func BuildSSA(c *gin.Context) {
8688
err = ioutil.WriteFile(buildFile, []byte(in.Code), os.ModePerm)
8789
if err != nil {
8890
os.Remove(path)
89-
out.Msg = err.Error()
91+
out.Msg = fmt.Sprintf("cannot save your code, err: \n%v", err)
9092
c.JSON(http.StatusInternalServerError, out)
9193
return
9294
}
9395

94-
// 3. goimports && GOSSAFUNC=foo go build
96+
// 3.1 goimports
9597
err = autoimports(buildFile)
9698
if err != nil {
9799
os.Remove(path)
98-
out.Msg = err.Error()
100+
out.Msg = fmt.Sprintf("cannot run autoimports for your code, err: \n%v", err)
99101
c.JSON(http.StatusBadRequest, out)
100102
return
101103
}
104+
105+
// 3.2 go mod init gossa && go mod tidy
106+
err = initModules(path)
107+
if err != nil {
108+
os.Remove(path)
109+
out.Msg = fmt.Sprintf("cannot use go modules for your code, err: \n%v", err)
110+
c.JSON(http.StatusBadRequest, out)
111+
return
112+
}
113+
114+
// 3.3 GOSSAFUNC=foo go build
102115
outFile := filepath.Join(path, "/main.out")
103116
err = buildSSA(in.FuncName, in.GcFlags, outFile, buildFile, isTest)
104117
if err != nil {
105118
os.Remove(path)
106-
out.Msg = err.Error()
119+
out.Msg = fmt.Sprintf("cannot build ssa for your code, err: \n%v", err)
107120
c.JSON(http.StatusBadRequest, out)
108121
return
109122
}
@@ -138,6 +151,32 @@ func autoimports(outf string) error {
138151
return nil
139152
}
140153

154+
func initModules(path string) error {
155+
// 1. go mod init
156+
cmd := exec.Command("go", "mod", "init", "gossa")
157+
cmd.Dir = path
158+
cmd.Stderr = &bytes.Buffer{}
159+
err := cmd.Run()
160+
if err != nil {
161+
msg := cmd.Stderr.(*bytes.Buffer).String()
162+
msg = strings.ReplaceAll(msg, path, "$GOSSAPATH")
163+
return errors.New(msg)
164+
}
165+
166+
// 2. go mod tidy
167+
cmd = exec.Command("go", "mod", "tidy")
168+
cmd.Dir = path
169+
cmd.Stderr = &bytes.Buffer{}
170+
err = cmd.Run()
171+
if err != nil {
172+
msg := cmd.Stderr.(*bytes.Buffer).String()
173+
msg = strings.ReplaceAll(msg, path, "$GOSSAPATH")
174+
return errors.New(msg)
175+
}
176+
177+
return nil
178+
}
179+
141180
func buildSSA(funcname, gcflags, outf, buildf string, isTest bool) error {
142181
var cmd *exec.Cmd
143182
if !isTest {

0 commit comments

Comments
 (0)