diff --git a/README.md b/README.md index 9193e9e..dc126d1 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# goxygen [![build](https://github.com/Shpota/goxygen/workflows/build/badge.svg)](https://github.com/Shpota/goxygen/actions?query=workflow%3Abuild) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://github.com/Shpota/goxygen/pulls) +# goxygen [![build](https://github.com/Shpota/goxygen/workflows/build/badge.svg)](https://github.com/Shpota/goxygen/actions?query=workflow%3Abuild) [![](https://badges.gitter.im/goxygen/community.svg)](https://gitter.im/goxygen/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://github.com/Shpota/goxygen/pulls) goxygen logo @@ -6,19 +6,21 @@ Goxygen aims at saving your time while setting up a new project. It creates a skeleton of an application with all configuration done for -you. You can start implementing your business logic strait away. +you. You can start implementing your business logic straight away. Goxygen generates back end Go code, connects it with front end React components, provides a Dockerfile for the application and creates docker-compose files for convenient run in development and production environments. ## How to use +You need to have Go 1.11 or newer on your machine. ```go go get -u github.com/shpota/goxygen go run github.com/shpota/goxygen init my-app ``` -This creates a new `my-app` project folder. The generated project -is ready to run with `docker-compose`: +This generates a project in `my-app` folder. + +The generated project is ready to run with `docker-compose`: ```sh cd my-app docker-compose up @@ -26,6 +28,9 @@ docker-compose up After the build is completed, the application is accessible on http://localhost:8080. +You can find more details on how to work with the generated +project in its readme file. + ![Showcase](.github/showcase.gif) ## Structure of a generated project @@ -55,28 +60,17 @@ on http://localhost:8080. ├── .gitignore └── README.md # guide on how to use the generated repo -Files such as unit tests or sample components are not reflected here +Files such as unit tests or sample components are not included here for simplicity. -## Toolchain - -You need to have [Go](https://golang.org/) installed to generate -a project with Goxygen. - -In order to work with the generated project you need to have -[Go](https://golang.org/), [Node.js](https://nodejs.org/), -[Docker](https://www.docker.com/), -and [Docker Compose](https://docs.docker.com/compose/) -(comes pre-installed with Docker on Mac and Windows). +## Dependencies -Verify the toolchain by running the following commands: - -```sh -go version -npm --version -docker --version -docker-compose --version -``` +Goxygen generates a basic structure of a project and doesn't force you +to use a specific set of tools. That's why it doesn't bring unneeded +dependencies to your project. The only two dependencies are +[mongo-go-driver](https://github.com/mongodb/mongo-go-driver) on the +back end side and [axios](https://github.com/axios/axios) on the front +end side. ## How to contribute @@ -88,6 +82,6 @@ You can also propose your changes via a Pull Request. Fork the repository, make changes, send us a pull request and we'll review it shortly. -### Logo +### Credits Goxygen's logo was created by [Egon Elbre](https://twitter.com/egonelbre). diff --git a/static/generated.go b/static/generated.go index 6367054..d71fc84 100644 --- a/static/generated.go +++ b/static/generated.go @@ -24,12 +24,12 @@ webapp/node_modules webapp/build webapp/npm-debug.log* `, - "Dockerfile": `FROM node:12.15 AS JS_BUILD + "Dockerfile": `FROM node:12.16 AS JS_BUILD COPY webapp /webapp WORKDIR webapp RUN npm install && npm run build --prod -FROM golang:1.13.7-alpine AS GO_BUILD +FROM golang:1.13.8-alpine AS GO_BUILD RUN apk add build-base COPY server /server WORKDIR /server @@ -60,6 +60,10 @@ docker --version docker-compose --version ` + "`" + `` + "`" + `` + "`" + ` +If you are using Windows you will also need +[gcc](https://gcc.gnu.org/). It comes installed +on Mac and almost all Linux distributions. + ## Start in development mode In the project directory run the command (you might @@ -102,7 +106,7 @@ its database. Access the application on http://localhost:8080. "docker-compose-dev.yml": `version: "3.7" services: dev_db: - image: mongo:4.2.2 + image: mongo:4.2.3 environment: MONGO_INITDB_DATABASE: tech ports: @@ -122,7 +126,7 @@ services: environment: profile: prod db: - image: mongo:4.2.2 + image: mongo:4.2.3 container_name: db environment: MONGO_INITDB_DATABASE: tech @@ -184,10 +188,7 @@ func (m MongoDB) GetTechnologies() ([]*model.Technology, error) { go 1.13 -require ( - github.com/gorilla/mux v1.7.3 - go.mongodb.org/mongo-driver v1.2.1 -) +require go.mongodb.org/mongo-driver v1.3.0 `, "server/model/technology.go": `package model @@ -236,34 +237,36 @@ func clientOptions() *options.ClientOptions { import ( "encoding/json" - "github.com/gorilla/mux" "log" "net/http" "project-name/db" ) type App struct { - d db.DB - r *mux.Router + d db.DB + handlers map[string]http.HandlerFunc } func NewApp(d db.DB, cors bool) App { app := App{ - d: d, - r: mux.NewRouter(), + d: d, + handlers: make(map[string]http.HandlerFunc), } - handler := app.GetTechnologies + techHandler := app.GetTechnologies if !cors { - handler = disableCors(handler) + techHandler = disableCors(techHandler) } - app.r.HandleFunc("/api/technologies", handler).Methods("GET") - app.r.PathPrefix("/").Handler(http.FileServer(http.Dir("/webapp"))) + app.handlers["/api/technologies"] = techHandler + app.handlers["/"] = http.FileServer(http.Dir("/webapp")).ServeHTTP return app } func (a *App) Serve() error { + for path, handler := range a.handlers { + http.Handle(path, handler) + } log.Println("Web server is available on port 8080") - return http.ListenAndServe(":8080", a.r) + return http.ListenAndServe(":8080", nil) } func (a *App) GetTechnologies(w http.ResponseWriter, r *http.Request) { diff --git a/templates/Dockerfile b/templates/Dockerfile index c59fc44..b83f1b7 100644 --- a/templates/Dockerfile +++ b/templates/Dockerfile @@ -1,9 +1,9 @@ -FROM node:12.15 AS JS_BUILD +FROM node:12.16 AS JS_BUILD COPY webapp /webapp WORKDIR webapp RUN npm install && npm run build --prod -FROM golang:1.13.7-alpine AS GO_BUILD +FROM golang:1.13.8-alpine AS GO_BUILD RUN apk add build-base COPY server /server WORKDIR /server diff --git a/templates/README.md b/templates/README.md index a7f4071..efba5ab 100644 --- a/templates/README.md +++ b/templates/README.md @@ -18,6 +18,10 @@ docker --version docker-compose --version ``` +If you are using Windows you will also need +[gcc](https://gcc.gnu.org/). It comes installed +on Mac and almost all Linux distributions. + ## Start in development mode In the project directory run the command (you might diff --git a/templates/docker-compose-dev.yml b/templates/docker-compose-dev.yml index ddd17ec..534d9e9 100644 --- a/templates/docker-compose-dev.yml +++ b/templates/docker-compose-dev.yml @@ -1,7 +1,7 @@ version: "3.7" services: dev_db: - image: mongo:4.2.2 + image: mongo:4.2.3 environment: MONGO_INITDB_DATABASE: tech ports: diff --git a/templates/docker-compose.yml b/templates/docker-compose.yml index df62694..f627df0 100644 --- a/templates/docker-compose.yml +++ b/templates/docker-compose.yml @@ -10,7 +10,7 @@ services: environment: profile: prod db: - image: mongo:4.2.2 + image: mongo:4.2.3 container_name: db environment: MONGO_INITDB_DATABASE: tech diff --git a/templates/server/go.mod b/templates/server/go.mod index 3fb0a31..fdbb2a4 100644 --- a/templates/server/go.mod +++ b/templates/server/go.mod @@ -2,7 +2,4 @@ module project-name go 1.13 -require ( - github.com/gorilla/mux v1.7.3 - go.mongodb.org/mongo-driver v1.2.1 -) +require go.mongodb.org/mongo-driver v1.3.0 diff --git a/templates/server/web/app.go b/templates/server/web/app.go index e77d40e..e2816c1 100644 --- a/templates/server/web/app.go +++ b/templates/server/web/app.go @@ -2,34 +2,36 @@ package web import ( "encoding/json" - "github.com/gorilla/mux" "log" "net/http" "project-name/db" ) type App struct { - d db.DB - r *mux.Router + d db.DB + handlers map[string]http.HandlerFunc } func NewApp(d db.DB, cors bool) App { app := App{ - d: d, - r: mux.NewRouter(), + d: d, + handlers: make(map[string]http.HandlerFunc), } - handler := app.GetTechnologies + techHandler := app.GetTechnologies if !cors { - handler = disableCors(handler) + techHandler = disableCors(techHandler) } - app.r.HandleFunc("/api/technologies", handler).Methods("GET") - app.r.PathPrefix("/").Handler(http.FileServer(http.Dir("/webapp"))) + app.handlers["/api/technologies"] = techHandler + app.handlers["/"] = http.FileServer(http.Dir("/webapp")).ServeHTTP return app } func (a *App) Serve() error { + for path, handler := range a.handlers { + http.Handle(path, handler) + } log.Println("Web server is available on port 8080") - return http.ListenAndServe(":8080", a.r) + return http.ListenAndServe(":8080", nil) } func (a *App) GetTechnologies(w http.ResponseWriter, r *http.Request) {