blob: 8312f4f8c335b45edaf423d18f16cd048b978d13 [file] [log] [blame]
// Copyright 2011 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 (
"fmt"
"html/template"
"net/http"
"strings"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
)
const hostname = "play.golang.org"
var editTemplate = template.Must(template.ParseFiles("edit.html"))
type editData struct {
Snippet *Snippet
Share bool
}
func edit(w http.ResponseWriter, r *http.Request) {
// Redirect foo.play.golang.org to play.golang.org.
if strings.HasSuffix(r.Host, "."+hostname) {
http.Redirect(w, r, "https://"+hostname, http.StatusFound)
return
}
snip := &Snippet{Body: []byte(hello)}
if strings.HasPrefix(r.URL.Path, "/p/") {
if !allowShare(r) {
w.WriteHeader(http.StatusUnavailableForLegalReasons)
w.Write([]byte(`<h1>Unavailable For Legal Reasons</h1><p>Viewing and/or sharing code snippets is not available in your country for legal reasons. This message might also appear if your country is misdetected. If you believe this is an error, please <a href="https://golang.org/issue">file an issue</a>.</p>`))
return
}
c := appengine.NewContext(r)
id := r.URL.Path[3:]
serveText := false
if strings.HasSuffix(id, ".go") {
id = id[:len(id)-3]
serveText = true
}
key := datastore.NewKey(c, "Snippet", id, 0, nil)
err := datastore.Get(c, key, snip)
if err != nil {
if err != datastore.ErrNoSuchEntity {
log.Errorf(c, "loading Snippet: %v", err)
}
http.Error(w, "Snippet not found", http.StatusNotFound)
return
}
if serveText {
if r.FormValue("download") == "true" {
w.Header().Set(
"Content-Disposition", fmt.Sprintf(`attachment; filename="%s.go"`, id),
)
}
w.Header().Set("Content-type", "text/plain")
w.Write(snip.Body)
return
}
}
editTemplate.Execute(w, &editData{snip, allowShare(r)})
}
const hello = `package main
import (
"fmt"
)
func main() {
fmt.Println("Hello, playground")
}
`