Teach sky_server not to serve up /.git

Sky doesn't need the git repo of the mojo repo.

R=eseidel@chromium.org

Review URL: https://codereview.chromium.org/1059113004
This commit is contained in:
James Robinson
2015-04-21 16:01:17 -07:00
parent 975fdca798
commit 8364320dfe
3 changed files with 59 additions and 55 deletions

View File

@@ -16,4 +16,4 @@ Uploading the locally built server
upload_to_google_storage.py is in depot_tools. It will overwrite
sky_server.sha1. Include that change in your code review.
sky_server was last built using go version go1.3 linux/amd64.
sky_server was last built using go version go1.4.2 linux/amd64.

View File

@@ -5,83 +5,87 @@
package main
import (
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"strings"
)
type skyHandlerRoot struct {
root string
root string
}
func skyHandler(root string) http.Handler {
return &skyHandlerRoot{root}
return &skyHandlerRoot{root}
}
func (handler *skyHandlerRoot) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := path.Join(handler.root, r.URL.Path)
if strings.HasSuffix(path, ".sky") {
w.Header().Set("Content-Type", "text/sky")
}
w.Header().Set("Cache-Control", "no-cache")
http.ServeFile(w, r, path)
if strings.HasPrefix(r.URL.Path, "/.git") {
w.WriteHeader(http.StatusNotFound)
return
}
path := path.Join(handler.root, r.URL.Path)
if strings.HasSuffix(path, ".sky") {
w.Header().Set("Content-Type", "text/sky")
}
w.Header().Set("Cache-Control", "no-cache")
http.ServeFile(w, r, path)
}
func usage() {
fmt.Fprintf(os.Stderr, "Usage: sky_server [flags] MOJO_SRC_ROOT PORT\n\n")
fmt.Fprintf(os.Stderr, "launches a basic http server with mappings into the\n")
fmt.Fprintf(os.Stderr, "mojo repository for framework/service paths.\n")
fmt.Fprintf(os.Stderr, "[flags] MUST be before arguments, because go:flag.\n\n")
flag.PrintDefaults()
os.Exit(2)
fmt.Fprintf(os.Stderr, "Usage: sky_server [flags] MOJO_SRC_ROOT PORT\n\n")
fmt.Fprintf(os.Stderr, "launches a basic http server with mappings into the\n")
fmt.Fprintf(os.Stderr, "mojo repository for framework/service paths.\n")
fmt.Fprintf(os.Stderr, "[flags] MUST be before arguments, because go:flag.\n\n")
flag.PrintDefaults()
os.Exit(2)
}
func addMapping(from_path string, to_path string) {
// Print to stderr to it's more obvious what this binary does.
fmt.Fprintf(os.Stderr, " %s -> %s\n", from_path, to_path)
http.Handle(from_path, http.StripPrefix(from_path, skyHandler(to_path)))
// Print to stderr to it's more obvious what this binary does.
fmt.Fprintf(os.Stderr, " %s -> %s\n", from_path, to_path)
http.Handle(from_path, http.StripPrefix(from_path, skyHandler(to_path)))
}
func main() {
var configuration = flag.String("t", "Release", "The target configuration (i.e. Release or Debug)")
var configuration = flag.String("t", "Release", "The target configuration (i.e. Release or Debug)")
flag.Parse()
flag.Usage = usage
// The built-in go:flag is awful. It only allows short-name arguments
// and they *must* be before any unnamed arguments. There are better ones:
// https://godoc.org/github.com/jessevdk/go-flags
// but for now we're using raw-go.
if flag.NArg() != 3 {
usage()
}
flag.Parse()
flag.Usage = usage
// The built-in go:flag is awful. It only allows short-name arguments
// and they *must* be before any unnamed arguments. There are better ones:
// https://godoc.org/github.com/jessevdk/go-flags
// but for now we're using raw-go.
if flag.NArg() != 3 {
usage()
}
root, _ := filepath.Abs(flag.Arg(0))
port := flag.Arg(1)
packageRoot := flag.Arg(2)
root, _ := filepath.Abs(flag.Arg(0))
port := flag.Arg(1)
packageRoot := flag.Arg(2)
// genRoot should not be needed once we figure out how mojom generation
// for sdk users should work.
genRoot := path.Join(root, "out", *configuration, "gen")
// genRoot should not be needed once we figure out how mojom generation
// for sdk users should work.
genRoot := path.Join(root, "out", *configuration, "gen")
fmt.Fprintf(os.Stderr, "Mappings for localhost:%s:\n", port)
fmt.Fprintf(os.Stderr, "Mappings for localhost:%s:\n", port)
fmt.Fprintf(os.Stderr, " / -> %s\n", root)
http.Handle("/", skyHandler(root))
fmt.Fprintf(os.Stderr, " / -> %s\n", root)
http.Handle("/", skyHandler(root))
fmt.Fprintf(os.Stderr, " /echo_post -> custom echo handler\n")
http.HandleFunc("/echo_post", func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, _ := ioutil.ReadAll(r.Body)
w.Write(body)
})
fmt.Fprintf(os.Stderr, " /echo_post -> custom echo handler\n")
http.HandleFunc("/echo_post", func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, _ := ioutil.ReadAll(r.Body)
w.Write(body)
})
addMapping("/gen/", genRoot)
addMapping("/packages/", packageRoot)
addMapping("/gen/", genRoot)
addMapping("/packages/", packageRoot)
http.ListenAndServe(":" + port, nil)
http.ListenAndServe(":"+port, nil)
}

View File

@@ -1 +1 @@
6d1d975f16d5ff09aeb2f7b6ee97eea33d0c9fa8
02d2a03fedfb85053f7f94a0f9464b228609658a