Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
lewislbr committed Apr 1, 2021
1 parent 35445f7 commit 05729b8
Show file tree
Hide file tree
Showing 20 changed files with 831 additions and 741 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ Dockerfile
LICENSE
Makefile
README.md
web
test
10 changes: 5 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
FROM golang:1-alpine AS build
ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64
ENV CGO_ENABLED=0 \
GO111MODULE=on \
GOARCH=amd64 \
GOOS=linux
WORKDIR /
COPY . ./
RUN go build -o gss -ldflags="-s -w" src/main.go
RUN go build -o gss -ldflags="-s -w" gss.go

FROM scratch
COPY --from=build /gss ./
Expand Down
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
build:
@docker build -t lewislbr/gss:test .

run-cli:
@docker run -p 8080:7891 -v $$PWD/web/dist:/public lewislbr/gss:test -d public -p 7891
run-cli: build
@docker run --rm -p 8080:8081 -v $$PWD/test/web/dist:/public lewislbr/gss:test -d public -p 8081

run-default:
@docker run -p 8080:80 -v $$PWD/web/dist:/dist lewislbr/gss:test
run-default: build
@docker run --rm -p 8080:80 -v $$PWD/test/web/dist:/dist lewislbr/gss:test

run-yaml:
@docker run -p 8080:7892 -v $$PWD/gss.yaml:/gss.yaml -v $$PWD/web/dist:/public lewislbr/gss:test
run-yaml: build
@docker run --rm -p 8080:8081 -v $$PWD/test/gss.yaml:/gss.yaml -v $$PWD/test/web/dist:/public lewislbr/gss:test
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ docker run -p [container-port]:[server-port] -v [yaml-file]:/gss.yaml -v [spa-fo
>
> ```yaml
> directory: public
> port: 7892
> port: 8080
> ```
>
> ```sh
> docker run -p 8080:7892 -v $PWD/gss.yaml:/gss.yaml -v $PWD/web/dist:/public lewislbr/gss:test
> docker run -p 8080:8080 -v $PWD/gss.yaml:/gss.yaml -v $PWD/web/dist:/public lewislbr/gss:test
> ```
With CLI:
Expand All @@ -52,7 +52,7 @@ docker run -p [container-port]:[server-port] -v [spa-folder]:/[container-folder]
> Example:
>
> ```sh
> docker run -p 8080:7891 -v $PWD/public:/dist lewislbr/gss -d public -p 7891
> docker run -p 8080:8080 -v $PWD/public:/dist lewislbr/gss -d public -p 8080
> ```
If both a YAML config and a CLI flag set up a configuration option, the CLI flag prevails.
Expand Down Expand Up @@ -106,13 +106,13 @@ Default: `80`.
> YAML:
>
> ```yaml
> port: 7892
> port: 8080
> ```
>
> CLI:
>
> ```sh
> docker run -p 8080:7892 -v $PWD/public:/dist lewislbr/gss -p 7892
> docker run -p 8080:8080 -v $PWD/public:/dist lewislbr/gss -p 8080
> ```
## Contributing
Expand Down
37 changes: 16 additions & 21 deletions src/main.go → gss.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,7 @@ func main() {
}
}

// startServer initializes the server.
func startServer() error {
httpServer := customHTTPServer(port, addHeaders(serveSPA(dir)))

log.Printf("GSS info: serving directory %q on port %v ✅\n", dir, port)

err := httpServer.ListenAndServe()
if err != nil {
return err
}

return nil
}

// setUpYAML enables configuration via YAML file.
// Enable configuration via YAML file.
func setUpYAML() error {
configFile := "gss.yaml"

Expand Down Expand Up @@ -99,7 +85,7 @@ func setUpYAML() error {
return nil
}

// setUpCLI enables configuration via CLI flags.
// Enable configuration via CLI flags.
func setUpCLI() {
d := flag.String("d", dir, "Path to the directory to serve.")
p := flag.String("p", port, "Port where to run the server.")
Expand All @@ -120,18 +106,27 @@ func setUpCLI() {
}
}

// customHTTPServer configures a basic HTTP server
func customHTTPServer(port string, h http.Handler) *http.Server {
// Initialize the server.
func startServer() error {
s := setUpServer(port)

log.Printf("GSS info: serving directory %q on port %v ✅\n", dir, port)

return s.ListenAndServe()
}

// Configure a basic HTTP server.
func setUpServer(port string) *http.Server {
return &http.Server{
Addr: ":" + port,
Handler: h,
Handler: addHeaders(serveSPA(dir)),
IdleTimeout: 120 * time.Second,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
}

// serveSPA serves files from a directory, defaulting to the index if the root
// Serve files from a directory, defaulting to the index if the root
// is requested or a file is not found, leaving it for the SPA to handle. If
// the directory contains pre-compressed brotli or gzip files those are served
// instead for the file types that accept them.
Expand Down Expand Up @@ -191,7 +186,7 @@ func serveSPA(dir string) http.HandlerFunc {
}
}

// addHeaders adds custom headers to the response
// Add custom headers to the response.
func addHeaders(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Server", "GSS")
Expand Down
2 changes: 1 addition & 1 deletion gss.yaml → test/gss.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ directory: public
headers:
Referrer-Policy: "strict-origin-when-cross-origin"
Strict-Transport-Security: "max-age=63072000; includeSubDomains; preload"
port: 7892
port: 8081
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 05729b8

Please sign in to comment.