├── CONTRIBUTING ├── LICENSE ├── README.md ├── cmd └── 127.go ├── main.bu └── onetwoseven.go /CONTRIBUTING: -------------------------------------------------------------------------------- 1 | Want to contribute? Great! First, read this page (including the small print at the end). 2 | 3 | ### Before you contribute 4 | Before we can use your code, you must sign the 5 | [Google Individual Contributor License Agreement] 6 | (https://cla.developers.google.com/about/google-individual) 7 | (CLA), which you can do online. The CLA is necessary mainly because you own the 8 | copyright to your changes, even after your contribution becomes part of our 9 | codebase, so we need your permission to use and distribute your code. We also 10 | need to be sure of various other things—for instance that you'll tell us if you 11 | know that your code infringes on other people's patents. You don't have to sign 12 | the CLA until after you've submitted your code for review and a member has 13 | approved it, but you must do it before we can put your code into our codebase. 14 | Before you start working on a larger contribution, you should get in touch with 15 | us first through the issue tracker with your idea so that we can help out and 16 | possibly guide you. Coordinating up front makes it much easier to avoid 17 | frustration later on. 18 | 19 | ### Code reviews 20 | All submissions, including submissions by project members, require review. We 21 | use Github pull requests for this purpose. 22 | 23 | ### The small print 24 | Contributions made by corporations are covered by a different agreement than 25 | the one above, the 26 | [Software Grant and Corporate Contributor License Agreement] 27 | (https://cla.developers.google.com/about/google-corporate). 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2016 Google Inc. All Rights Reserved. 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License. 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # onetwoseven 2 | 3 | The simplest programmer's web server. Use it as an executable or a Go library. 4 | 5 | $ 127 6 | 12:43 I ━┃ ━━┃┏━┃ 7 | 12:43 I ┃ ┏━┛ ┃ 8 | 12:43 I ━━┛━━┛ ┛ 9 | 12:43 I starting . on 127.0.0.1:8190 10 | 11 | There are as few options as possible. 12 | 13 | $ 127 --help 14 | usage: 127 [] [] 15 | 16 | Programmer's test http server 17 | 18 | Flags: 19 | --help Show context-sensitive help (also try --help-long and --help-man). 20 | -h, --host="127.0.0.1:8190" Host and port to start server on 21 | -p, --path="." path to serve. 22 | -d, --verbose Print debugging output. 23 | 24 | -------------------------------------------------------------------------------- /cmd/127.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 Google Inc. All Rights Reserved. 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // Command 127 provides a programmers debugging web server. 16 | package main 17 | 18 | import ( 19 | "os" 20 | "path/filepath" 21 | 22 | "github.com/aliafshar/toylog" 23 | "gopkg.in/alecthomas/kingpin.v2" 24 | 25 | "github.com/googlesamples/onetwoseven" 26 | ) 27 | 28 | var ( 29 | app = kingpin.New("127", "Programmer's test http server") 30 | hostFlag = app.Flag("host", 31 | "Host and port to start server on"). 32 | Short('h'). 33 | Default("127.0.0.1:8190"). 34 | String() 35 | pathFlag = app.Flag("path", 36 | "path to serve."). 37 | Short('p'). 38 | Default("."). 39 | String() 40 | verboseFlag = app.Flag("verbose", 41 | "Print debugging output."). 42 | Short('d'). 43 | Bool() 44 | 45 | ) 46 | 47 | const ( 48 | logo1 = "━┃ ━━┃┏━┃" 49 | logo2 = " ┃ ┏━┛ ┃" 50 | logo3 = "━━┛━━┛ ┛" 51 | ) 52 | 53 | func main() { 54 | app.Arg("path", "path to serve.").StringVar(pathFlag) 55 | _, err := app.Parse(os.Args[1:]) 56 | if err != nil { 57 | toylog.Fatalln(err) 58 | } 59 | path, err := filepath.Abs(*pathFlag) 60 | if err != nil { 61 | toylog.Fatalln("path looks suspicious", *pathFlag, err) 62 | } 63 | cfg := &onetwoseven.Config{Path: path, HostPort: *hostFlag} 64 | toylog.Infoln(logo1) 65 | toylog.Infoln(logo2) 66 | toylog.Infoln(logo3) 67 | toylog.Infoln("starting", cfg) 68 | onetwoseven.Serve(cfg) 69 | } 70 | -------------------------------------------------------------------------------- /main.bu: -------------------------------------------------------------------------------- 1 | 2 | lint: 3 | gofmt -w *.go cmd/*.go 4 | 5 | run: 6 | go run cmd/127.go --path=test 7 | -------------------------------------------------------------------------------- /onetwoseven.go: -------------------------------------------------------------------------------- 1 | // Copyright 2016 Google Inc. All Rights Reserved. 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // Package onetwoseven is a simple web server. 16 | package onetwoseven 17 | 18 | import ( 19 | "fmt" 20 | "net/http" 21 | 22 | "github.com/aliafshar/toylog" 23 | ) 24 | 25 | type Config struct { 26 | Path string 27 | HostPort string 28 | } 29 | 30 | func (c *Config) String() string { 31 | return fmt.Sprintf("%v on %v", c.Path, c.HostPort) 32 | } 33 | 34 | type server struct { 35 | config *Config 36 | } 37 | 38 | type wrappingHandler struct { 39 | pre func(http.ResponseWriter, *http.Request) 40 | post func(http.ResponseWriter, *http.Request) 41 | handler http.Handler 42 | } 43 | 44 | func (h *wrappingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { 45 | if h.pre != nil { 46 | h.pre(w, r) 47 | } 48 | h.handler.ServeHTTP(w, r) 49 | if h.post != nil { 50 | h.post(w, r) 51 | } 52 | } 53 | 54 | func (s *server) FileServer() http.Handler { 55 | h := http.FileServer(http.Dir(s.config.Path)) 56 | return &wrappingHandler{ 57 | handler: h, 58 | post: func(w http.ResponseWriter, r *http.Request) { 59 | log(w, r) 60 | }, 61 | } 62 | } 63 | 64 | func (s *server) start() { 65 | http.Handle("/", s.FileServer()) 66 | http.ListenAndServe(s.config.HostPort, nil) 67 | } 68 | 69 | func log(w http.ResponseWriter, r *http.Request) { 70 | toylog.Infof("%s %s %s", r.Method, r.URL, w.Header()) 71 | } 72 | 73 | func Serve(config *Config) { 74 | s := &server{config: config} 75 | s.start() 76 | } 77 | --------------------------------------------------------------------------------