Witam,
Staram się znaleźć metodę na debugowanie frameworka GIN w kontenerze dockera. Jako IDE wybrałem vscode, po dodaniu breakpoint'a w debugerze widzę SetBreakPointsRequest
ale gdy wysyłam request przez przeglądarkę nie ma efektu, oto moja konfiguracja:
Dockerfile
FROM golang:1.22.1 AS builder
WORKDIR /app/
COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main .
#dlv
RUN CGO_ENABLED=0 go install -ldflags "-s -w -extldflags '-static'" github.com/go-delve/delve/cmd/dlv@latest
FROM alpine:latest
WORKDIR /app/
COPY /app/main main
COPY /go/bin/dlv /dlv
CMD ["/dlv", "--listen=:8081", "--headless=true", "--continue", "--accept-multiclient", "--headless=true", "--api-version=2", "exec", "./main"]
main.go
package main
import (
"fmt"
"net/http"
"pbuf/models"
"github.com/gin-gonic/gin"
)
func sayOk(c *gin.Context) {
fmt.Println(2 + 2)
c.JSON(http.StatusOK, gin.H{
"status": "ok",
})
}
func GetDBContentTags(c *gin.Context) {
contentTags := models.ListContentTags()
c.JSON(http.StatusOK, contentTags)
}
func main() {
router := gin.Default()
router.StaticFile("/content_tag.proto", "./content_tag.proto")
//router.GET("/albums", getAlbums)
router.GET("/", sayOk)
router.GET("/content_tags", GetDBContentTags)
router.Run(":8080")
}
w docker-compose tylko mapowanie portów 8080 i 8081
services:
library-go-backend:
build: .
ports:
- 8080:8080
- 8081:8081
i .vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
//"processId": 36840,
"type": "go",
"request": "attach",
"mode": "remote",
//"remotePath": "",
"port": 8080,
"host": "127.0.0.1",
"showLog": true,
"trace": "log",
"logOutput": "rpc"
}
]
w logach:
⠋ Container pbuf-library-go-backend-1 Recreated 0.0s
Attaching to library-go-backend-1
library-go-backend-1 | 2024-07-20T13:28:58Z warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted)
library-go-backend-1 | API server listening at: [::]:8081
library-go-backend-1 | [GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
library-go-backend-1 |
library-go-backend-1 | [GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
library-go-backend-1 | - using env: export GIN_MODE=release
library-go-backend-1 | - using code: gin.SetMode(gin.ReleaseMode)
library-go-backend-1 |
library-go-backend-1 | [GIN-debug] GET /content_tag.proto --> main.main.(*RouterGroup).StaticFile.func1 (3 handlers)
library-go-backend-1 | [GIN-debug] HEAD /content_tag.proto --> main.main.(*RouterGroup).StaticFile.func1 (3 handlers)
library-go-backend-1 | [GIN-debug] GET / --> main.sayOk (3 handlers)
library-go-backend-1 | [GIN-debug] GET /content_tags --> main.GetDBContentTags (3 handlers)
library-go-backend-1 | [GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
library-go-backend-1 | Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
library-go-backend-1 | [GIN-debug] Listening and serving HTTP on :8080
library-go-backend-1 | 4
library-go-backend-1 | [GIN] 2024/07/20 - 13:29:22 | 200 | 3.274208ms | 192.168.65.1 | GET "/"
library-go-backend-1 | 2024-07-20T13:29:24Z error layer=rpc rpc:invalid character 'G' looking for beginning of value
library-go-backend-1 | 2024-07-20T13:29:25Z error layer=rpc rpc:invalid character 'G' looking for beginning of value
library-go-backend-1 | 4
library-go-backend-1 | [GIN] 2024/07/20 - 13:29:29 | 200 | 144.667µs | 192.168.65.1 | GET "/"
request który wykonuję:
curl http://localhost:8080
odpowiedź
{"status":"ok"}%