Skip to content

Commit

Permalink
all: support gcflags and query param for sharing
Browse files Browse the repository at this point in the history
  • Loading branch information
changkun committed Apr 5, 2020
1 parent d4af302 commit cb32305
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@

# Dependency directories (remove the comment below to include it)
# vendor/
data/
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ BUILD=ssaplayground.out
all:
go build -o $(BUILD) -mod vendor
start:
./$(BUILD) -conf config.yaml
./$(BUILD) -conf config/config.yaml
docker:
echo "NOT IMPLEMENTED"
docker build -t ssaplayground:v0.1 -f docker/Dockerfile .
update:
docker run -itd -v ~/dev/ssaplayground/data:/app/public/buildbox -p 6789:6789 ssaplayground:v0.1
clean:
rm -rf $(BUILD)
rm -rf $(BUILD)
.PHONY: all start docker update clean
12 changes: 6 additions & 6 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
- [x] Auto format && import
- [x] ~~Dark mode~~ Doesn't work for dynamic iframe
- [x] Message: waiting for SSA response
- [ ] Dragable SSA
- [ ] Compiler flags, e.g. disable
- [ ] More environment variable support, need investigation
- [ ] Use short link instead of UUID
- [ ] Containerized deployment
- [ ] Better Editor: Monaco Editor
- [x] ~~Dragable SSA~~ Doesn't work for dynamic iframe
- [x] Compiler flags, -gcflags="-N -l"
- [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
2 changes: 1 addition & 1 deletion config.yaml → config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
addr: localhost:6789
addr: 0.0.0.0:6789
mode: debug
static: public
13 changes: 8 additions & 5 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@
<div id="banner">
<div id="head" itemprop="name">The Go SSA Playground</div>
<div id="controls" class="form-inline">
<input class="form-control" type="text" value="" placeholder="GOSSAFUNC" id="funcname">
<input class="btn btn-primary" type="button" value="Build" id="build">
<input type="button" value="Build" id="build">
<span>GOSSAFUNC=</span>
<input type="text" value="" placeholder="Function name" id="funcname">
<span>-gcflags=</span>
<input type="text" value="" placeholder="e.g. -N -l" id="gcflags">
</div>
<div id="aboutControls">
<!-- doesn't work for iframe -->
<!-- <label class="switch">
<input id="dark-mode-checkbox" type="checkbox">
<span class="slider"></span>
</label> -->
<input class="btn btn-primary" type="button" value="About" id="aboutbtn">
<input type="button" value="About" id="aboutbtn">
</div>
</div>
<div id="main">
Expand Down Expand Up @@ -53,7 +56,7 @@
</div>
</div>
<script src="/gossa/main.js"></script>
<script src="/gossa/magic.js"></script>
<script src="/gossa/dark.js"></script>
<!-- <script src="/gossa/magic.js"></script>
<script src="/gossa/dark.js"></script> -->
</body>
</html>
49 changes: 42 additions & 7 deletions public/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ let msgbox = document.getElementById('outputMsg')
let ssabox = document.getElementById('ssa')
ssabox.addEventListener('load', () => {
// inject ssa style
let head = $("iframe").contents().find("head");
head.append($("<link/>", { rel: 'stylesheet', href: '/gossa/scrollbar.css', type: 'text/css'}));
$("iframe").contents().find("head").append($("<link/>", { rel: 'stylesheet', href: '/gossa/scrollbar.css', type: 'text/css'}));
setMessageBox('', true)
});

let lastFuncName, lastCode;
let lastFuncName, lastCode, lastGcflags;
function build() {
let funcname = document.getElementById('funcname').value;
let code = document.getElementById('code').value;
let gcflags = document.getElementById('gcflags').value;

// several early checks
if (funcname === lastFuncName && code == lastCode) {
if (funcname === lastFuncName && code === lastCode && gcflags === lastGcflags) {
console.log('no changes, do not submit')
return
}
Expand All @@ -33,6 +33,7 @@ function build() {

lastFuncName = funcname
lastCode = code
lastGcflags = gcflags
setMessageBox('Waiting for response...', false)

fetch('/api/v1/buildssa', {
Expand All @@ -41,6 +42,7 @@ function build() {
body: JSON.stringify({
'funcname': funcname,
'code': code,
'gcflags': gcflags,
}),
})
.then((response) => {
Expand All @@ -53,8 +55,12 @@ function build() {
})
.then(res => {
ssabox.src = `/gossa/buildbox/${res.data.build_id}/ssa.html`

// update url
const param = 'id='+res.data.build_id
history.pushState(null, null, document.location.href.split('?')[0] + '?' + param)
})
.catch(res => setMessageBox(res.data.msg, true));
.catch(res => setMessageBox(res.data.msg, false));
}

function setMessageBox(msg, hide) {
Expand Down Expand Up @@ -95,8 +101,8 @@ $('#code').linedtextarea();
$('#code').keydown(function(event){
if (event.keyCode == 9) {
event.preventDefault();
var start = this.selectionStart;
var end = this.selectionEnd;
let start = this.selectionStart;
let end = this.selectionEnd;
// set textarea value to: text before caret + tab + text after caret
$(this).val($(this).val().substring(0, start)
+ "\t"
Expand All @@ -107,6 +113,35 @@ $('#code').keydown(function(event){
}
});

function loadCode() {
const params = new URLSearchParams(window.location.search);
const id = params.get('id');
if (id === null || id === undefined || id === '') { return; }
ssabox.src = `/gossa/buildbox/${id}/ssa.html`
// load code
fetch(`/gossa/buildbox/${id}/main.go`)
.then((response) => {
return new Promise((resolve, reject) => {
let func; response.status < 400 ? func = resolve : func = reject;
response.text().then(data => func({'status': response.status, 'data': data}));
});
})
.then(res => {
console.log(res)
document.getElementById('code').textContent = res.data
})
.catch(res => {
fetch(`/gossa/buildbox/${id}/main_test.go`)
.then(res => res.text())
.then(res => {
console.log(res)
document.getElementById('code').textContent = res
})
// if still fail? don't handle anything
});
}
loadCode() // load content if access with id

// TODO: dragable scroll
// let wholePage = document.querySelector('body');
// let el = document.querySelector("#ssa").contentDocument.querySelector('body');
Expand Down
9 changes: 6 additions & 3 deletions public/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ body {
margin-bottom: 1em;
}

#funcname {
width: 200px;
#funcname,#gcflags {
width: 150px;
padding: 6px 12px;
font-size: 14px;
color: #555;
Expand Down Expand Up @@ -95,10 +95,13 @@ input[type=button]:focus {
padding: 10px;
background: rgba(255, 252, 221, 0.81);
}
#code, #output, pre, .lines {
#code, #output, pre, .lines, #controls span {
font-family: Menlo, monospace;
font-size: 11pt;
}
#controls span {
margin: 0 5px;
}
#output {
flex: 70%;
background-color: #f1f1f1;
Expand Down
9 changes: 5 additions & 4 deletions src/route/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func Pong(c *gin.Context) {

type BuildSSAInput struct {
FuncName string `json:"funcname"`
GcFlags string `json:"gcflags"`
Code string `json:"code"`
}
type BuildSSAOutput struct {
Expand Down Expand Up @@ -99,7 +100,7 @@ func BuildSSA(c *gin.Context) {
return
}
outFile := filepath.Join(path, "/main.out")
err = buildSSA(in.FuncName, outFile, buildFile, isTest)
err = buildSSA(in.FuncName, in.GcFlags, outFile, buildFile, isTest)
if err != nil {
os.Remove(path)
out.Msg = err.Error()
Expand Down Expand Up @@ -137,12 +138,12 @@ func autoimports(outf string) error {
return nil
}

func buildSSA(funcname, outf, buildf string, isTest bool) error {
func buildSSA(funcname, gcflags, outf, buildf string, isTest bool) error {
var cmd *exec.Cmd
if !isTest {
cmd = exec.Command("go", "build", "-o", outf, buildf)
cmd = exec.Command("go", "build", fmt.Sprintf(`-gcflags=%s`, gcflags), "-o", outf, buildf)
} else {
cmd = exec.Command("go", "test", buildf)
cmd = exec.Command("go", "test", fmt.Sprintf(`-gcflags=%s`, gcflags), buildf)
}
cmd.Env = append(os.Environ(), fmt.Sprintf("GOSSAFUNC=%s", funcname))
cmd.Stderr = &bytes.Buffer{}
Expand Down

0 comments on commit cb32305

Please sign in to comment.