Skip to content

Commit

Permalink
Merge pull request #63 from Remitly/static-assets
Browse files Browse the repository at this point in the history
Static assets now built into the binary
  • Loading branch information
Shraya Ramani committed Sep 25, 2018
2 parents 0fcc468 + a5a8639 commit a946076
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 6 deletions.
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 2
jobs:
build:
docker:
- image: circleci/golang:1.9
- image: circleci/golang:1.10
working_directory: /go/src/github.com/buzzfeed/sso
steps:
- checkout
Expand All @@ -15,7 +15,7 @@ jobs:
command: |
cp Godeps /tmp/Godeps
cp gpm /tmp/gpm
cd /tmp && ./gpm
cd /tmp && ./gpm install
- run:
name: copy source
command: |
Expand All @@ -38,4 +38,4 @@ jobs:
if [[ -n $DOCKER_USER ]]; then
docker login -u $DOCKER_USER -p $DOCKER_PASS
make imagepush
fi
fi
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@ RUN cd cmd/sso-proxy && go build -o /bin/sso-proxy
FROM debian:stable-slim
RUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/lib/apt/lists/*
WORKDIR /sso
COPY ./static ./static
COPY --from=build /bin/sso-* /bin/
1 change: 1 addition & 0 deletions Godeps
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ github.com/benbjohnson/clock 7dc76406b6d3c05b5f71a86293cbcf3c4ea03b1
github.com/kelseyhightower/envconfig v1.3.0
github.com/miscreant/miscreant-go 6b98fbe3dd42dfd24a8ecbabdb3586ada20dc5f8
github.com/sirupsen/logrus e54a77765aca7bbdd8e56c1c54f60579968b2dc9
github.com/rakyll/statik v0.1.4
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ build: dist/sso-auth dist/sso-proxy

dist/sso-auth:
mkdir -p dist
go generate ./...
go build -o dist/sso-auth ./cmd/sso-auth

dist/sso-proxy:
mkdir -p dist
go generate ./...
go build -o dist/sso-proxy ./cmd/sso-proxy

test:
Expand Down
9 changes: 7 additions & 2 deletions internal/auth/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ func NewAuthenticator(opts *Options, optionFuncs ...func(*Authenticator) error)
}

func (p *Authenticator) newMux() http.Handler {
logger := log.NewLogEntry()

mux := http.NewServeMux()

// we setup global endpoints that should respond to any hostname
Expand All @@ -181,8 +183,11 @@ func (p *Authenticator) newMux() http.Handler {
serviceMux.HandleFunc("/validate", p.withMethods(p.validateClientID(p.validateClientSecret(p.ValidateToken)), "GET"))
serviceMux.HandleFunc("/redeem", p.withMethods(p.validateClientID(p.validateClientSecret(p.Redeem)), "POST"))
serviceMux.HandleFunc("/refresh", p.withMethods(p.validateClientID(p.validateClientSecret(p.Refresh)), "POST"))
// serve static files but prevent directory listings, see static_files.go
serviceMux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(noDirectoryFS{http.Dir("./static")})))
fsHandler, err := loadFSHandler()
if err != nil {
logger.Fatal(err)
}
serviceMux.Handle("/static/", http.StripPrefix("/static/", fsHandler))

// NOTE: we have to include trailing slash for the router to match the host header
host := p.Host
Expand Down
16 changes: 16 additions & 0 deletions internal/auth/static_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ package auth
import (
"net/http"
"os"

"github.com/rakyll/statik/fs"

// Statik makes assets available via a blank import
_ "github.com/buzzfeed/sso/internal/auth/statik"
)

// noDirectoryFilesystem is used to prevent an http.FileServer from providing directory listings
Expand All @@ -29,3 +34,14 @@ func (fs noDirectoryFS) Open(name string) (http.File, error) {

return f, nil
}

//go:generate $GOPATH/bin/statik -src=./static

func loadFSHandler() (http.Handler, error) {
statikFS, err := fs.New()
if err != nil {
return nil, err
}

return http.FileServer(noDirectoryFS{statikFS}), nil
}
12 changes: 12 additions & 0 deletions internal/auth/statik/statik.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a946076

Please sign in to comment.