├── main.go ├── README.md └── cmd └── root.go /main.go: -------------------------------------------------------------------------------- 1 | // Copyright © 2018 NAME HERE 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 main 16 | 17 | import "github.com/snwfdhmp/simplehttp/cmd" 18 | 19 | func main() { 20 | cmd.Execute() 21 | } 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Quickly serve a local directory over http 2 | 3 | ## Getting started 4 | 5 | Install with 6 | 7 | ```shell 8 | go install github.com/snwfdhmp/simplehttp@latest 9 | ``` 10 | 11 | ## Usage 12 | 13 | With no arguments, simplehttp starts serving files under ./ over port 8080. 14 | 15 | ```shell 16 | $ simplehttp 17 | INFO[0000] Serving ./ over 0.0.0.0:8080... Stop with ^C 18 | ``` 19 | 20 | With `-d` arg, specify the directory to be served. 21 | 22 | ```shell 23 | $ simplehttp -d ./templates/ 24 | INFO[0000] Serving ./templates/ over 0.0.0.0:8080... Stop with ^C 25 | ``` 26 | 27 | With `-p` arg, specify the port to serve on. 28 | 29 | ```shell 30 | $ simplehttp -p 31415 31 | INFO[0000] Serving ./ over 0.0.0.0:31415... Stop with ^C 32 | ``` 33 | 34 | ## Feedback 35 | 36 | Feel free to open an issue for any feedback or suggestion. 37 | 38 | I fix bugs quickly. 39 | 40 | ## Contributions 41 | 42 | PR are accepted as soon as they follow Golang common standards. 43 | For more information: https://golang.org/doc/effective_go.html 44 | -------------------------------------------------------------------------------- /cmd/root.go: -------------------------------------------------------------------------------- 1 | // Copyright © 2018 NAME HERE 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 cmd 16 | 17 | import ( 18 | "fmt" 19 | "net/http" 20 | "os" 21 | 22 | log "github.com/sirupsen/logrus" 23 | "github.com/spf13/cobra" 24 | ) 25 | 26 | var dirToServe string 27 | var serverPort string 28 | var urlPrefix string 29 | 30 | // RootCmd represents the base command when called without any subcommands 31 | var RootCmd = &cobra.Command{ 32 | Use: "simplehttp", 33 | Short: "Quickly serve a local filesystem directory over http", 34 | Long: `Quickly serve a local filesystem directory over http 35 | 36 | With no arguments, simplehttp starts serving files under ./ over port 8080. 37 | With '-d' arg, specify the directory to be served. 38 | With '-p' arg, specify the port to serve on. 39 | `, 40 | Run: func(cmd *cobra.Command, args []string) { 41 | fs := http.FileServer(http.Dir(dirToServe)) 42 | 43 | http.Handle(urlPrefix, http.StripPrefix(urlPrefix, fs)) 44 | 45 | log.Infof("Serving %s over 0.0.0.0:%s... Stop with ^C", dirToServe, serverPort) 46 | http.ListenAndServe(":"+serverPort, nil) 47 | }, 48 | } 49 | 50 | // Execute adds all child commands to the root command and sets flags appropriately. 51 | // This is called by main.main(). It only needs to happen once to the rootCmd. 52 | func Execute() { 53 | if err := RootCmd.Execute(); err != nil { 54 | fmt.Println(err) 55 | os.Exit(1) 56 | } 57 | } 58 | 59 | func init() { 60 | RootCmd.Flags().StringVarP(&dirToServe, "dir", "d", "./", "root directory to be served (ex: /var/www) [default is ./]") 61 | 62 | RootCmd.Flags().StringVar(&serverPort, "port", "8080", "port to listen to [default is 8080)") 63 | RootCmd.Flags().StringVar(&urlPrefix, "prefix", "/", "prefix required (ex: /static), suffix to host:port [default is /]") 64 | } 65 | --------------------------------------------------------------------------------