I drafted this tutorial on my old blog but as I have mentioned, it is terminated and… Goodbye. Fortunately, I keep this step by step in my *private* repository readme. It’s kinda short since I was documenting each step quickly. Please inform me if the step is incomplete as I just copied it and haven’t check it again.
The scope of this tutorial is making REST API with Go, PostgreSQL and host it in Heroku.
Don’t forget to set GOPATH:
$GOPATH: C:/Go or /home/gopkg
Make $GOPATH/src/ternaku-api main.go
package main
import (
"log"
"os"
"net/http"
"github.com/gin-gonic/gin"
)
func Index(c **gin.Context) {
c.String(http.StatusOK, "Ternaku API (c) Mobile AppDev Ristek 2016")
}
func main() {
port := os.Getenv("PORT")
if port == "" {
log.Fatal("$PORT not set")
}
router := gin.New()
router.Use(gin.Logger())
router.GET("/", Index)
router.Run(":" + port)
}
run:
$ go install ./...
$ PORT=5000 $GOPATH/bin/ternaku-api
we’ll add it to git:
$ git commit
$ git add -A
$ git commit -m "initial commit"
Because I want to push this to my repo (oops sorry it’s private),
$ git remote add origin https://gitlab.com/pnteresa/ternaku-api.git
$ git push origin master
Heroku needs Procfile. Create Procfile file:
Procfile
web: ternaku-api
Add to git
$ git add Procfile
$ git commit -m "added Procfile"
Install Godep (Heroku needs it too zzzz)
$ go get -u github.com/tools/godep
Everytime we add dependencies, don’t forget to:
$ godep save -r ./...
(do it now, btw)
If godep not found:
export GOBIN=$GOPATH/bin
export PATH=$GOPATH:$GOBIN:$PATH
godep save -r ./...
Add to git
$ git add -A Godeps
$ git commit -m Godeps
Now let’s create the Heroku app!
$ heroku create -b https://github.com/heroku/heroku-buildpack-go.git
Creating app... done, stack is cedar-14
Setting buildpack to https://github.com/heroku/heroku-buildpack-go.git... done
https://evening-hamlet-19910.herokuapp.com/ | https://git.heroku.com/evening-hamlet-19910.git
Push push push to heroku
$ git push heroku master
Counting objects: 103, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (94/94), done.
Writing objects: 100% (103/103), 171.31 KiB | 0 bytes/s, done.
Total 103 (delta 7), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Fetching set buildpack https://github.com/heroku/heroku-buildpack-go.git... done
remote: -----> Go app detected
remote: -----> Checking Godeps/Godeps.json file.
remote: -----> Installing go1.5.3... done
remote: -----> Running: godep go install -tags heroku ./...
remote:
remote: -----> Discovering process types
remote: Procfile declares types -> (none)
remote:
remote: -----> Compressing...
remote: Done: 2.9M
remote: -----> Launching...
remote: Released v3
remote: https://evening-hamlet-19910.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy.... done.
To https://git.heroku.com/evening-hamlet-19910.git
** [new branch] master -> master
If you get Application Error and heroku logs --tail
shows No web processes running
, run:
heroku scale web=1
lol.
We’ll try to make a Ternak & Ternaks class (omg what’s the name, this is Java lol)
ternak.go
package main
import (
"time"
)
type Ternak struct {
Id int `json:"id"`
IdJenis int `json:"id_jenis"`
IdPeternak int `json:"id_peternak"`
Bobot int `json:"bobot"`
TanggalLahir time.Time `json:"tanggal_lahir"`
IsJantan bool `json:"is_jantan"`
Harga int `json:"harga"`
Keterangan string `json:"keterangan"`
}
type Ternaks []Ternak
Duh ntaran. Now create PostgreSQL db
$ heroku addons:create heroku-postgresql:hobby-dev
hobby-dev: the plan. I’m broke af
If we read the config:
$ heroku config -s
DATABASE_URL='postgres://fpaselewarvrur:f3k9X0t-Wfpjr5IsLt76SZO-i-@ec2-23-21-255-14.compute-1.amazonaws.com:5432/ddiqgfelq64kb6'
If you don’t have DATABASE_URL, upgrade with:
$ heroku pg:promote WHAT_CONTAINS_POSTGRES
See the info with: $ heroku pg
To connect to db:
$ heroku pg:psql
Readreadread: https://devcenter.heroku.com/categories/go https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-go
#DB WORKED
- I changed /etc/postgresql/9.3/main/pg_hba.conf
local postgres peer
tolocal postgres trust
so I can change psql password (which I don’t have before) sudo service postgresql restart
psql -U postgres
, and set postgres user password with postgresALTER USER postgres WITH PASSWORD postgres;
- Change trust to md5
sudo service postgresql restart
- Pull table to local Postgres database: (Well, I create it manually…) WARNING: create it in
postgres
db! See available dbs with\list
in postgres - Install as usual, but when running, instead of
PORT=5000 $GOPATH/bin/ternaku-api
, I changed it toPGUSER=postgres PGPASSWORD=postgres PORT=5000 $GOPATH/bin/ternaku-api
Note: I think we can stay with trust, and when running, we don’t need to set PGPASSWORD (cmiiw?)
Added ./runlocal.sh
script
heroku pg:pull DATABASE_URL postgres