Index: sky/tools/skygo/sky_server.go |
diff --git a/sky/tools/skygo/sky_server.go b/sky/tools/skygo/sky_server.go |
index 1327a19d3c6c8628ba2a5b7c18ec12d9aca33fc4..3b6ac706c651d8a2709bc37df12cb8462cd020d9 100644 |
--- a/sky/tools/skygo/sky_server.go |
+++ b/sky/tools/skygo/sky_server.go |
@@ -15,6 +15,8 @@ import ( |
"strings" |
) |
+var verbose bool = false; |
+ |
type skyHandlerRoot struct { |
root string |
} |
@@ -29,63 +31,70 @@ func (handler *skyHandlerRoot) ServeHTTP(w http.ResponseWriter, r *http.Request) |
return |
} |
path := path.Join(handler.root, r.URL.Path) |
+ |
+ // Remove this one once .sky files are gone: |
if strings.HasSuffix(path, ".sky") { |
w.Header().Set("Content-Type", "text/sky") |
} |
+ |
+ if strings.HasSuffix(path, ".dart") { |
+ w.Header().Set("Content-Type", "application/dart") |
+ } |
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") |
+ fmt.Fprintf(os.Stderr, "Usage: sky_server [flags] MOJO_SRC_ROOT PACKAGE_ROOT\n") |
+ fmt.Fprintf(os.Stderr, "Launches a basic http server with mappings into the mojo repository for framework/service paths.\n") |
+ fmt.Fprintf(os.Stderr, "MOJO_SRC_ROOT must be the root of the Mojo repository.\n") |
+ fmt.Fprintf(os.Stderr, "PACKAGE_ROOT must be the root of your Dart packages (e.g. out/Debug/gen/dart-pkg/packages/).\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) |
+ if (verbose) { |
+ 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)") |
- |
- 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() |
+func setupMappings(mojoRoot string, packageRoot string, port int) { |
+ if (verbose) { |
+ fmt.Fprintf(os.Stderr, "Mappings for localhost:%v:\n", port) |
+ fmt.Fprintf(os.Stderr, " / -> %s\n", mojoRoot) |
} |
+ http.Handle("/", skyHandler(mojoRoot)) |
- 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") |
- |
- 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, " /echo_post -> custom echo handler\n") |
+ if (verbose) { |
+ 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) |
+} |
+ |
+func main() { |
+ var portPtr = flag.Int("p", 8000, "The HTTP port") |
+ var verbosePtr = flag.Bool("v", false, "Verbose mode. Without this flag, the default behaviour only reports errors.") |
+ flag.Parse() |
+ flag.Usage = usage |
+ if flag.NArg() != 2 { |
+ usage() |
+ } |
+ |
+ var port int = *portPtr; |
+ verbose = *verbosePtr; |
+ |
+ root, _ := filepath.Abs(flag.Arg(0)) |
+ packageRoot := flag.Arg(1) |
+ |
+ setupMappings(root, packageRoot, port); |
- http.ListenAndServe(":"+port, nil) |
+ http.ListenAndServe(fmt.Sprintf(":%v", port), nil) |
} |