diff --git a/.gitignore b/.gitignore index b1ef685..9d3b7d0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -fs-go -!.gitignore \ No newline at end of file +fs-go \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..99a49c7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM golang:1.13-alpine as base + +WORKDIR /app +COPY . . +RUN go get ./... && go build -o fs-go . + +FROM alpine:3.10 + +VOLUME /data +WORKDIR /app +COPY --from=base /app/fs-go . +EXPOSE 8080 + +ENTRYPOINT ["/app/fs-go", "-dir", "/data"] \ No newline at end of file diff --git a/README.md b/README.md index 8d9126b..483e555 100644 --- a/README.md +++ b/README.md @@ -19,13 +19,13 @@ go build Starting the server is easy: ```bash -./fs-go -port 8080 -dir . +./fs-go -addr 8080 -dir . ``` Only two parameters are needed: - **dir**: The directory that will be served. (**default**: `.`) -- **port**: The port that the server will listen to. (**default**: `8080`) +- **addr**: The address that the server will listen to. (**default**: `:8080`) ### INSTALL GLOBALLY @@ -42,4 +42,22 @@ Now you can use: fs-go ``` -From anywhere in your system. \ No newline at end of file +From anywhere in your system. + +### BUILD AND RUN WITH DOCKER + +To build the Docker image: + +```bash +docker build -t fs:local . +``` + +Run the server using Docker: +``` +docker run --rm -v $(pwd):/data -p 8080:8080 -d fs:local +``` + +To run the server using Docker in a different port: +``` +docker run --rm -v $(pwd):/data -p 9000:9000 fs:local -addr :9000 +``` diff --git a/main.go b/main.go index 655514f..f2add68 100644 --- a/main.go +++ b/main.go @@ -2,32 +2,25 @@ package main import ( "flag" - "fmt" "log" "net/http" ) -// The port that the fileserver will listen to. -var port int - -// The root directory that will be served. -var dir string +var ( + addr = flag.String("addr", ":8080", "The address that the server will listen to.") + dir = flag.String("dir", ".", "The root directory that will be served.") +) 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)) + fs := http.FileServer(http.Dir(*dir)) // Print details to the console. - log.Printf("FS - Port: %d | Dir: %s", port, dir) + log.Printf("fs-go - address: %s | directory: %s", *addr, *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) - } + log.Fatal(http.ListenAndServe(*addr, fs)) }