OLD | NEW |
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 "io/ioutil" | 10 "io/ioutil" |
10 "net/http" | 11 "net/http" |
| 12 "os" |
11 "path" | 13 "path" |
| 14 "path/filepath" |
12 "strings" | 15 "strings" |
13 ) | 16 ) |
14 | 17 |
15 type skyHandlerRoot struct { | 18 type skyHandlerRoot struct { |
16 root string | 19 root string |
17 } | 20 } |
18 | 21 |
19 func skyHandler(root string) http.Handler { | 22 func skyHandler(root string) http.Handler { |
20 return &skyHandlerRoot{root} | 23 return &skyHandlerRoot{root} |
21 } | 24 } |
22 | 25 |
23 func (handler *skyHandlerRoot) ServeHTTP(w http.ResponseWriter, r *http.Request)
{ | 26 func (handler *skyHandlerRoot) ServeHTTP(w http.ResponseWriter, r *http.Request)
{ |
24 path := path.Join(handler.root, r.URL.Path) | 27 path := path.Join(handler.root, r.URL.Path) |
25 if strings.HasSuffix(path, ".sky") { | 28 if strings.HasSuffix(path, ".sky") { |
26 w.Header().Set("Content-Type", "text/sky") | 29 w.Header().Set("Content-Type", "text/sky") |
27 } | 30 } |
28 http.ServeFile(w, r, path) | 31 http.ServeFile(w, r, path) |
29 } | 32 } |
30 | 33 |
| 34 func usage() { |
| 35 fmt.Fprintf(os.Stderr, "Usage: sky_server [flags] MOJO_SRC_ROOT PORT\n\n") |
| 36 fmt.Fprintf(os.Stderr, "launches a basic http server with mappings into the\
n") |
| 37 fmt.Fprintf(os.Stderr, "mojo repository for framework/service paths.\n") |
| 38 fmt.Fprintf(os.Stderr, "[flags] MUST be before arguments, because go:flag.\n
\n") |
| 39 flag.PrintDefaults() |
| 40 os.Exit(2) |
| 41 } |
| 42 |
| 43 func addMapping(from_path string, to_path string) { |
| 44 // Print to stderr to it's more obvious what this binary does. |
| 45 fmt.Fprintf(os.Stderr, " %s -> %s\n", from_path, to_path) |
| 46 http.Handle(from_path, http.StripPrefix(from_path, skyHandler(to_path))) |
| 47 } |
| 48 |
31 func main() { | 49 func main() { |
32 var configuration = flag.String("t", "Release", "The target configuration (i
.e. Release or Debug)") | 50 var configuration = flag.String("t", "Release", "The target configuration (i
.e. Release or Debug)") |
| 51 |
33 flag.Parse() | 52 flag.Parse() |
| 53 flag.Usage = usage |
| 54 // The built-in go:flag is awful. It only allows short-name arguments |
| 55 // and they *must* be before any unnamed arguments. There are better ones: |
| 56 // https://godoc.org/github.com/jessevdk/go-flags |
| 57 // but for now we're using raw-go. |
| 58 if flag.NArg() != 2 { |
| 59 usage() |
| 60 } |
34 | 61 |
35 args := flag.Args() | 62 root, _ := filepath.Abs(flag.Arg(0)) |
36 root := args[0] | 63 port := flag.Arg(1) |
37 port := args[1] | |
38 | 64 |
39 genRoot := path.Join(root, "out", *configuration, "gen") | 65 genRoot := path.Join(root, "out", *configuration, "gen") |
40 | 66 |
| 67 fmt.Fprintf(os.Stderr, "Mappings for localhost:%s:\n", port) |
| 68 |
| 69 fmt.Fprintf(os.Stderr, " / -> %s\n", root) |
41 http.Handle("/", skyHandler(root)) | 70 http.Handle("/", skyHandler(root)) |
| 71 |
| 72 fmt.Fprintf(os.Stderr, " /echo_post -> custom echo handler\n") |
42 http.HandleFunc("/echo_post", func(w http.ResponseWriter, r *http.Request) { | 73 http.HandleFunc("/echo_post", func(w http.ResponseWriter, r *http.Request) { |
43 defer r.Body.Close() | 74 defer r.Body.Close() |
44 body, _ := ioutil.ReadAll(r.Body) | 75 body, _ := ioutil.ReadAll(r.Body) |
45 w.Write(body) | 76 w.Write(body) |
46 }) | 77 }) |
47 http.Handle("/mojo/public/", http.StripPrefix("/mojo/public/", skyHandler(pa
th.Join(genRoot, "mojo", "public")))) | 78 |
48 http.Handle("/mojo/services/", http.StripPrefix("/mojo/services/", skyHandle
r(path.Join(genRoot, "mojo", "services")))) | 79 addMapping("/mojo/public/", path.Join(genRoot, "mojo", "public")) |
49 http.Handle("/sky/services/", http.StripPrefix("/sky/services/", skyHandler(
path.Join(genRoot, "sky", "services")))) | 80 addMapping("/mojo/services/", path.Join(genRoot, "mojo", "services")) |
| 81 addMapping("/sky/services/", path.Join(genRoot, "sky", "services")) |
50 | 82 |
51 http.ListenAndServe(":" + port, nil) | 83 http.ListenAndServe(":" + port, nil) |
52 } | 84 } |
OLD | NEW |