├── .gitignore ├── LICENSE ├── README.md ├── ghp.go └── ghp_test.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, build with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Curtis Lusmore 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ghp 2 | A simple web server for serving static GitHub Pages locally, to test before 3 | deploying. 4 | 5 | This can be useful compared to browsing local HTML files from your browser when 6 | you use absolute paths in links, such as `/about`, `/js/app.js`, 7 | `/css/style.css`, etc., which won't resolve correctly in the context of your 8 | filesystem. 9 | 10 | It is also handy compared to something like `python -m http.server` which 11 | doesn't support dropping the file extension, e.g. `/about` rather than 12 | `/about.html`. 13 | 14 | When requesting any path (`$path`), `ghp` will do the following (all file 15 | operations are relative to the `root` commandline flag): 16 | 1. Check whether `$path` points to a file, if so serve that file 17 | 1. Check whether `$path` points to a directory, if so serve `$path/index.html` 18 | 2. Check whether `$path.html` points to a file, if so serve that file 19 | 3. Check whether `404.html` is a file, if so serve that file as a 404 20 | 4. Serve a 404 21 | 5. If any of the above results in serving a Markdown file (extension `.md`), 22 | render the contents as HTML by using the [GitHub Markdown API][3]. 23 | 24 | ## Getting It 25 | From source (requires installing Go): 26 | ``` 27 | $ git clone https://github.com/CurtisLusmore/ghp 28 | $ cd ghp 29 | $ go build ghp.go 30 | ``` 31 | 32 | With Go Get (requires installing Go): 33 | ``` 34 | $ go get github.com/CurtisLusmore/ghp 35 | ``` 36 | 37 | Pre-compiled binaries: Check the [latest Releases][1] 38 | 39 | 40 | ## Usage 41 | ``` 42 | $ ghp -help 43 | Usage of ghp: 44 | -port int 45 | The port to serve over (default 8080) 46 | -root string 47 | The root directory to serve files from (your GitHub Pages repo) (default ".") 48 | $ ghp -root MyGitHubPages 49 | ``` 50 | 51 | ## Notes 52 | * This tool currently does not support building Jekyll-based GitHub Pages. If 53 | you use Jekyll-based GitHub Pages, please see 54 | [Setting up your GitHub Pages site locally with Jekyll][2]. 55 | * As this tool exposes your filesystem to your network, you should be careful 56 | using this on untrusted networks. 57 | * This tool will send the contents of Markdown files (extension `.md`) to 58 | https://api.github.com/markdown. Make sure not to run this in a directory 59 | with sensitive content in Markdown files - this is mostly intended for use on 60 | files you are likely to push to GitHub Pages anyway. 61 | 62 | ## Todo 63 | * Confirm response headers match live GitHub Pages 64 | 65 | 66 | [1]: https://github.com/CurtisLusmore/ghp/releases 67 | [2]: https://help.github.com/articles/setting-up-your-github-pages-site-locally-with-jekyll/ 68 | [3]: https://developer.github.com/v3/markdown/#render-an-arbitrary-markdown-document -------------------------------------------------------------------------------- /ghp.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "flag" 6 | "fmt" 7 | "io/ioutil" 8 | "log" 9 | "net/http" 10 | "os" 11 | "path" 12 | "path/filepath" 13 | ) 14 | 15 | var root string 16 | 17 | func handler(w http.ResponseWriter, r *http.Request) { 18 | basepath := root + r.URL.Path 19 | filename := basepath 20 | 21 | if fi, err := os.Stat(filename); fi != nil && fi.IsDir() { 22 | filename = basepath + "/index.html" 23 | } else if os.IsNotExist(err) { 24 | filename = basepath + ".html" 25 | if _, err := os.Stat(filename); os.IsNotExist(err) { 26 | filename = basepath + ".md" 27 | } 28 | } 29 | 30 | if _, err := os.Stat(filename); os.IsNotExist(err) { 31 | filename = root + "/404.html" 32 | page, _ := ioutil.ReadFile(filename) 33 | w.WriteHeader(http.StatusNotFound) 34 | w.Write(page) 35 | fmt.Printf("GET %s 404 NOT FOUND\n", r.URL.Path) 36 | return 37 | } 38 | 39 | filename = filepath.Clean(filename) 40 | renderFile(w, r, filename) 41 | fmt.Printf("GET %s (%s) 200 OK\n", r.URL.Path, filename) 42 | } 43 | 44 | func main() { 45 | var port int 46 | flag.StringVar(&root, "root", ".", 47 | "The root directory to serve files from "+ 48 | "(your GitHub Pages repo)") 49 | flag.IntVar(&port, "port", 8080, "The port to serve over") 50 | flag.Parse() 51 | 52 | root, err := filepath.Abs(root) 53 | if err != nil { 54 | fmt.Printf("Unable to serve directory: %s\n", root) 55 | os.Exit(1) 56 | } 57 | 58 | fmt.Printf("Serving from %s on http://localhost:%d ...\n", root, port) 59 | http.HandleFunc("/", handler) 60 | log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil)) 61 | } 62 | 63 | func renderFile(w http.ResponseWriter, r *http.Request, filename string) { 64 | ext := path.Ext(filename) 65 | switch ext { 66 | case ".md": 67 | content, err := ioutil.ReadFile(filename) 68 | if err != nil { 69 | http.ServeFile(w, r, filename) 70 | } 71 | 72 | htmlContent, err := convertMarkdown(content) 73 | if err != nil { 74 | http.ServeFile(w, r, filename) 75 | } 76 | 77 | w.Write(htmlContent) 78 | default: 79 | http.ServeFile(w, r, filename) 80 | } 81 | } 82 | 83 | func convertMarkdown(mdText []byte) ([]byte, error) { 84 | body := bytes.NewReader(mdText) 85 | req, err := http.NewRequest("POST", "https://api.github.com/markdown/raw", body) 86 | if err != nil { 87 | return nil, err 88 | } 89 | req.Header.Set("Content-Type", "text/plain") 90 | 91 | resp, err := http.DefaultClient.Do(req) 92 | if err != nil { 93 | return nil, err 94 | } 95 | defer resp.Body.Close() 96 | 97 | return ioutil.ReadAll(resp.Body) 98 | } 99 | -------------------------------------------------------------------------------- /ghp_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "reflect" 6 | "testing" 7 | ) 8 | 9 | func Test_convertMarkdown(t *testing.T) { 10 | type args struct { 11 | mdText []byte 12 | } 13 | tests := []struct { 14 | name string 15 | args args 16 | want []byte 17 | wantErr bool 18 | }{ 19 | { 20 | name: "Test 1", 21 | args: args{mdText: []byte("*test*")}, 22 | want: []byte("

test

"), 23 | wantErr: false, 24 | }, 25 | } 26 | for _, tt := range tests { 27 | t.Run(tt.name, func(t *testing.T) { 28 | got, err := convertMarkdown(tt.args.mdText) 29 | if (err != nil) != tt.wantErr { 30 | t.Errorf("convertMarkdown() error = %v, wantErr %v", err, tt.wantErr) 31 | return 32 | } 33 | tt.want = bytes.TrimSpace(tt.want) 34 | got = bytes.TrimSpace(got) 35 | if !reflect.DeepEqual(got, tt.want) { 36 | t.Errorf("convertMarkdown() = %v, want %v", got, tt.want) 37 | } 38 | }) 39 | } 40 | } 41 | --------------------------------------------------------------------------------