Skip to content

Commit

Permalink
Proposal: move buffalo app to cmd (gomods#91)
Browse files Browse the repository at this point in the history
* buffalo app moved to cmd

* ooops:

* added copy of proxy as olympus

* removed buffalo test

* added blank endpoint for feed

* forgot db config file and storage setup

* resolved comments

* newlines

* resolved conficts
  • Loading branch information
michalpristas authored and bketelsen committed Mar 28, 2018
1 parent e76125d commit 9c14b1f
Show file tree
Hide file tree
Showing 239 changed files with 13,791 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
build:
buffalo build
cd cmd/proxy && buffalo build

run: build
./athens
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Download
binary into your PATH, and then run the following from the root of this repository:

```console
cd cmd/proxy
buffalo dev
```

Expand Down
3 changes: 2 additions & 1 deletion .babelrc → cmd/olympus/.babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"presets": ["env"]
}
}

21 changes: 21 additions & 0 deletions cmd/olympus/.buffalo.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
app_root: .
ignored_folders:
- vendor
- log
- logs
- assets
- public
- grifts
- tmp
- bin
- node_modules
- .sass-cache
included_extensions:
- .go
- .env
build_path: tmp
build_delay: 200ns
binary_name: olympus-build
command_flags: []
enable_colors: true
log_name: buffalo
36 changes: 36 additions & 0 deletions cmd/olympus/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This is a multi-stage Dockerfile and requires >= Docker 17.05
# https://docs.docker.com/engine/userguide/eng-image/multistage-build/
FROM gobuffalo/buffalo:v0.11.0 as builder

RUN mkdir -p $GOPATH/src/github.com/gomods/athens/cmd/olympus
WORKDIR $GOPATH/src/github.com/gomods/athens/cmd/olympus

# this will cache the npm install step, unless package.json changes
ADD cmd/olympus/package.json .
ADD cmd/olympus/yarn.lock .
RUN yarn install --no-progress

WORKDIR $GOPATH/src/github.com/gomods/athens

ADD . .
RUN cd cmd/olympus && buffalo build -s -o /bin/app

FROM alpine
RUN apk add --no-cache bash
RUN apk add --no-cache ca-certificates

WORKDIR /bin/

COPY --from=builder /bin/app .

# Comment out to run the binary in "production" mode:
# ENV GO_ENV=production

# Bind the app to 0.0.0.0 so it can be seen from outside the container
ENV ADDR=0.0.0.0

EXPOSE 3000

# Comment out to run the migrations before running the binary:
# CMD /bin/app migrate; /bin/app
CMD exec /bin/app
File renamed without changes.
78 changes: 78 additions & 0 deletions cmd/olympus/actions/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package actions

import (
"log"

"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/buffalo/middleware"
"github.com/gobuffalo/buffalo/middleware/ssl"
"github.com/gobuffalo/envy"
"github.com/rs/cors"
"github.com/unrolled/secure"

"github.com/gobuffalo/buffalo/middleware/csrf"
"github.com/gobuffalo/buffalo/middleware/i18n"
"github.com/gobuffalo/packr"
)

// ENV is used to help switch settings based on where the
// application is being run. Default is "development".
var ENV = envy.Get("GO_ENV", "development")
var app *buffalo.App

// T is buffalo Translator
var T *i18n.Translator

// App is where all routes and middleware for buffalo
// should be defined. This is the nerve center of your
// application.
func App() *buffalo.App {
if app == nil {
app = buffalo.New(buffalo.Options{
Env: ENV,
PreWares: []buffalo.PreWare{
cors.Default().Handler,
},
SessionName: "_olympus_session",
})
// Automatically redirect to SSL
app.Use(ssl.ForceSSL(secure.Options{
SSLRedirect: ENV == "production",
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
}))

if ENV == "development" {
app.Use(middleware.ParameterLogger)
}
initializeTracing(app)
// Protect against CSRF attacks. https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)
// Remove to disable this.
csrfMiddleware := csrf.New
app.Use(csrfMiddleware)

// Wraps each request in a transaction.
// c.Value("tx").(*pop.PopTransaction)
// Remove to disable this.
// app.Use(middleware.PopTransaction(models.DB))

// Setup and use translations:
var err error
if T, err = i18n.New(packr.NewBox("../locales"), "en-US"); err != nil {
app.Stop(err)
}
app.Use(T.Middleware())

storage, err := newStorage()
if err != nil {
log.Fatalf("error creating storage (%s)", err)
return nil
}

app.GET("/", homeHandler)
app.GET("/feed/{syncpoint:.*}", feedHandler(storage))

app.ServeFiles("/", assetsBox) // serve files from the public directory
}

return app
}
20 changes: 20 additions & 0 deletions cmd/olympus/actions/feed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package actions

