Skip to content

Commit cb32305

Browse files
committed
all: support gcflags and query param for sharing
1 parent d4af302 commit cb32305

File tree

8 files changed

+75
-29
lines changed

8 files changed

+75
-29
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313

1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
16+
data/

Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ BUILD=ssaplayground.out
22
all:
33
go build -o $(BUILD) -mod vendor
44
start:
5-
./$(BUILD) -conf config.yaml
5+
./$(BUILD) -conf config/config.yaml
66
docker:
7-
echo "NOT IMPLEMENTED"
7+
docker build -t ssaplayground:v0.1 -f docker/Dockerfile .
8+
update:
9+
docker run -itd -v ~/dev/ssaplayground/data:/app/public/buildbox -p 6789:6789 ssaplayground:v0.1
810
clean:
9-
rm -rf $(BUILD)
11+
rm -rf $(BUILD)
12+
.PHONY: all start docker update clean

TODO.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
- [x] Auto format && import
55
- [x] ~~Dark mode~~ Doesn't work for dynamic iframe
66
- [x] Message: waiting for SSA response
7-
- [ ] Dragable SSA
8-
- [ ] Compiler flags, e.g. disable
9-
- [ ] More environment variable support, need investigation
10-
- [ ] Use short link instead of UUID
11-
- [ ] Containerized deployment
12-
- [ ] Better Editor: Monaco Editor
7+
- [x] ~~Dragable SSA~~ Doesn't work for dynamic iframe
8+
- [x] Compiler flags, -gcflags="-N -l"
9+
- [x] ~~Use short link instead of UUID?~~ seems no need
10+
- [x] share with /gossa?id=uuid -> access /gossa/uuid/main.go or /gossa/uuid/main_test.go, access /gossa/uuid/ssa.html
11+
- [x] ~~Better Editor: Monaco Editor~~ maybe not?
12+
- [ ] Containerized deployment
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
---
2-
addr: localhost:6789
2+
addr: 0.0.0.0:6789
33
mode: debug
44
static: public

public/index.html

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@
1515
<div id="banner">
1616
<div id="head" itemprop="name">The Go SSA Playground</div>
1717
<div id="controls" class="form-inline">
18-
<input class="form-control" type="text" value="" placeholder="GOSSAFUNC" id="funcname">
19-
<input class="btn btn-primary" type="button" value="Build" id="build">
18+
<input type="button" value="Build" id="build">
19+
<span>GOSSAFUNC=</span>
20+
<input type="text" value="" placeholder="Function name" id="funcname">
21+
<span>-gcflags=</span>
22+
<input type="text" value="" placeholder="e.g. -N -l" id="gcflags">
2023
</div>
2124
<div id="aboutControls">
2225
<!-- doesn't work for iframe -->
2326
<!-- <label class="switch">
2427
<input id="dark-mode-checkbox" type="checkbox">
2528
<span class="slider"></span>
2629
</label> -->
27-
<input class="btn btn-primary" type="button" value="About" id="aboutbtn">
30+
<input type="button" value="About" id="aboutbtn">
2831
</div>
2932
</div>
3033
<div id="main">
@@ -53,7 +56,7 @@
5356
</div>
5457
</div>
5558
<script src="/gossa/main.js"></script>
56-
<script src="/gossa/magic.js"></script>
57-
<script src="/gossa/dark.js"></script>
59+
<!-- <script src="/gossa/magic.js"></script>
60+
<script src="/gossa/dark.js"></script> -->
5861
</body>
5962
</html>

public/main.js

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ let msgbox = document.getElementById('outputMsg')
1111
let ssabox = document.getElementById('ssa')
1212
ssabox.addEventListener('load', () => {
1313
// inject ssa style
14-
let head = $("iframe").contents().find("head");
15-
head.append($("<link/>", { rel: 'stylesheet', href: '/gossa/scrollbar.css', type: 'text/css'}));
14+
$("iframe").contents().find("head").append($("<link/>", { rel: 'stylesheet', href: '/gossa/scrollbar.css', type: 'text/css'}));
1615
setMessageBox('', true)
1716
});
1817

