Browse Source

Add the cli tool

Konstantinos Lypitkas 5 years ago
commit
10b3774e55
4 changed files with 70 additions and 0 deletions
  1. 2 0
      .gitignore
  2. 33 0
      README.md
  3. 3 0
      go.mod
  4. 32 0
      main.go

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+fs-go
+!.gitignore

+ 33 - 0
README.md

@@ -0,0 +1,33 @@
+# FS Go
+
+FS Go is a tiny CLI tool in order to spawn a local file server.
+
+### REQUIREMENTS
+
+- [Go](https://golang.org) >= **1.13**
+
+### BUILD
+
+```
+go build
+```
+
+### START THE SERVER
+
+```
+./fs-go -port 8080 -dir .
+```
+
+### GLOBAL INSTALLATION
+
+```
+go install
+```
+
+Now you can use:
+
+```
+fs-go
+```
+
+in order to start a new instance.

+ 3 - 0
go.mod

@@ -0,0 +1,3 @@
+module github.com/klipitkas/fs-go
+
+go 1.13

+ 32 - 0
main.go

@@ -0,0 +1,32 @@
+package main
+
+import (
+	"flag"
+	"fmt"
+	"log"
+	"net/http"
+)
+
+var port int
+var dir string
+
+func main() {
+
+	// Handle the flags that are provided.
+	flag.IntVar(&port, "port", 8080, "The port that the server will listen to.")
+	flag.StringVar(&dir, "dir", ".", "The root directory that will be served.")
+	// Parse the flags.
+	flag.Parse()
+
+	// Create the fileserver.
+	fs := http.FileServer(http.Dir(dir))
+
+	// Print details to the console.
+	log.Printf("FS - Port: %v | Dir: %v", port, dir)
+
+	// Start the file server.
+	if err := http.ListenAndServe(":"+fmt.Sprintf("%d", port), fs); err != nil {
+		log.Fatalf("server listen on port %v: %v", port, err)
+	}
+
+}