Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(518)

Side by Side Diff: sky/tools/skygo/sky_server.go

Issue 1200993002: Update README.md and HACKING.md and resulting yak shave. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package main 5 package main
6 6
7 import ( 7 import (
8 "flag" 8 "flag"
9 "fmt" 9 "fmt"
10 "io/ioutil" 10 "io/ioutil"
11 "net/http" 11 "net/http"
12 "os" 12 "os"
13 "path" 13 "path"
14 "path/filepath" 14 "path/filepath"
15 "strings" 15 "strings"
16 ) 16 )
17 17
18 var verbose bool = false;
19
18 type skyHandlerRoot struct { 20 type skyHandlerRoot struct {
19 root string 21 root string
20 } 22 }
21 23
22 func skyHandler(root string) http.Handler { 24 func skyHandler(root string) http.Handler {
23 return &skyHandlerRoot{root} 25 return &skyHandlerRoot{root}
24 } 26 }
25 27
26 func (handler *skyHandlerRoot) ServeHTTP(w http.ResponseWriter, r *http.Request) { 28 func (handler *skyHandlerRoot) ServeHTTP(w http.ResponseWriter, r *http.Request) {
27 if strings.HasPrefix(r.URL.Path, "/.git") { 29 if strings.HasPrefix(r.URL.Path, "/.git") {
28 w.WriteHeader(http.StatusNotFound) 30 w.WriteHeader(http.StatusNotFound)
29 return 31 return
30 } 32 }
31 path := path.Join(handler.root, r.URL.Path) 33 path := path.Join(handler.root, r.URL.Path)
34
35 // Remove this one once .sky files are gone:
32 if strings.HasSuffix(path, ".sky") { 36 if strings.HasSuffix(path, ".sky") {
33 w.Header().Set("Content-Type", "text/sky") 37 w.Header().Set("Content-Type", "text/sky")
34 } 38 }
39
40 if strings.HasSuffix(path, ".dart") {
41 w.Header().Set("Content-Type", "application/dart")
42 }
35 w.Header().Set("Cache-Control", "no-cache") 43 w.Header().Set("Cache-Control", "no-cache")
36 http.ServeFile(w, r, path) 44 http.ServeFile(w, r, path)
37 } 45 }
38 46
39 func usage() { 47 func usage() {
40 » fmt.Fprintf(os.Stderr, "Usage: sky_server [flags] MOJO_SRC_ROOT PORT\n\n ") 48 » fmt.Fprintf(os.Stderr, "Usage: sky_server [flags] MOJO_SRC_ROOT PACKAGE_ ROOT\n")
41 » fmt.Fprintf(os.Stderr, "launches a basic http server with mappings into the\n") 49 » fmt.Fprintf(os.Stderr, "Launches a basic http server with mappings into the mojo repository for framework/service paths.\n")
42 » fmt.Fprintf(os.Stderr, "mojo repository for framework/service paths.\n") 50 » fmt.Fprintf(os.Stderr, "MOJO_SRC_ROOT must be the root of the Mojo repos itory.\n")
43 » fmt.Fprintf(os.Stderr, "[flags] MUST be before arguments, because go:fla g.\n\n") 51 » fmt.Fprintf(os.Stderr, "PACKAGE_ROOT must be the root of your Dart packa ges (e.g. out/Debug/gen/dart-pkg/packages/).\n")
44 flag.PrintDefaults() 52 flag.PrintDefaults()
45 os.Exit(2) 53 os.Exit(2)
46 } 54 }
47 55
48 func addMapping(from_path string, to_path string) { 56 func addMapping(from_path string, to_path string) {
49 » // Print to stderr to it's more obvious what this binary does. 57 » if (verbose) {
50 » fmt.Fprintf(os.Stderr, " %s -> %s\n", from_path, to_path) 58 » » fmt.Fprintf(os.Stderr, " %s -> %s\n", from_path, to_path)
59 » }
51 http.Handle(from_path, http.StripPrefix(from_path, skyHandler(to_path))) 60 http.Handle(from_path, http.StripPrefix(from_path, skyHandler(to_path)))
52 } 61 }
53 62
54 func main() { 63 func setupMappings(mojoRoot string, packageRoot string, port int) {
55 » var configuration = flag.String("t", "Release", "The target configuratio n (i.e. Release or Debug)") 64 » if (verbose) {
65 » » fmt.Fprintf(os.Stderr, "Mappings for localhost:%v:\n", port)
66 » » fmt.Fprintf(os.Stderr, " / -> %s\n", mojoRoot)
67 » }
68 » http.Handle("/", skyHandler(mojoRoot))
56 69
57 » flag.Parse() 70 » if (verbose) {
58 » flag.Usage = usage 71 » » fmt.Fprintf(os.Stderr, " /echo_post -> custom echo handler\n")
59 » // The built-in go:flag is awful. It only allows short-name arguments
60 » // and they *must* be before any unnamed arguments. There are better on es:
61 » // https://godoc.org/github.com/jessevdk/go-flags
62 » // but for now we're using raw-go.
63 » if flag.NArg() != 3 {
64 » » usage()
65 } 72 }
66
67 root, _ := filepath.Abs(flag.Arg(0))
68 port := flag.Arg(1)
69 packageRoot := flag.Arg(2)
70
71 // genRoot should not be needed once we figure out how mojom generation
72 // for sdk users should work.
73 genRoot := path.Join(root, "out", *configuration, "gen")
74
75 fmt.Fprintf(os.Stderr, "Mappings for localhost:%s:\n", port)
76
77 fmt.Fprintf(os.Stderr, " / -> %s\n", root)
78 http.Handle("/", skyHandler(root))
79
80 fmt.Fprintf(os.Stderr, " /echo_post -> custom echo handler\n")
81 http.HandleFunc("/echo_post", func(w http.ResponseWriter, r *http.Reques t) { 73 http.HandleFunc("/echo_post", func(w http.ResponseWriter, r *http.Reques t) {
82 defer r.Body.Close() 74 defer r.Body.Close()
83 body, _ := ioutil.ReadAll(r.Body) 75 body, _ := ioutil.ReadAll(r.Body)
84 w.Write(body) 76 w.Write(body)
85 }) 77 })
86 78
87 addMapping("/gen/", genRoot)
88 addMapping("/packages/", packageRoot) 79 addMapping("/packages/", packageRoot)
80 }
89 81
90 » http.ListenAndServe(":"+port, nil) 82 func main() {
83 » var portPtr = flag.Int("p", 8000, "The HTTP port")
84 » var verbosePtr = flag.Bool("v", false, "Verbose mode. Without this flag, the default behaviour only reports errors.")
85 » flag.Parse()
86 » flag.Usage = usage
87 » if flag.NArg() != 2 {
88 » » usage()
89 » }
90
91 » var port int = *portPtr;
92 » verbose = *verbosePtr;
93
94 » root, _ := filepath.Abs(flag.Arg(0))
95 » packageRoot := flag.Arg(1)
96
97 » setupMappings(root, packageRoot, port);
98
99 » http.ListenAndServe(fmt.Sprintf(":%v", port), nil)
91 } 100 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698