diff --git a/.buffalo.dev.yml b/.buffalo.dev.yml new file mode 100644 index 0000000..cf4e466 --- /dev/null +++ b/.buffalo.dev.yml @@ -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: simple-api-build +command_flags: [] +enable_colors: true +log_name: buffalo diff --git a/.codeclimate.yml b/.codeclimate.yml new file mode 100644 index 0000000..bef6900 --- /dev/null +++ b/.codeclimate.yml @@ -0,0 +1,23 @@ +version: "2" +checks: + method-lines: + config: + threshold: 100 +plugins: + fixme: + enabled: true + gofmt: + enabled: true + golint: + enabled: true + govet: + enabled: true +exclude_patterns: + - grifts/**/* + - "**/*_test.go" + - "*_test.go" + - "**_test.go" + - logs/* + - public/* + - templates/* + - "**/node_modules/" diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..af960a7 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +node_modules/ +*.log +bin/ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3e7fd5c --- /dev/null +++ b/.gitignore @@ -0,0 +1,27 @@ +vendor/ +**/*.log +**/*.sqlite +.idea/ +bin/ +tmp/ +node_modules/ +.sass-cache/ +*-packr.go +public/assets/ +.vscode/ +.grifter/ +.env +**/.DS_Store +*.pid +coverage +coverage.data +.svn +.console_history +.sass-cache/* +.jhw-cache/ +jhw.* +*.sublime* +dist/ +generated/ +.vendor/ + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..30c901c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,39 @@ +# 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.16.12 as builder + +ENV GO111MODULE on +ENV GOPROXY http://proxy.golang.org + +RUN mkdir -p /src/trying-buffalo +WORKDIR /src/trying-buffalo + +# Copy the Go Modules manifests +COPY go.mod go.mod +COPY go.sum go.sum +# cache deps before building and copying source so that we don't need to re-download as much +# and so that source changes don't invalidate our downloaded layer +RUN go mod download + +ADD . . +RUN buffalo build --static -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 . + +# Uncomment 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 + +# Uncomment to run the migrations before running the binary: +# CMD /bin/app migrate; /bin/app +CMD exec /bin/app diff --git a/README.md b/README.md new file mode 100644 index 0000000..9a31c8f --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# Welcome to Buffalo! + +Thank you for choosing Buffalo for your web development needs. + +## Starting the Application + +Buffalo ships with a command that will watch your application and automatically rebuild the Go binary and any assets for you. To do that run the "buffalo dev" command: + + $ buffalo dev + +If you point your browser to [http://127.0.0.1:3000](http://127.0.0.1:3000) you should see a "Welcome to Buffalo!" page. + +**Congratulations!** You now have your Buffalo application up and running. + +## What Next? + +We recommend you heading over to [http://gobuffalo.io](http://gobuffalo.io) and reviewing all of the great documentation there. + +Good luck! + +[Powered by Buffalo](http://gobuffalo.io) diff --git a/actions/actions_test.go b/actions/actions_test.go new file mode 100644 index 0000000..82bfe1e --- /dev/null +++ b/actions/actions_test.go @@ -0,0 +1,24 @@ +package actions + +import ( + "testing" + + "github.com/gobuffalo/packr/v2" + "github.com/gobuffalo/suite" +) + +type ActionSuite struct { + *suite.Action +} + +func Test_ActionSuite(t *testing.T) { + action, err := suite.NewActionWithFixtures(App(), packr.New("Test_ActionSuite", "../fixtures")) + if err != nil { + t.Fatal(err) + } + + as := &ActionSuite{ + Action: action, + } + suite.Run(t, as) +} diff --git a/actions/app.go b/actions/app.go new file mode 100644 index 0000000..8688983 --- /dev/null +++ b/actions/app.go @@ -0,0 +1,69 @@ +package actions + +import ( + "github.com/gobuffalo/buffalo" + "github.com/gobuffalo/envy" + forcessl "github.com/gobuffalo/mw-forcessl" + paramlogger "github.com/gobuffalo/mw-paramlogger" + "github.com/unrolled/secure" + + contenttype "github.com/gobuffalo/mw-contenttype" + "github.com/gobuffalo/x/sessions" + "github.com/rs/cors" +) + +// 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 + +// App is where all routes and middleware for buffalo +// should be defined. This is the nerve center of your +// application. +// +// Routing, middleware, groups, etc... are declared TOP -> DOWN. +// This means if you add a middleware to `app` *after* declaring a +// group, that group will NOT have that new middleware. The same +// is true of resource declarations as well. +// +// It also means that routes are checked in the order they are declared. +// `ServeFiles` is a CATCH-ALL route, so it should always be +// placed last in the route declarations, as it will prevent routes +// declared after it to never be called. +func App() *buffalo.App { + if app == nil { + app = buffalo.New(buffalo.Options{ + Env: ENV, + SessionStore: sessions.Null{}, + PreWares: []buffalo.PreWare{ + cors.Default().Handler, + }, + SessionName: "_simple_api_session", + }) + + // Automatically redirect to SSL + app.Use(forceSSL()) + + // Log request parameters (filters apply). + app.Use(paramlogger.ParameterLogger) + + // Set the request content type to JSON + app.Use(contenttype.Set("application/json")) + + app.GET("/", HomeHandler) + } + + return app +} + +// forceSSL will return a middleware that will redirect an incoming request +// if it is not HTTPS. "http://example.com" => "https://example.com". +// This middleware does **not** enable SSL. for your application. To do that +// we recommend using a proxy: https://gobuffalo.io/en/docs/proxy +// for more information: https://github.com/unrolled/secure/ +func forceSSL() buffalo.MiddlewareFunc { + return forcessl.Middleware(secure.Options{ + SSLRedirect: ENV == "production", + SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"}, + }) +} diff --git a/actions/home.go b/actions/home.go new file mode 100644 index 0000000..06a2bda --- /dev/null +++ b/actions/home.go @@ -0,0 +1,13 @@ +package actions + +import ( + "net/http" + + "github.com/gobuffalo/buffalo" +) + +// HomeHandler is a default handler to serve up +// a home page. +func HomeHandler(c buffalo.Context) error { + return c.Render(http.StatusOK, r.JSON(map[string]string{"message": "Welcome to Buffalo!"})) +} diff --git a/actions/home_test.go b/actions/home_test.go new file mode 100644 index 0000000..d5cf583 --- /dev/null +++ b/actions/home_test.go @@ -0,0 +1,10 @@ +package actions + +import "net/http" + +func (as *ActionSuite) Test_HomeHandler() { + res := as.JSON("/").Get() + + as.Equal(http.StatusOK, res.Code) + as.Contains(res.Body.String(), "Welcome to Buffalo") +} diff --git a/actions/render.go b/actions/render.go new file mode 100644 index 0000000..a44df05 --- /dev/null +++ b/actions/render.go @@ -0,0 +1,13 @@ +package actions + +import ( + "github.com/gobuffalo/buffalo/render" +) + +var r *render.Engine + +func init() { + r = render.New(render.Options{ + DefaultContentType: "application/json", + }) +} diff --git a/config/buffalo-app.toml b/config/buffalo-app.toml new file mode 100644 index 0000000..d5e5420 --- /dev/null +++ b/config/buffalo-app.toml @@ -0,0 +1,13 @@ +name = "simple-api" +bin = "bin\\simple-api" +vcs = "git" +with_pop = false +with_sqlite = false +with_dep = false +with_webpack = false +with_nodejs = false +with_yarn = false +with_docker = true +with_grifts = true +as_web = false +as_api = true diff --git a/config/buffalo-plugins.toml b/config/buffalo-plugins.toml new file mode 100644 index 0000000..e69de29 diff --git a/fixtures/sample.toml b/fixtures/sample.toml new file mode 100644 index 0000000..b9281d4 --- /dev/null +++ b/fixtures/sample.toml @@ -0,0 +1,32 @@ +[[scenario]] +name = "lots of widgets" + + [[scenario.table]] + name = "widgets" + + [[scenario.table.row]] + id = "<%= uuidNamed("widget") %>" + name = "This is widget #1" + body = "some widget body" + created_at = "<%= now() %>" + updated_at = "<%= now() %>" + + [[scenario.table.row]] + id = "<%= uuid() %>" + name = "This is widget #2" + body = "some widget body" + created_at = "<%= now() %>" + updated_at = "<%= now() %>" + + [[scenario.table]] + name = "users" + + [[scenario.table.row]] + id = "<%= uuid() %>" + name = "Mark Bates" + admin = true + age = 41 + widget_id = "<%= uuidNamed("widget") %>" + created_at = "<%= now() %>" + updated_at = "<%= now() %>" + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..6881bba --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module simple_api + +go 1.14 diff --git a/grifts/init.go b/grifts/init.go new file mode 100644 index 0000000..f15408c --- /dev/null +++ b/grifts/init.go @@ -0,0 +1,11 @@ +package grifts + +import ( + "simple_api/actions" + + "github.com/gobuffalo/buffalo" +) + +func init() { + buffalo.Grifts(actions.App()) +} diff --git a/inflections.json b/inflections.json new file mode 100644 index 0000000..ef0214f --- /dev/null +++ b/inflections.json @@ -0,0 +1,3 @@ +{ + "singular": "plural" +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..e7272f0 --- /dev/null +++ b/main.go @@ -0,0 +1,38 @@ +package main + +import ( + "log" + + "simple_api/actions" +) + +// main is the starting point for your Buffalo application. +// You can feel free and add to this `main` method, change +// what it does, etc... +// All we ask is that, at some point, you make sure to +// call `app.Serve()`, unless you don't want to start your +// application that is. :) +func main() { + app := actions.App() + if err := app.Serve(); err != nil { + log.Fatal(err) + } +} + +/* +# Notes about `main.go` + +## SSL Support + +We recommend placing your application behind a proxy, such as +Apache or Nginx and letting them do the SSL heavy lifting +for you. https://gobuffalo.io/en/docs/proxy + +## Buffalo Build + +When `buffalo build` is run to compile your binary, this `main` +function will be at the heart of that binary. It is expected +that your `main` function will start your application using +the `app.Serve()` method. + +*/