Skip to content

Commit

Permalink
Remove Buffalo (gomods#1010)
Browse files Browse the repository at this point in the history
* Remove Buffalo

* gofmt

* pr fixes

* fix subrouter

* bring back secure middleware + pr fixes

* better place for subrouter

* vendor
  • Loading branch information
marwan-at-work authored Dec 23, 2018
1 parent 36aba59 commit 5870aee
Show file tree
Hide file tree
Showing 1,129 changed files with 461 additions and 371,404 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ tmp/*
vgp
bin/*
.envrc
athens
yarn-error.log
cmd/proxy/proxy
test-keys.json
tmp
Expand Down
21 changes: 0 additions & 21 deletions cmd/proxy/.buffalo.dev.yml

This file was deleted.

35 changes: 0 additions & 35 deletions cmd/proxy/actions/actions_test.go

This file was deleted.

78 changes: 31 additions & 47 deletions cmd/proxy/actions/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,27 @@ package actions

import (
"fmt"
"net/http"
"os"

"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/buffalo/render"
"github.com/gobuffalo/mw-forcessl"
"github.com/gobuffalo/mw-paramlogger"
"github.com/gobuffalo/x/sessions"
"github.com/gomods/athens/pkg/config"
"github.com/gomods/athens/pkg/log"
mw "github.com/gomods/athens/pkg/middleware"
"github.com/gomods/athens/pkg/module"
"github.com/gomods/athens/pkg/observ"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
"github.com/unrolled/secure"
"go.opencensus.io/plugin/ochttp"
)

// Service is the name of the service that we want to tag our processes with
const Service = "proxy"

var proxy = render.New(render.Options{
// Add template helpers here:
Helpers: render.Helpers{},
})

// App is where all routes and middleware for buffalo
// App is where all routes and middleware for the proxy
// should be defined. This is the nerve center of your
// application.
func App(conf *config.Config) (*buffalo.App, error) {
func App(conf *config.Config) (http.Handler, error) {
// ENV is used to help switch settings based on where the
// application is being run. Default is "development".
ENV := conf.GoEnv
Expand Down Expand Up @@ -62,30 +55,25 @@ func App(conf *config.Config) (*buffalo.App, error) {
}
lggr := log.New(conf.CloudRuntime, logLvl)

bLogLvl, err := logrus.ParseLevel(conf.BuffaloLogLevel)
if err != nil {
return nil, err
r := mux.NewRouter()
if conf.GoEnv == "development" {
r.Use(mw.RequestLogger)
}
blggr := log.Buffalo(bLogLvl)

app := buffalo.New(buffalo.Options{
Env: ENV,
SessionStore: sessions.Null{},
Logger: blggr,
Addr: conf.Port,
WorkerOff: true,
Host: "http://127.0.0.1" + conf.Port,
})

app.Use(mw.LogEntryMiddleware(lggr))
r.Use(mw.LogEntryMiddleware(lggr))
r.Use(secure.New(secure.Options{
SSLRedirect: conf.ForceSSL,
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
}).Handler)
r.Use(mw.ContentType)

var subRouter *mux.Router
if prefix := conf.PathPrefix; prefix != "" {
// certain Ingress Controllers (such as GCP Load Balancer)
// can not send custom headers and therefore if the proxy
// is running behind a prefix as well as some authentication
// mechanism, we should allow the plain / to return 200.
app.GET("/", healthHandler)
app = app.Group(prefix)
r.HandleFunc("/", healthHandler).Methods(http.MethodGet)
subRouter = r.PathPrefix(prefix).Subrouter()
}

// RegisterExporter will register an exporter where we will export our traces to.
Expand All @@ -104,52 +92,48 @@ func App(conf *config.Config) (*buffalo.App, error) {
lggr.Infof("%s", err)
} else {
defer flushTraces()
app.Use(observ.Tracer(Service))
}

// RegisterStatsExporter will register an exporter where we will collect our stats.
// The error from the RegisterStatsExporter would be nil if the proper stats exporter
// was specified by the user.
flushStats, err := observ.RegisterStatsExporter(app, conf.StatsExporter, Service)
flushStats, err := observ.RegisterStatsExporter(r, conf.StatsExporter, Service)
if err != nil {
lggr.Infof("%s", err)
} else {
defer flushStats()
app.Use(observ.StatsMiddleware())
}

// Automatically redirect to SSL
app.Use(forcessl.Middleware(secure.Options{
SSLRedirect: conf.ForceSSL,
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
}))

if ENV == "development" {
app.Use(paramlogger.ParameterLogger)
}

user, pass, ok := conf.BasicAuth()
if ok {
app.Use(basicAuth(user, pass))
r.Use(basicAuth(user, pass))
}

if !conf.FilterOff() {
mf, err := module.NewFilter(conf.FilterFile)
if err != nil {
lggr.Fatal(err)
}
app.Use(mw.NewFilterMiddleware(mf, conf.GlobalEndpoint))
r.Use(mw.NewFilterMiddleware(mf, conf.GlobalEndpoint))
}

// Having the hook set means we want to use it
if vHook := conf.ValidatorHook; vHook != "" {
app.Use(mw.NewValidationMiddleware(vHook))
r.Use(mw.NewValidationMiddleware(vHook))
}

if err := addProxyRoutes(app, store, lggr, conf.GoBinary, conf.GoGetWorkers, conf.ProtocolWorkers); err != nil {
proxyRouter := r
if subRouter != nil {
proxyRouter = subRouter
}
if err := addProxyRoutes(proxyRouter, store, lggr, conf.GoBinary, conf.GoGetWorkers, conf.ProtocolWorkers); err != nil {
err = fmt.Errorf("error adding proxy routes (%s)", err)
return nil, err
}

return app, nil
h := &ochttp.Handler{
Handler: r,
}

return h, nil
}
20 changes: 9 additions & 11 deletions cmd/proxy/actions/app_proxy.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
package actions

import (
"github.com/gobuffalo/buffalo"
"github.com/gomods/athens/pkg/download"
"github.com/gomods/athens/pkg/download/addons"
"github.com/gomods/athens/pkg/log"
"github.com/gomods/athens/pkg/module"
"github.com/gomods/athens/pkg/observ"
"github.com/gomods/athens/pkg/stash"
"github.com/gomods/athens/pkg/storage"
"github.com/gorilla/mux"
"github.com/spf13/afero"
)

func addProxyRoutes(
app *buffalo.App,
r *mux.Router,
s storage.Backend,
l *log.Logger,
goBin string,
goGetWorkers int,
goGetWorkers,
protocolWorkers int,
) error {
app.GET("/", proxyHomeHandler)
app.GET("/healthz", healthHandler)
app.GET("/readyz", getReadinessHandler(s))
app.GET("/version", versionHandler)
app.Middleware.Skip(observ.StatsMiddleware(), healthHandler, getReadinessHandler(s), versionHandler)
r.HandleFunc("/", proxyHomeHandler)
r.HandleFunc("/healthz", healthHandler)
r.HandleFunc("/readyz", getReadinessHandler(s))
r.HandleFunc("/version", versionHandler)

// Download Protocol
// the download.Protocol and the stash.Stasher interfaces are composable
Expand Down Expand Up @@ -64,8 +62,8 @@ func addProxyRoutes(
}
dp := download.New(dpOpts, addons.WithPool(protocolWorkers))

handlerOpts := &download.HandlerOpts{Protocol: dp, Logger: l, Engine: proxy}
download.RegisterHandlers(app, handlerOpts)
handlerOpts := &download.HandlerOpts{Protocol: dp, Logger: l}
download.RegisterHandlers(r, handlerOpts)

return nil
}
19 changes: 10 additions & 9 deletions cmd/proxy/actions/basicauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ import (
"crypto/subtle"
"net/http"

"github.com/gobuffalo/buffalo"
"github.com/gorilla/mux"
)

func basicAuth(user, pass string) buffalo.MiddlewareFunc {
return func(next buffalo.Handler) buffalo.Handler {
return func(c buffalo.Context) error {
if !checkAuth(c.Request(), user, pass) {
c.Response().Header().Set("WWW-Authenticate", `Basic realm="basic auth required"`)
c.Render(http.StatusUnauthorized, nil)
return nil
func basicAuth(user, pass string) mux.MiddlewareFunc {
return func(h http.Handler) http.Handler {
f := func(w http.ResponseWriter, r *http.Request) {
if !checkAuth(r, user, pass) {
w.Header().Set("WWW-Authenticate", `Basic realm="basic auth required"`)
w.WriteHeader(http.StatusUnauthorized)
return
}

return next(c)
h.ServeHTTP(w, r)
}
return http.HandlerFunc(f)
}
}

Expand Down
5 changes: 2 additions & 3 deletions cmd/proxy/actions/health.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package actions

import (
"github.com/gobuffalo/buffalo"
"net/http"
)

func healthHandler(c buffalo.Context) error {
return c.Render(http.StatusOK, nil)
func healthHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
5 changes: 2 additions & 3 deletions cmd/proxy/actions/home.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package actions

import (
"github.com/gobuffalo/buffalo"
"net/http"
)

func proxyHomeHandler(c buffalo.Context) error {
return c.Render(http.StatusOK, proxy.JSON("Welcome to The Athens Proxy"))
func proxyHomeHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`"Welcome to The Athens Proxy"`))
}
14 changes: 6 additions & 8 deletions cmd/proxy/actions/readiness.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package actions

import (
"github.com/gobuffalo/buffalo"
"github.com/gomods/athens/pkg/storage"
"net/http"

"github.com/gomods/athens/pkg/storage"
)

func getReadinessHandler(s storage.Backend) buffalo.Handler {
return func(c buffalo.Context) error {
if _, err := s.List(c, "github.com/gomods/athens"); err != nil {
return c.Render(http.StatusInternalServerError, nil)
func getReadinessHandler(s storage.Backend) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if _, err := s.List(r.Context(), "github.com/gomods/athens"); err != nil {
w.WriteHeader(http.StatusInternalServerError)
}

return c.Render(http.StatusOK, nil)
}
}
9 changes: 5 additions & 4 deletions cmd/proxy/actions/version.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package actions

import (
"github.com/gobuffalo/buffalo"
"github.com/gomods/athens/pkg/build"
"encoding/json"
"net/http"

"github.com/gomods/athens/pkg/build"
)

func versionHandler(c buffalo.Context) error {
return c.Render(http.StatusOK, proxy.JSON(build.Data()))
func versionHandler(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(build.Data())
}
Loading

0 comments on commit 5870aee

Please sign in to comment.