Sample API Using GoLang gin-gonic Framework
- Go
- PostgreSQL
- Text Editor/IDE. Recommendation: GoLand
- GOPATH setup
- github.com/gin-gonic/gin : GoLang HTTP web framework
- github.com/go-sql-driver/mysql : GoLang MySQL-Driver
- github.com/jinzhu/gorm : GoLang ORM library
- github.com/spf13/viper : Golang configuration library to read your application environment variables
run go get packageName
to install package
Create new file config.yml
in root directory. File Content:
app:
host: yourApplicationHost e.g. http://localhost:8080
port: yourApplicationPort e.g. 8080
show-sql: true
production: false
datasource: "user:password@/db_name?charset=utf8&parseTime=True&loc=Local"
Create new Table on your database. You need to run it manually because we don't use migration process in this application.
CREATE TABLE IF NOT EXISTS user_users (
id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name varchar(50) NOT NULL,
skin_type varchar(50),
age int(11),
updated_at TIMESTAMP,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS user_skin_cares (
id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
brand varchar(50),
name varchar(50) NOT NULL,
product_type varchar(50),
skin_concern varchar(100),
updated_at TIMESTAMP,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
user_id int not null,
FOREIGN KEY fk_user_skin_cares_user_users_user_id(user_id)
REFERENCES user_users(id)
ON UPDATE CASCADE
ON DELETE RESTRICT
);
- Run
go build -o sample-golang-api
to build Application - Then use
./sample-golang-api
to run Application
if your application run successfully, here is the result :
[GIN-debug] GET /v1/user/user --> github.com/yaumianwar/sample-golang-api/user/handler.GetAllUsers.func1 (3 handlers)
[GIN-debug] POST /v1/user/user --> github.com/yaumianwar/sample-golang-api/user/handler.CreateUser.func1 (3 handlers)
[GIN-debug] GET /v1/user/user/:id --> github.com/yaumianwar/sample-golang-api/user/handler.GetSingleUser.func1 (3 handlers)
[GIN-debug] POST /v1/user/user/:id --> github.com/yaumianwar/sample-golang-api/user/handler.UpdateUser.func1 (3 handlers)
[GIN-debug] DELETE /v1/user/user/:id --> github.com/yaumianwar/sample-golang-api/user/handler.DeleteUser.func1 (3 handlers)
[GIN-debug] GET /v1/user/user/:id/skincare --> github.com/yaumianwar/sample-golang-api/user/handler.GetAllSkincareByUser.func1 (3 handlers)
[GIN-debug] POST /v1/user/user/:id/skincare --> github.com/yaumianwar/sample-golang-api/user/handler.CreateSkincare.func1 (3 handlers)
[GIN-debug] GET /v1/user/user/:id/skincare/:skincareId --> github.com/yaumianwar/sample-golang-api/user/handler.GetSingleSkincare.func1 (3 handlers)
[GIN-debug] POST /v1/user/user/:id/skincare/:skincareId --> github.com/yaumianwar/sample-golang-api/user/handler.UpdateSkincare.func1 (3 handlers)
[GIN-debug] DELETE /v1/user/user/:id/skincare/:skincareId --> github.com/yaumianwar/sample-golang-api/user/handler.DeleteSkincare.func1 (3 handlers)
[GIN-debug] Listening and serving HTTP on :8080
This application is about user skincare, this application allow us to :
- Get All User Data : API
GET /v1/user/user
- Get Single User Data : API
GET /v1/user/user/:id
- Create User Data : API
POST /v1/user/user
- Update user Data : API
POST /v1/user/user/:id
- Delete User Data : API
DELETE /v1/user/user/:id
- Get All Skincare Data By User : API
GET /v1/user/user/:id/skincare
- Get Single Skincare Data : API
GET /v1/user/user/:id/skincare/:skincareId
- Create User Skincare Data : API
POST /v1/user/user/:id/skincare
- Update User Skincare Data : API
POST /v1/user/user/:id/skincare/:skincareId
- Delete User Skincare Data : API
DELETE /v1/user/user/:id/skincare/:skincareId
After your application already running, you can create API Request using curl
or Postman.
-
I provide Postman Collection at root directory called
sample-golang-api.postman_collection.json
, so you can import this file to your Postman Application and will create new Postman Collection for this application API which contains all of API of this application -
curl
example to request API Get All User Datarun :
curl -X GET http://localhost:8080/v1/user/user
result :
{"data":[{"id":1,"name":"Nur Yaumi","skinType":"oily acne prone","age":20,"updatedAt":"2019-07-16T19:41:27+08:00","createdAt":"2019-07-16T19:41:27+08:00"}],"status":{"message":"data fetched"}}