19-
let lastFuncName, lastCode;
18+
let lastFuncName, lastCode, lastGcflags;
2019
function build() {
2120
let funcname = document.getElementById('funcname').value;
2221
let code = document.getElementById('code').value;
22+
let gcflags = document.getElementById('gcflags').value;
2323

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

3434
lastFuncName = funcname
3535
lastCode = code
36+
lastGcflags = gcflags
3637
setMessageBox('Waiting for response...', false)
3738

3839
fetch('/api/v1/buildssa', {
@@ -41,6 +42,7 @@ function build() {
4142
body: JSON.stringify({
4243
'funcname': funcname,
4344
'code': code,
45+
'gcflags': gcflags,
4446
}),
4547
})
4648
.then((response) => {
@@ -53,8 +55,12 @@ function build() {
5355
})
5456
.then(res => {
5557
ssabox.src = `/gossa/buildbox/${res.data.build_id}/ssa.html`
58+
59+
// update url
60+
const param = 'id='+res.data.build_id
61+
history.pushState(null, null, document.location.href.split('?')[0] + '?' + param)
5662
})
57-
.catch(res => setMessageBox(res.data.msg, true));
63+
.catch(res => setMessageBox(res.data.msg, false));
5864
}
5965

6066
function setMessageBox(msg, hide) {
@@ -95,8 +101,8 @@ $('#code').linedtextarea();
95101
$('#code').keydown(function(event){
96102
if (event.keyCode == 9) {
97103
event.preventDefault();
98-
var start = this.selectionStart;
99-
var end = this.selectionEnd;
104+
let start = this.selectionStart;
105+
let end = this.selectionEnd;
100106
// set textarea value to: text before caret + tab + text after caret
101107
$(this).val($(this).val().substring(0, start)
102108
+ "\t"
@@ -107,6 +113,35 @@ $('#code').keydown(function(event){
107113
}
108114
});
109115

116+
function loadCode() {
117+
const params = new URLSearchParams(window.location.search);
118+
const id = params.get('id');
119+
if (id === null || id === undefined || id === '') { return; }
120+
ssabox.src = `/gossa/buildbox/${id}/ssa.html`
121+
// load code
122+
fetch(`/gossa/buildbox/${id}/main.go`)
123+
.then((response) => {
124+
return new Promise((resolve, reject) => {
125+
let func; response.status < 400 ? func = resolve : func = reject;
126+
response.text().then(data => func({'status': response.status, 'data': data}));
127+
});
128+
})
129+
.then(res => {
130+
console.log(res)
131+
document.getElementById('code').textContent = res.data
132+
})
133+
.catch(res => {
134+
fetch(`/gossa/buildbox/${id}/main_test.go`)
135+
.then(res => res.text())
136+
.then(res => {
137+
console.log(res)
138+
document.getElementById('code').textContent = res
139+
})
140+
// if still fail? don't handle anything
141+
});
142+
}
143+
loadCode() // load content if access with id
144+
110145
// TODO: dragable scroll
111146
// let wholePage = document.querySelector('body');
112147
// let el = document.querySelector("#ssa").contentDocument.querySelector('body');

public/style.css

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ body {
5151
margin-bottom: 1em;
5252
}
5353

54-
#funcname {
55-
width: 200px;
54+
#funcname,#gcflags {
55+
width: 150px;
5656
padding: 6px 12px;
5757
font-size: 14px;
5858
color: #555;
@@ -95,10 +95,13 @@ input[type=button]:focus {
9595
padding: 10px;
9696
background: rgba(255, 252, 221, 0.81);
9797
}
98-
#code, #output, pre, .lines {
98+
#code, #output, pre, .lines, #controls span {
9999
font-family: Menlo, monospace;
100100
font-size: 11pt;
101101
}
102+
#controls span {
103+
margin: 0 5px;
104+
}
102105
#output {
103106
flex: 70%;
104107
background-color: #f1f1f1;

src/route/api.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func Pong(c *gin.Context) {
3838

3939
type BuildSSAInput struct {
4040
FuncName string `json:"funcname"`
41+
GcFlags string `json:"gcflags"`
4142
Code string `json:"code"`
4243
}
4344
type BuildSSAOutput struct {
@@ -99,7 +100,7 @@ func BuildSSA(c *gin.Context) {
99100
return
100101
}
101102
outFile := filepath.Join(path, "/main.out")
102-
err = buildSSA(in.FuncName, outFile, buildFile, isTest)
103+
err = buildSSA(in.FuncName, in.GcFlags, outFile, buildFile, isTest)
103104
if err != nil {
104105
os.Remove(path)
105106
out.Msg = err.Error()
@@ -137,12 +138,12 @@ func autoimports(outf string) error {
137138
return nil
138139
}
139140

140-
func buildSSA(funcname, outf, buildf string, isTest bool) error {
141+
func buildSSA(funcname, gcflags, outf, buildf string, isTest bool) error {
141142
var cmd *exec.Cmd
142143
if !isTest {
143-
cmd = exec.Command("go", "build", "-o", outf, buildf)
144+
cmd = exec.Command("go", "build", fmt.Sprintf(`-gcflags=%s`, gcflags), "-o", outf, buildf)
144145
} else {
145-
cmd = exec.Command("go", "test", buildf)
146+
cmd = exec.Command("go", "test", fmt.Sprintf(`-gcflags=%s`, gcflags), buildf)
146147
}
147148
cmd.Env = append(os.Environ(), fmt.Sprintf("GOSSAFUNC=%s", funcname))
148149
cmd.Stderr = &bytes.Buffer{}

0 commit comments

Comments
 (0)