Learning Go by building a simple blog APIs with GoLang, Echo
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
choco install make
#Install go-migrate cli
On Windows, you can install the cli using scoop
irm get.scoop.sh | iex
scoop install go-migrate
On Mac, you can install the cli using brew
brew install golang-migrate
On Linux, you can install the cli using apt
$ curl -L https://packagecloud.io/golang-migrate/migrate/gpgkey | apt-key add -
$ echo "deb https://packagecloud.io/golang-migrate/migrate/ubuntu/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/migrate.list
$ apt-get update
$ apt-get install -y migrate
Run postgres on docker
make postgres
Create database
make createdb
Migrate database to the latest version
make migrateup
If you want to migrate to a go back one version, use this command
make migratedown
Just in case you want to drop the database because it is dirty or something
make dropdb
To have more flexibility, you can use the migrate cli directly
go run github.com/tumivn/goblog/cmd/api
go build -a -installsuffix cgo -ldflags "-extldflags -static" -tags musl go build -a -installsuffix cgo -ldflags "-extldflags -static" -tags musl github.com/tumivn/goblog/cmd/api
docker build -t tumivn/tumivn:goblog-v01 .
docker run --rm -p 8080:8080 --link postgres:postgres --name legangs-auth -e DB_HOST=postgres -e DB_PORT=5432 -e DB_USER=postgres -e DB_PASSWORD=docker -e DB_NAME=goblog -e PORT=8080 -e JWT_SECRET=my_secret_key tuminv/tumivn:auth-v01
Using the golang-migrate/migrate library apply for database migration
Create seperated handler and entry point to run migration.
Create a migration files
$ migrate create -ext sql -dir internal/storage/migrations/ -seq migration_name