Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

route: support Go Modules #2

Merged
merged 1 commit into from
Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ docker:
run:
docker ps -a | grep $(NAME) && ([ $$? -eq 0 ] && (docker stop $(NAME) && docker rm -f $(NAME))) || echo "no running container."
docker run -itd -v $(shell pwd)/data:/app/public/buildbox -p 6789:6789 --name $(NAME) ssaplayground:latest
tidy-docker:
tidy:
docker ps -a | grep $(NAME)
[ $$? -eq 0 ] && docker stop $(NAME) && docker rm -f $(NAME)
docker images -f "dangling=true" -q | xargs docker rmi -f
docker image prune -f
clean:
rm -rf $(BUILD)
.PHONY: all start docker update clean
.PHONY: all docker run tidy clean
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
- [x] ~~Use short link instead of UUID?~~ seems no need
- [x] share with /gossa?id=uuid -> access /gossa/uuid/main.go or /gossa/uuid/main_test.go, access /gossa/uuid/ssa.html
- [x] ~~Better Editor: Monaco Editor~~ maybe not?
- [ ] Containerized deployment
- [x] Containerized deployment
- [x] Support Go Modules
4 changes: 3 additions & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
FROM golang:1.14-alpine
WORKDIR /app
ADD . /app
# required for runtime/cgo
RUN apk add g++
RUN go get -u golang.org/x/tools/cmd/goimports && go mod tidy
RUN go build -o ssaplayground.service -mod vendor
RUN go build -o ssaplayground.service -mod=vendor
EXPOSE 6789
ENTRYPOINT [ "/app/ssaplayground.service", "-conf=/app/configs/docker.yaml"]
49 changes: 44 additions & 5 deletions src/route/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type BuildSSAOutput struct {
Msg string `json:"msg"`
}

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

// 3. goimports && GOSSAFUNC=foo go build
// 3.1 goimports
err = autoimports(buildFile)
if err != nil {
os.Remove(path)
out.Msg = err.Error()
out.Msg = fmt.Sprintf("cannot run autoimports for your code, err: \n%v", err)
c.JSON(http.StatusBadRequest, out)
return
}

// 3.2 go mod init gossa && go mod tidy
err = initModules(path)
if err != nil {
os.Remove(path)
out.Msg = fmt.Sprintf("cannot use go modules for your code, err: \n%v", err)
c.JSON(http.StatusBadRequest, out)
return
}

// 3.3 GOSSAFUNC=foo go build
outFile := filepath.Join(path, "/main.out")
err = buildSSA(in.FuncName, in.GcFlags, outFile, buildFile, isTest)
if err != nil {
os.Remove(path)
out.Msg = err.Error()
out.Msg = fmt.Sprintf("cannot build ssa for your code, err: \n%v", err)
c.JSON(http.StatusBadRequest, out)
return
}
Expand Down Expand Up @@ -138,6 +151,32 @@ func autoimports(outf string) error {
return nil
}

func initModules(path string) error {
// 1. go mod init
cmd := exec.Command("go", "mod", "init", "gossa")
cmd.Dir = path
cmd.Stderr = &bytes.Buffer{}
err := cmd.Run()
if err != nil {
msg := cmd.Stderr.(*bytes.Buffer).String()
msg = strings.ReplaceAll(msg, path, "$GOSSAPATH")
return errors.New(msg)
}

// 2. go mod tidy
cmd = exec.Command("go", "mod", "tidy")
cmd.Dir = path
cmd.Stderr = &bytes.Buffer{}
err = cmd.Run()
if err != nil {
msg := cmd.Stderr.(*bytes.Buffer).String()
msg = strings.ReplaceAll(msg, path, "$GOSSAPATH")
return errors.New(msg)
}

return nil
}

func buildSSA(funcname, gcflags, outf, buildf string, isTest bool) error {
var cmd *exec.Cmd
if !isTest {
Expand Down