Skip to content

Commit

Permalink
Merge branch 'changes'
Browse files Browse the repository at this point in the history
  • Loading branch information
klipitkas committed Nov 5, 2019
2 parents 5c1a829 + e5402cf commit b073263
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
fs-go
!.gitignore
fs-go
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -42,4 +42,22 @@ Now you can use:
fs-go
```

From anywhere in your system.
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
```
21 changes: 7 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit b073263

Please sign in to comment.