import (
"net/http"

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

func feedHandler(s storage.Backend) func(c buffalo.Context) error {
return func(c buffalo.Context) error {
if _, err := getSyncPoint(c); err != nil {
return err
}

feed := make(map[string][]string)

return c.Render(http.StatusOK, olympus.JSON(feed))
}
}
9 changes: 9 additions & 0 deletions cmd/olympus/actions/home.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package actions

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

func homeHandler(c buffalo.Context) error {
return c.Render(200, olympus.HTML("index.html"))
}
13 changes: 13 additions & 0 deletions cmd/olympus/actions/path_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package actions

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

func getSyncPoint(c buffalo.Context) (string, error) {
syncpoint := c.Param("syncpoint")
if syncpoint == "" {
return "", nil
}
return syncpoint, nil
}
24 changes: 24 additions & 0 deletions cmd/olympus/actions/render.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package actions

import (
"github.com/gobuffalo/buffalo/render"
"github.com/gobuffalo/packr"
)

var olympus *render.Engine
var assetsBox = packr.NewBox("../public")

func init() {
olympus = render.New(render.Options{
// HTML layout to be used for all HTML requests:
HTMLLayout: "application.html",
JavaScriptLayout: "application.js",

// Box containing all of the templates:
TemplatesBox: packr.NewBox("../templates/olympus"),
AssetsBox: assetsBox,

// Add template helpers here:
Helpers: render.Helpers{},
})
}
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.
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
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
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
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
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
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
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
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
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
6 changes: 3 additions & 3 deletions database.yml → cmd/olympus/database.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
development:
dialect: "mysql"
database: vgoprox
database: olympusdb
host: 127.0.0.1
port: 3307
user: vgp
password: vgp

test:
dialect: "mysql"
database: vgoprox
database: olympusdb
host: 127.0.0.1
port: 3306
user: vgp
password: vgp

production:
dialect: "mysql"
database: vgoprox
database: olympusdb
host: {{ env "DB_HOST" }}
port: {{ env "DB_PORT" }}
user: {{ env "DB_USER" }}
Expand Down
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions cmd/olympus/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"log"

"github.com/gomods/athens/cmd/olympus/actions"
)

func main() {
app := actions.App()
if err := app.Serve(); err != nil {
log.Fatal(err)
}
}
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.
6 changes: 6 additions & 0 deletions cmd/olympus/templates/olympus/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="row mt-5">
<div class="col text-center">
<h1>Welcome to GopherPacks.io</h1>
<p> This is where gods resides </p>
</div>
</div>
Binary file added cmd/olympus/tmp/athens-build
Binary file not shown.
Binary file added cmd/olympus/tmp/olympus-build
Binary file not shown.
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions cmd/proxy/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["env"]
}

File renamed without changes.
13 changes: 8 additions & 5 deletions Dockerfile → cmd/proxy/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
# https://docs.docker.com/engine/userguide/eng-image/multistage-build/
FROM gobuffalo/buffalo:v0.11.0 as builder

RUN mkdir -p $GOPATH/src/github.com/gomods/athens
WORKDIR $GOPATH/src/github.com/gomods/athens
RUN mkdir -p $GOPATH/src/github.com/gomods/athens/cmd/proxy
WORKDIR $GOPATH/src/github.com/gomods/athens/cmd/proxy

# this will cache the npm install step, unless package.json changes
ADD package.json .
ADD yarn.lock .
ADD cmd/proxy/package.json .
ADD cmd/proxy/yarn.lock .
RUN yarn install --no-progress

WORKDIR $GOPATH/src/github.com/gomods/athens

ADD . .
RUN buffalo build --static -o /bin/app
RUN cd cmd/proxy && buffalo build -s -o /bin/app

FROM alpine
RUN apk add --no-cache bash
Expand Down
16 changes: 16 additions & 0 deletions cmd/proxy/actions/actions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package actions

import (
"testing"

"github.com/gobuffalo/suite"
)

type ActionSuite struct {
*suite.Action
}

func Test_ActionSuite(t *testing.T) {
as := &ActionSuite{suite.NewAction(App())}
suite.Run(t, as)
}
30 changes: 29 additions & 1 deletion actions/app.go → cmd/proxy/actions/app.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package actions

import (
"fmt"
"log"

// "github.com/gomods/athens/models"
Expand All @@ -11,6 +12,7 @@ import (
"github.com/gobuffalo/buffalo/middleware/ssl"
"github.com/gobuffalo/envy"
"github.com/gobuffalo/packr"
"github.com/gomods/athens/pkg/storage"
"github.com/gomods/athens/pkg/user"
"github.com/rs/cors"
"github.com/unrolled/secure"
Expand Down Expand Up @@ -83,7 +85,12 @@ func App() *buffalo.App {

if MODE == "proxy" {
log.Printf("starting athens in proxy mode")
if err := addProxyRoutes(app); err != nil {
store, err := getStorage()
if err != nil {
log.Fatalf("error getting storage configuration (%s)", err)
return nil
}
if err := addProxyRoutes(app, store); err != nil {
log.Fatalf("error adding proxy routes (%s)", err)
return nil
}
Expand All @@ -106,3 +113,24 @@ func App() *buffalo.App {

return app
}

func getStorage() (storage.Backend, error) {
storageType := envy.Get("ATHENS_STORAGE_TYPE", "memory")
var storageRoot string
var err error

switch storageType {
case "mongo":
storageRoot, err = envy.MustGet("ATHENS_MONGO_STORAGE_URL")
if err != nil {
return nil, fmt.Errorf("missing mongo URL (%s)", err)
}
case "disk":
storageRoot, err = envy.MustGet("ATHENS_DISK_STORAGE_ROOT")
if err != nil {
return nil, fmt.Errorf("missing disk storage root (%s)", err)
}
}

return newStorage(storageType, storageRoot)
}
8 changes: 2 additions & 6 deletions actions/app_proxy.go → cmd/proxy/actions/app_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ package actions

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

func addProxyRoutes(app *buffalo.App) error {
storage, err := newStorage()
if err != nil {
return err
}

func addProxyRoutes(app *buffalo.App, storage storage.Backend) error {
app.GET("/", proxyHomeHandler)
app.GET("/{module:.+}/@v/list", listHandler(storage))
app.GET("/{module:.+}/@v/{version}.info", versionInfoHandler(storage))
Expand Down
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 9c14b1f

Please sign in to comment.