2010-10-13 21:06:02 -06:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2012-02-20 17:24:29 -07:00
|
|
|
"bytes"
|
2010-10-13 21:06:02 -06:00
|
|
|
"flag"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2011-11-08 16:43:02 -07:00
|
|
|
"net/http"
|
2010-10-13 21:06:02 -06:00
|
|
|
"os"
|
2011-11-08 16:43:02 -07:00
|
|
|
"os/exec"
|
2012-02-20 17:24:29 -07:00
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
2010-10-13 21:06:02 -06:00
|
|
|
"strconv"
|
2011-11-08 16:43:02 -07:00
|
|
|
"text/template"
|
2010-10-13 21:06:02 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
httpListen = flag.String("http", "127.0.0.1:3999", "host:port to listen on")
|
|
|
|
htmlOutput = flag.Bool("html", false, "render program output as HTML")
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// a source of numbers, for naming temporary files
|
|
|
|
uniq = make(chan int)
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
// source of unique numbers
|
|
|
|
go func() {
|
|
|
|
for i := 0; ; i++ {
|
|
|
|
uniq <- i
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
http.HandleFunc("/", FrontPage)
|
|
|
|
http.HandleFunc("/compile", Compile)
|
2011-02-01 13:47:35 -07:00
|
|
|
log.Fatal(http.ListenAndServe(*httpListen, nil))
|
2010-10-13 21:06:02 -06:00
|
|
|
}
|
|
|
|
|
2012-10-30 14:38:01 -06:00
|
|
|
// FrontPage is an HTTP handler that renders the goplay interface.
|
2010-10-13 21:06:02 -06:00
|
|
|
// If a filename is supplied in the path component of the URI,
|
|
|
|
// its contents will be put in the interface's text area.
|
|
|
|
// Otherwise, the default "hello, world" program is displayed.
|
|
|
|
func FrontPage(w http.ResponseWriter, req *http.Request) {
|
|
|
|
data, err := ioutil.ReadFile(req.URL.Path[1:])
|
|
|
|
if err != nil {
|
|
|
|
data = helloWorld
|
|
|
|
}
|
2011-02-09 15:23:01 -07:00
|
|
|
frontPage.Execute(w, data)
|
2010-10-13 21:06:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compile is an HTTP handler that reads Go source code from the request,
|
2012-02-06 10:10:49 -07:00
|
|
|
// runs the program (returning any errors),
|
2010-10-13 21:06:02 -06:00
|
|
|
// and sends the program's output as the HTTP response.
|
|
|
|
func Compile(w http.ResponseWriter, req *http.Request) {
|
2012-02-20 17:24:29 -07:00
|
|
|
out, err := compile(req)
|
2010-10-13 21:06:02 -06:00
|
|
|
if err != nil {
|
2011-11-01 20:06:05 -06:00
|
|
|
error_(w, out, err)
|
2012-02-06 10:10:49 -07:00
|
|
|
return
|
2010-10-13 21:06:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// write the output of x as the http response
|
|
|
|
if *htmlOutput {
|
|
|
|
w.Write(out)
|
|
|
|
} else {
|
2011-02-09 15:23:01 -07:00
|
|
|
output.Execute(w, out)
|
2010-10-13 21:06:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-20 17:24:29 -07:00
|
|
|
var (
|
|
|
|
commentRe = regexp.MustCompile(`(?m)^#.*\n`)
|
|
|
|
tmpdir string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// find real temporary directory (for rewriting filename in output)
|
|
|
|
var err error
|
|
|
|
tmpdir, err = filepath.EvalSymlinks(os.TempDir())
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func compile(req *http.Request) (out []byte, err error) {
|
|
|
|
// x is the base name for .go, .6, executable files
|
|
|
|
x := filepath.Join(tmpdir, "compile"+strconv.Itoa(<-uniq))
|
|
|
|
src := x + ".go"
|
|
|
|
|
|
|
|
// rewrite filename in error output
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
// drop messages from the go tool like '# _/compile0'
|
|
|
|
out = commentRe.ReplaceAll(out, nil)
|
|
|
|
}
|
|
|
|
out = bytes.Replace(out, []byte(src+":"), []byte("main.go:"), -1)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// write body to x.go
|
|
|
|
body := new(bytes.Buffer)
|
|
|
|
if _, err = body.ReadFrom(req.Body); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.Remove(src)
|
|
|
|
if err = ioutil.WriteFile(src, body.Bytes(), 0666); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-07-11 17:41:10 -06:00
|
|
|
// go run x.go
|
2012-02-20 17:24:29 -07:00
|
|
|
dir, file := filepath.Split(src)
|
2013-07-11 17:41:10 -06:00
|
|
|
out, err = run(dir, "go", "run", file)
|
2012-02-20 17:24:29 -07:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-07-11 17:41:10 -06:00
|
|
|
return out, nil
|
2012-02-20 17:24:29 -07:00
|
|
|
}
|
|
|
|
|
2010-10-13 21:06:02 -06:00
|
|
|
// error writes compile, link, or runtime errors to the HTTP connection.
|
|
|
|
// The JavaScript interface uses the 404 status code to identify the error.
|
2011-11-01 20:06:05 -06:00
|
|
|
func error_(w http.ResponseWriter, out []byte, err error) {
|
2010-10-13 21:06:02 -06:00
|
|
|
w.WriteHeader(404)
|
|
|
|
if out != nil {
|
2011-02-09 15:23:01 -07:00
|
|
|
output.Execute(w, out)
|
2010-10-13 21:06:02 -06:00
|
|
|
} else {
|
2011-11-01 20:06:05 -06:00
|
|
|
output.Execute(w, err.Error())
|
2010-10-13 21:06:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// run executes the specified command and returns its output and an error.
|
2012-02-20 17:24:29 -07:00
|
|
|
func run(dir string, args ...string) ([]byte, error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
cmd := exec.Command(args[0], args[1:]...)
|
|
|
|
cmd.Dir = dir
|
|
|
|
cmd.Stdout = &buf
|
|
|
|
cmd.Stderr = cmd.Stdout
|
|
|
|
err := cmd.Run()
|
|
|
|
return buf.Bytes(), err
|
2010-10-13 21:06:02 -06:00
|
|
|
}
|
|
|
|
|
2011-08-16 22:34:48 -06:00
|
|
|
var frontPage = template.Must(template.New("frontPage").Parse(frontPageText)) // HTML template
|
|
|
|
var output = template.Must(template.New("output").Parse(outputText)) // HTML template
|
2010-10-13 21:06:02 -06:00
|
|
|
|
2011-09-04 23:03:41 -06:00
|
|
|
var outputText = `<pre>{{printf "%s" . |html}}</pre>`
|
2010-10-13 21:06:02 -06:00
|
|
|
|
|
|
|
var frontPageText = `<!doctype html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<style>
|
|
|
|
pre, textarea {
|
|
|
|
font-family: Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace;
|
|
|
|
font-size: 100%;
|
|
|
|
}
|
|
|
|
.hints {
|
|
|
|
font-size: 0.8em;
|
|
|
|
text-align: right;
|
|
|
|
}
|
|
|
|
#edit, #output, #errors { width: 100%; text-align: left; }
|
|
|
|
#edit { height: 500px; }
|
|
|
|
#output { color: #00c; }
|
|
|
|
#errors { color: #c00; }
|
|
|
|
</style>
|
|
|
|
<script>
|
|
|
|
|
|
|
|
function insertTabs(n) {
|
|
|
|
// find the selection start and end
|
|
|
|
var cont = document.getElementById("edit");
|
|
|
|
var start = cont.selectionStart;
|
|
|
|
var end = cont.selectionEnd;
|
|
|
|
// split the textarea content into two, and insert n tabs
|
|
|
|
var v = cont.value;
|
|
|
|
var u = v.substr(0, start);
|
|
|
|
for (var i=0; i<n; i++) {
|
|
|
|
u += "\t";
|
|
|
|
}
|
|
|
|
u += v.substr(end);
|
|
|
|
// set revised content
|
|
|
|
cont.value = u;
|
|
|
|
// reset caret position after inserted tabs
|
|
|
|
cont.selectionStart = start+n;
|
|
|
|
cont.selectionEnd = start+n;
|
|
|
|
}
|
|
|
|
|
|
|
|
function autoindent(el) {
|
|
|
|
var curpos = el.selectionStart;
|
|
|
|
var tabs = 0;
|
|
|
|
while (curpos > 0) {
|
|
|
|
curpos--;
|
|
|
|
if (el.value[curpos] == "\t") {
|
|
|
|
tabs++;
|
|
|
|
} else if (tabs > 0 || el.value[curpos] == "\n") {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setTimeout(function() {
|
|
|
|
insertTabs(tabs);
|
|
|
|
}, 1);
|
|
|
|
}
|
|
|
|
|
2012-03-26 18:16:29 -06:00
|
|
|
function preventDefault(e) {
|
|
|
|
if (e.preventDefault) {
|
|
|
|
e.preventDefault();
|
|
|
|
} else {
|
|
|
|
e.cancelBubble = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-16 02:44:51 -06:00
|
|
|
function keyHandler(event) {
|
|
|
|
var e = window.event || event;
|
2010-10-13 21:06:02 -06:00
|
|
|
if (e.keyCode == 9) { // tab
|
|
|
|
insertTabs(1);
|
2012-03-26 18:16:29 -06:00
|
|
|
preventDefault(e);
|
2010-10-13 21:06:02 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (e.keyCode == 13) { // enter
|
|
|
|
if (e.shiftKey) { // +shift
|
|
|
|
compile(e.target);
|
2012-03-26 18:16:29 -06:00
|
|
|
preventDefault(e);
|
2010-10-13 21:06:02 -06:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
autoindent(e.target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
var xmlreq;
|
|
|
|
|
|
|
|
function autocompile() {
|
|
|
|
if(!document.getElementById("autocompile").checked) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
compile();
|
|
|
|
}
|
|
|
|
|
|
|
|
function compile() {
|
|
|
|
var prog = document.getElementById("edit").value;
|
|
|
|
var req = new XMLHttpRequest();
|
|
|
|
xmlreq = req;
|
|
|
|
req.onreadystatechange = compileUpdate;
|
|
|
|
req.open("POST", "/compile", true);
|
|
|
|
req.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
|
|
|
|
req.send(prog);
|
|
|
|
}
|
|
|
|
|
|
|
|
function compileUpdate() {
|
|
|
|
var req = xmlreq;
|
|
|
|
if(!req || req.readyState != 4) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(req.status == 200) {
|
|
|
|
document.getElementById("output").innerHTML = req.responseText;
|
|
|
|
document.getElementById("errors").innerHTML = "";
|
|
|
|
} else {
|
|
|
|
document.getElementById("errors").innerHTML = req.responseText;
|
|
|
|
document.getElementById("output").innerHTML = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<table width="100%"><tr><td width="60%" valign="top">
|
2011-09-04 17:50:22 -06:00
|
|
|
<textarea autofocus="true" id="edit" spellcheck="false" onkeydown="keyHandler(event);" onkeyup="autocompile();">{{printf "%s" . |html}}</textarea>
|
2010-10-13 21:06:02 -06:00
|
|
|
<div class="hints">
|
|
|
|
(Shift-Enter to compile and run.)
|
|
|
|
<input type="checkbox" id="autocompile" value="checked" /> Compile and run after each keystroke
|
|
|
|
</div>
|
|
|
|
<td width="3%">
|
|
|
|
<td width="27%" align="right" valign="top">
|
|
|
|
<div id="output"></div>
|
|
|
|
</table>
|
|
|
|
<div id="errors"></div>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`
|
|
|
|
|
|
|
|
var helloWorld = []byte(`package main
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
fmt.Println("hello, world")
|
|
|
|
}
|
|
|
|
`)
|