Skip to content

Commit 2b99405

Browse files
committed
add start command
1 parent 2ac9d26 commit 2b99405

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

cmd/root.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cmd
22

3-
import "github.com/spf13/cobra"
3+
import (
4+
"github.com/spf13/cobra"
5+
)
46

57
var rootCmd = &cobra.Command{
68
Use: "http-server",

cmd/start.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var folder string
11+
var port int
12+
13+
func init() {
14+
rootCmd.AddCommand(startCmd)
15+
startCmd.Flags().StringVarP(&folder, "directory", "d", "", "Path of the web folder")
16+
startCmd.Flags().IntVarP(&port, "port", "p", 8080, "Port number")
17+
}
18+
19+
var startCmd = &cobra.Command{
20+
Use: "start",
21+
Short: "Start the HTTP server",
22+
23+
Args: cobra.MinimumNArgs(0),
24+
25+
Run: func(cmd *cobra.Command, args []string) {
26+
fmt.Println("serving directory: ", folder)
27+
fmt.Println("listening on port: ", port)
28+
fileHandler := http.FileServer(http.Dir(folder))
29+
http.ListenAndServe(fmt.Sprint(":", port), fileHandler)
30+
},
31+
}

0 commit comments

Comments
 (0)