Goè¨èªã§ gin ãã¬ã¼ã ã¯ã¼ã¯ã使ã£ã¦ REST API ãä½æããæé ã«ã¤ãã¦è¨è¼ãã¦ãã¾ãã
Â
å®è¡ç°å¢ã«ã¤ãã¦
ä¸è¨ã®ç°å¢ã§åããã¦ãã¾ãã
Â
DBç°å¢ãæºå
ä»å㯠PostgreSQL ã使ç¨ãã¦ãã¾ãã
docker-compose.yml
Â
ãã¼ãã«ãä½æ
docker exec -it postgres_gin_rest psql -U user -d sample
ä¸è¨ã§ PostgreSQL ã«æ¥ç¶ããä¸è¨ã®ãã¼ãã«CREATEæãå®è¡ãã¾ãã
Â
CREATE TABLE "user" (
  id SERIAL PRIMARY KEY,
  name VARCHAR(255),
  age INT
);
Â
ããã§ä½æãããã¼ãã«ã«å¯¾ã㦠CRUD ãè¡ã REST API ãä½æãã¦ããã¾ãã
Â
gin ãå°å ¥
ã¾ã㯠gin ã使ããããã«ãã¾ãã
go mod init {ããã¸ã§ã¯ãå}
㧠go.mod ãã¡ã¤ã«ãä½æã
Â
main.go ãä½æããä¸èº«ãè¨è¼ãã¾ãã
main.go
Â
ä¸è¨ã® import ã§ã³ã³ãã¤ã«ã¨ã©ã¼ã«ãªãã®ã§ã
Â
ããã¸ã§ã¯ãã«ã¼ãã§ãgo mod tidyãã³ãã³ããå®è¡ããgin ã®ä¾åé¢ä¿ã追å ãã¾ãã
Â
go run main.goÂ
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:  export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)[GIN-debug] GET   /             --> main.handleHome (3 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080
Â
main.go ãå®è¡ãã㨠8080 ãã¼ãã§èµ·åããã®ã§ããhttp://localhost:8080ãã«ãã©ã¦ã¶ããã¢ã¯ã»ã¹ãã¦ã¿ã¾ãã
{
  "message": "test"
}
ä¸è¨ãç»é¢ã«è¡¨ç¤ºãããã°ç¡äºã«æ¥ç¶ã§ãã¦ãã¾ãã
Â
gin 㧠REST API æ§ç¯ã¼ç»é²å¦ç
ç¶ãã¦ãäºåã«ä½æãã BOOK ãã¼ãã«ã«å¯¾ãã¦ãã¼ã¿ç»é²ã®ã¨ã³ããã¤ã³ããä½æãã¦ããã¾ãã
ãã®ã¨ã³ããã¤ã³ãã¯ãä¸è¨ã®ä»æ§ã¨ãã¾ãã
- ã¨ã³ããã¤ã³ã㯠http://localhost:8080/user
- POSTã¡ã½ãããåãä»ãããªã¯ã¨ã¹ãããã£ã¼ã§æå®ãããå¤ããã¨ã«Userãã¼ãã«ã«ãã¼ã¿ãç»é²ãã
- ç»é²ãå®äºããããHTTPã¹ãã¼ã¿ã¹ï¼200ã¨ãç»é²ãããUserã¬ã³ã¼ãã®å¤ãã¬ã¹ãã³ã¹ãã
- ãªã¯ã¨ã¹ãããã£ã¼ã«ä¸åãããå ´åã¯ãHTTP ã¹ãã¼ã¿ã¹ï¼400ã¨ãã¨ã©ã¼æ å ±ãã¬ã¹ãã³ã¹ãã
- ãã®ä»æ³å®å¤ã®ã¨ã©ã¼ãçºçããå ´åã¯ãHTTP ã¹ãã¼ã¿ã¹ï¼500ã¨ãã¨ã©ã¼æ å ±ãã¬ã¹ãã³ã¹ãã
Â
ã¾ãã¯BOOKãã¼ãã«ã«è©²å½ããã¢ãã«ã¯ã©ã¹ãä½æãã¾ãã(model ããã±ã¼ã¸ãä½æãã¦ãã®ä¸ã« model.go ãã¡ã¤ã«ãä½æãã¦ãã¾ã)
Â
model/model.go
Â
Â
ç¶ãã¦ããã¼ã¿ç»é²å¦çãæ¸ãã¾ããï¼ãã¡ãããfuncs ããã±ã¼ã¸ãä½æãã¦ãã®ä¸ã« funcs.go ãã¡ã¤ã«ãä½æãã¦ãã¾ãï¼
Â
funcs/funcs.go
Â
main.go ã«ã¯ä¸è¨ã追è¨ãã¾ãã
main.go
Â
Â
POST ãªã¯ã¨ã¹ããåãä»ãããªã¯ã¨ã¹ãããã£ã¼ãã USER ãã¼ãã«ã®ç»é²å 容ãèªã¿åãããã¼ã¿ç»é²ããããã«ãã¾ããã
ç»é²ãå®äºããããHTTPã¬ã¹ãã³ã¹ã¹ãã¼ã¿ã¹ï¼200ã¨ãç»é²ããããã¼ã¿ãã¬ã¹ãã³ã¹ããã£ã¼ã«å«ãã¦è¿ãããã«ãã¦ãã¾ãã
Â
ã¾ããä¸è¨ã®importãã³ã³ãã¤ã«ã¨ã©ã¼ã«ãªãããã
Â
go mod tidy
pq ã®ä¾åã追å ãã¾ãã
Â
ããã§ã¯ãmain.go ãèµ·åããPOSTãªã¯ã¨ã¹ããéä¿¡ãã¦ã¿ã¾ãã
Â
curl -X POST -H "Content-Type: application/json" -d '{"Name": "User1", "Age": 40}' http://localhost:8080/user
{"user":{"Id":1,"Name":"User1","Age":40}}
Â
ã¬ã¹ãã³ã¹ãè¿ã£ã¦ããã®ã§ãPostgreSQL ã«ãã¤ãªãã§ãã¼ã¿ç»é²ããã¦ããã確èªãã¾ãã
Â
sample=# select * from public.user;
 id | name  | ageÂ
----+-------+-----
 1 | User1 |  40
(1 row)
ç¡äºã«ç»é²ã§ãã¦ãã¾ãã
Â
gin 㧠REST API æ§ç¯ã¼æ¤ç´¢å¦ç
ç¶ãã¦ããã¼ã¿æ¤ç´¢ã®ã¨ã³ããã¤ã³ããä½æãã¦ããã¾ãã
ãã®ã¨ã³ããã¤ã³ãã¯ãä¸è¨ã®ä»æ§ã¨ãã¾ãã
- ã¨ã³ããã¤ã³ã㯠http://localhost:8080/user/{æ¤ç´¢ãããã¦ã¼ã¶ã¼ID}
- GETã¡ã½ãããåãä»ãããã¹ãã©ã¡ã¼ã¿ã¼ã§æå®ãããUserIDããã¨ã«Userãã¼ãã«ãæ¤ç´¢ãã
- ãã¼ã¿ãåå¾ãã§ããããHTTPã¹ãã¼ã¿ã¹ï¼200ã¨ãåå¾ãããUserã¬ã³ã¼ãã®å¤ãã¬ã¹ãã³ã¹ãã
- ãªã¯ã¨ã¹ãã«ä¸åãããå ´åã¯ãHTTP ã¹ãã¼ã¿ã¹ï¼400ã¨ãã¨ã©ã¼æ å ±ãã¬ã¹ãã³ã¹ãã
- ãã®ä»æ³å®å¤ã®ã¨ã©ã¼ãçºçããå ´åã¯ãHTTP ã¹ãã¼ã¿ã¹ï¼500ã¨ãã¨ã©ã¼æ å ±ãã¬ã¹ãã³ã¹ãã
Â
DB æ¤ç´¢å¦çã¨ãã¦ãä¸è¨ã追è¨ãã¾ãã
funcs/funcs.go
Â
æ¤ç´¢ã®ãªã¯ã¨ã¹ãããã³ããªã³ã°ã§ããããã«ãããããmain.go ã«ã¯ä¸è¨ã追è¨ãã¾ãã
main.go
Â
Â
- ctx.Param ã§ãã¹ãã©ã¡ã¼ã¿ã¼ããå¤ãåå¾ãããã¨ãã§ãã¾ãããã㯠r.GET("/user/:id" ã®å®ç¾©ç®æã«ãªãã¾ãã
- strconv.Atio ã§æååãæ°å¤ã«ãã¼ã¹ãã¾ãã
Â
main.go ãèµ·åãã¦ãhttp://localhost:8080/user/1 ã«ãã©ã¦ã¶ããã¢ã¯ã»ã¹ãã¦ã¿ã¾ãã
以ä¸ã®å 容ããã©ã¦ã¶ä¸ã§ç¢ºèªã§ããã¨æãã¾ãã
Â
Â
Â
gin 㧠REST API æ§ç¯ã¼æ´æ°å¦ç
ç¶ãã¦ããã¼ã¿æ´æ°ã®ã¨ã³ããã¤ã³ããä½æãã¦ããã¾ãã
ãã®ã¨ã³ããã¤ã³ãã¯ãä¸è¨ã®ä»æ§ã¨ãã¾ãã
- ã¨ã³ããã¤ã³ã㯠http://localhost:8080/user/{æ´æ°ãããã¦ã¼ã¶ã¼ID}
- PUTã¡ã½ãããåãä»ãããªã¯ã¨ã¹ãããã£ã¼ã§æå®ãããå¤ããã¨ã«Userãã¼ãã«ã®æå®IDã®ãã¼ã¿ãæ´æ°ãã
- æ´æ°ãå®äºããããHTTPã¹ãã¼ã¿ã¹ï¼200ã¨ãæ´æ°ãããUserã¬ã³ã¼ãã®å¤ãã¬ã¹ãã³ã¹ãã
- ãªã¯ã¨ã¹ãããã£ã¼ã«ä¸åãããå ´åã¯ãHTTP ã¹ãã¼ã¿ã¹ï¼400ã¨ãã¨ã©ã¼æ å ±ãã¬ã¹ãã³ã¹ãã
- ãã®ä»æ³å®å¤ã®ã¨ã©ã¼ãçºçããå ´åã¯ãHTTP ã¹ãã¼ã¿ã¹ï¼500ã¨ãã¨ã©ã¼æ å ±ãã¬ã¹ãã³ã¹ãã
DB æ´æ°å¦çã¨ãã¦ãä¸è¨ã追è¨ãã¾ãã
funcs/funcs.go
Â
æ´æ°ã®ãªã¯ã¨ã¹ãããã³ããªã³ã°ã§ããããã«ãããããmain.go ã«ã¯ä¸è¨ã追è¨ãã¾ãã
main.go
Â
Â
- ctx.Param ã§ãã¹ãã©ã¡ã¼ã¿ã¼ããæ´æ°å¯¾è±¡ã®ã¦ã¼ã¶ã¼IDãåå¾ãã¦ãã¾ã
- ãªã¯ã¨ã¹ãããã£ã¼ãèªã¿è¾¼ã¿ãæ´æ°å¯¾è±¡ã®ã¦ã¼ã¶ã¼ã¢ãã«ã«ãããã³ã°ãã¾ã
Â
main.go ãèµ·åããPUTãªã¯ã¨ã¹ããéä¿¡ãã¦ã¿ã¾ãã
curl -X PUT -H "Content-Type: application/json" -d '{"Name": "Updated User1", "Age": 30}' http://localhost:8080/user/1
{"user":{"Id":1,"Name":"Updated User1","Age":30}}
ã¬ã¹ãã³ã¹ãè¿ã£ã¦ããã®ã§ãPostgreSQL ã«ãã¤ãªãã§ãã¼ã¿ç»é²ããã¦ããã確èªãã¾ãã
Â
select * from public.user;
 id |   name    | ageÂ
----+---------------+-----
 1 | Updated User1 |  30
(1 row)
ç¡äºã«æ´æ°ã§ãã¾ããã
Â
gin 㧠REST API æ§ç¯ã¼åé¤å¦ç
æå¾ã«ããã¼ã¿åé¤ã®ã¨ã³ããã¤ã³ããä½æãã¦ããã¾ãã
ãã®ã¨ã³ããã¤ã³ãã¯ãä¸è¨ã®ä»æ§ã¨ãã¾ãã
- ã¨ã³ããã¤ã³ã㯠http://localhost:8080/user/{åé¤ãããã¦ã¼ã¶ã¼ID}
- DELETEã¡ã½ãããåãä»ãããã¹ãã©ã¡ã¼ã¿ã¼ã§æå®ãããUserIDã«è©²å½ããã¦ã¼ã¶ã¼ãåé¤ãã
- ãã¼ã¿ãåé¤ãã§ããããHTTPã¹ãã¼ã¿ã¹ï¼200ãã¬ã¹ãã³ã¹ãã
- ãªã¯ã¨ã¹ãã«ä¸åãããå ´åã¯ãHTTP ã¹ãã¼ã¿ã¹ï¼400ã¨ãã¨ã©ã¼æ å ±ãã¬ã¹ãã³ã¹ãã
- ãã®ä»æ³å®å¤ã®ã¨ã©ã¼ãçºçããå ´åã¯ãHTTP ã¹ãã¼ã¿ã¹ï¼500ã¨ãã¨ã©ã¼æ å ±ãã¬ã¹ãã³ã¹ãã
Â
DB åé¤å¦çã¨ãã¦ãä¸è¨ã追è¨ãã¾ãã
funcs/funcs.go
Â
- æå®IDã®ã¦ã¼ã¶ã¼ãåå¨ãããDBã«åãåãããåå¨ããå ´åã«åé¤å¦çãå®è¡ãã¾ã
Â
åé¤ã®ãªã¯ã¨ã¹ãããã³ããªã³ã°ã§ããããã«ãããããmain.go ã«ã¯ä¸è¨ã追è¨ãã¾ãã
main.go
Â
Â
- ctx.Param ã§ãã¹ãã©ã¡ã¼ã¿ã¼ããåé¤å¯¾è±¡ã®ã¦ã¼ã¶ã¼IDãåå¾ããåé¤å¦çãå®è¡ãã¾ã
Â
main.go ãèµ·åããDELETEãªã¯ã¨ã¹ããéä¿¡ãã¦ã¿ã¾ãã
Â
curl -X DELETE http://localhost:8080/user/1
{}
Â
PostgreSQL ã«ãã¤ãªãã§ãã¼ã¿åé¤ããã¦ããã確èªãã¾ãã
Â
select * from public.user;
 id | name | ageÂ
----+------+-----
(0 rows)
Â
ç¡äºã«åé¤ããã¾ããã
Â
以ä¸ãç°¡åã§ããgo/ginã使ã£ãRESTãAPIä½æã®ãµã³ãã«ã§ãã
gin ãã¬ã¼ã ã¯ã¼ã¯ã使ç¨ãããã¨ã§ã·ã³ãã«ãªã³ã¼ãã§APIéçºã楽ã«å®è£ ã§ããã®ã¯ãã¡ãããä»åã¯æ¸ãã¦ãã¾ãããHTMLãè¿ããããã§ããã®ã§ç»é¢éçºãã§ããããã¾ãã
ä¼¼ããããªãã¬ã¼ã ã¯ã¼ã¯ã§ Echo ãããã®ã§ããã®ããããå¦ãã§ããããã
Â
Â
Â