Code Monkey home page Code Monkey logo

gdrp's Introduction

GDRP: Go-Docker-React-Postgres

A Template Stack. Use this if you want to spend less time doing infra stuff and more time to actually developing stuff.

What's in this template?

  • Backend API server (Golang).
  • Frontend server (ReactJS).
  • Database (PostgreSQL).
  • Tunneling (using localtunnel.me).

Setting Up

Run

First time running? Use npm install (in ./frontend folder) and use docker-compose up --build.
If not, use docker-compose up.

Or you can use make start. This will not make docker to rebuild if you made changes to the Dockerfile(s), though.

NOTE: You need to rebuild if you made some changes to the Dockerfile(s). However, I'm pretty sure you know this if you are planning to make changes to those file(s).

After the servers are up and running, you can access your web directly in your browser. Look for the url in your terminal.

# This is your API server link (if subdomain is available)
server-backend_1   | your url is: https://[API_SUBDOMAIN].loca.lt
# This is your WEB server link (if subdomain is available). Open this in your browser.
server-frontend_1  | your url is: https://[WEB_SUBDOMAIN].loca.lt

Cheat Sheet

This section contains some commands and examples that you might need. I have made a Makefile to make things easier. Check it out!

Useful Commands

Below are some useful commands that you might need.

Prettify Codes

In frontend you can use this command (make sure to cd to ./frontend first):

npm run prettify

In back end you can use this command (make sure to cd to ./backend first):

go fmt *.go
go fmt main         # 'main' is a package name

If you are using VSCode and you have already installed the Go extension, This is not needed because the extension will prettify your code everytime you save.

Using Makefile Macros

make generate-migration name=AddUserTable
make run-migration-up database=${POSTGRESQL_URL}
make run-migration-down database='postgres://root:root@localhost:5432/app-db?sslmode=disable'

Export POSTGRESQL_URL Environment Variable

export POSTGRESQL_URL='postgres://root:root@localhost:5432/app-db?sslmode=disable'

Not Working? Try these steps:

  • Open ~/.bashrc using nano or your favorite text editor.
  • Scroll to the very bottom.
  • Copy the command above and paste it there.
  • Save and exit.
  • source ~/.bashrc or log out and log back in.

Create Migration

migrate create -ext sql -dir ./backend/database/migrations -seq create_users_table

Run "Up" Migration

migrate -database ${POSTGRESQL_URL} -path ./backend/database/migrations up

Run "Down" Migration

migrate -database ${POSTGRESQL_URL} -path ./backend/database/migrations down

Useful Examples

Below are some useful examples that you might need.

Database Transactions (SQL)

What is a transaction? Find out here.

BEGIN;

CREATE TYPE enum_mood AS ENUM (
	'happy',
	'sad',
	'neutral'
);
ALTER TABLE users ADD COLUMN mood enum_mood;

COMMIT;
BEGIN;

ALTER TABLE users DROP COLUMN mood;
DROP TYPE enum_mood;

COMMIT;

Run Migration From Go File(s)

import (
	"log"

	"github.com/golang-migrate/migrate/v4"
	_ "github.com/golang-migrate/migrate/v4/database/postgres"
	_ "github.com/golang-migrate/migrate/v4/source/file"
)

func main() {
	m, err := migrate.New(
		"file://db/migrations",
		"postgres://postgres:postgres@localhost:5432/example?sslmode=disable")
	if err != nil {
		log.Fatal(err)
	}
	if err := m.Up(); err != nil {
		log.Fatal(err)
	}
}

Useful Tools

Below are some useful tools that you can use to develop your app.

Adminer

This is a database admin panel (similar to phpmyadmin). To access:

  • Open up your browser.
  • http://localhost:3645. Or you can click here.

ReqBin

This is a free API testing tools to test out your endpoints. To access:

Errors

In this section I will list out some errors that I encountered and provide solutions for that problem.

I/O Timeout when trying to go get in Docker

The error will look like this:

go: github.com/go-pg/pg/[email protected]: Get "https://proxy.golang.org/github.com/go-pg/pg/v10/@v/v10.9.1.mod": dial tcp: lookup proxy.golang.org on 172.17.254.1:53: read udp 172.17.0.2:45074->172.17.254.1:53: i/o timeout

To solve:

  • nano /etc/resolv.conf
  • Change all nameserver to 8.8.8.8 and 8.8.4.4
  • Your resolv.conf should looks like this (more or less)
nameserver 8.8.8.8
nameserver 8.8.4.4

CORS Problem when trying to call API

When you call an endpoint from the frontend, this error will appear. The error will look something like this (browser console):

Access to fetch at 'http://localhost:5000/users' from origin 'http://localhost:3000' has been blocked by CORS policy: Request header field access-control-allow-origin is not allowed by Access-Control-Allow-Headers in preflight response.

To solve:

  • Install Moesif in Google Chrome.
  • Turn the extension on
  • Refresh the React page.

gdrp's People

Contributors

acailuv avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.