├── LICENSE.md ├── README.md └── go-http.go /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 Gokul Srinivas 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [](https://github.com/GokulSrinivas/go-http/blob/master/LICENSE.md) 2 | [](http://goreportcard.com/report/gokulsrinivas/go-http) 3 | # Go-Http 4 | 5 | A simple command-line static file server written in the Go Programming Language. 6 | 7 | An alternative to `python -m SimpleHTTPServer` 8 | 9 | # Installation 10 | 11 | It's as simple as 12 | 13 | ```sh 14 | $ go get -u github.com/GokulSrinivas/go-http 15 | ``` 16 | # Usage 17 | 18 | To serve the current directory on `localhost:8080`, simply type 19 | 20 | ```sh 21 | $ go-http 22 | ``` 23 | 24 | Options :- 25 | 26 | -p : Specify the port number 27 | -d : Specify subdirectory 28 | 29 | ## Specifying Port Number 30 | 31 | ```sh 32 | $ go-http -p=1234 33 | ``` 34 | 35 | This serves the current directory to port 1234 of localhost 36 | 37 | ## Specifying subdirectory 38 | 39 | ```sh 40 | $ go-http -d=sample 41 | ``` 42 | 43 | This serves the subdirectory `sample` to port 8080 of localhost 44 | 45 | ## Example Usage 46 | 47 | ```sh 48 | $ go-http -p=4141 -d=sample 49 | ``` 50 | 51 | This serves the subdirectory `sample` to port 4141 of localhost 52 | 53 | # Contribute 54 | 55 | If you want to add features, improve them, or report issues, feel free to send a pull request! 56 | 57 | # LICENSE 58 | 59 | [MIT](https://github.com/GokulSrinivas/go-http/blob/master/LICENSE.md) 60 | -------------------------------------------------------------------------------- /go-http.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "io" 7 | "io/ioutil" 8 | "log" 9 | "net/http" 10 | "os" 11 | "path" 12 | "strconv" 13 | "strings" 14 | "time" 15 | ) 16 | 17 | var defaultPath string 18 | 19 | var baseURL string 20 | 21 | func Logger(req *http.Request, statusCode int) { 22 | const layout = "[ 2/Jan/2006 15:04:05 ]" 23 | fmt.Println(baseURL + " --- " + time.Now().Format(layout) + " " + req.Method + " " + strconv.Itoa(statusCode) + " " + req.URL.Path) 24 | } 25 | 26 | func Handler(w http.ResponseWriter, req *http.Request) { 27 | 28 | filename := defaultPath + req.URL.Path[1:] 29 | if last := len(filename) - 1; last >= 0 && filename[last] == '/' && len(filename) != 1 { 30 | filename = filename[:last] 31 | } 32 | 33 | // Empty request (Root) 34 | if filename == "" { 35 | filename = "./" 36 | } 37 | 38 | file, err := os.Stat(filename) 39 | 40 | // 404 if file doesn't exist 41 | if os.IsNotExist(err) { 42 | _, err = io.WriteString(w, "404 Not Found") 43 | Logger(req, http.StatusNotFound) 44 | return 45 | } 46 | 47 | // Serve directory 48 | if file.IsDir() { 49 | 50 | slashCheck := "" 51 | 52 | files, err := ioutil.ReadDir(filename) 53 | // Catch the Error in reading from directory 54 | if err != nil { 55 | http.Redirect(w, req, "", http.StatusInternalServerError) 56 | Logger(req, http.StatusInternalServerError) 57 | } 58 | // Checking for Root Directory 59 | if filename != "./" { 60 | if filename[len(filename)-1] != '/' { 61 | slashCheck = "/" 62 | } 63 | } 64 | 65 | responseString := "