Debugowanie frameworka GIN w kontenerze

Debugowanie frameworka GIN w kontenerze
Tomek Strzeszkowski
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 4
1

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

Kopiuj
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 --from=builder /app/main main
COPY --from=builder /go/bin/dlv /dlv

CMD ["/dlv", "--listen=:8081",  "--headless=true", "--continue", "--accept-multiclient", "--headless=true", "--api-version=2", "exec", "./main"]

main.go

Kopiuj
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

Kopiuj
services:
  library-go-backend:
    build: .
    ports:
      - 8080:8080
      - 8081:8081

i .vscode/launch.json

Kopiuj
{
    "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:

Kopiuj
 ⠋ 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ę:

Kopiuj
curl http://localhost:8080

odpowiedź
{"status":"ok"}%

DR
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 1135
0

Co masz w logach dockera?

Wrzuć jeszcze request jaki wykonujesz.

Tomek Strzeszkowski
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 4
1
Dregorio napisał(a):

Co masz w logach dockera?

Wrzuć jeszcze request jaki wykonujesz.

zrobiłem update w pytaniu,

DR
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 1135
0

Debug console coś ci pokazuje?

Tomek Strzeszkowski
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 4
1
Dregorio napisał(a):

Debug console coś ci pokazuje?

Kopiuj
DisconnectRequest
Closing Delve.
Remote Debugging: close dlv connection.
DisconnectRequest to parent to shut down protocol server.
DisconnectResponse
Verbose logs are written to:
/var/folders/pl/md29lpmn1bn5cv11vk1s_wk80000gn/T/vscode-go-debug.txt
InitializeRequest
InitializeResponse
AttachRequest
Start remote debugging: connecting 127.0.0.1:8081
InitializeEvent
ConfigurationDoneRequest
ConfigurationDoneResponse {"seq":12,"type":"response","request_seq":3,"command":"configurationDone","success":true}
SetBreakPointsRequest
Halting before setting breakpoints. SkipStopEventOnce is false.
All cleared
Creating on: /Users/tomasz/pbuf/main.go:19
All set:[{"Breakpoint":{"id":60,"name":"","addr":7225507,"addrs":[7225507],"addrpid":[11],"file":"/app/main.go","line":19,"functionName":"main.GetDBContentTags","ExprString":"","Cond":"","HitCond":"","HitCondPerG":false,"continue":false,"traceReturn":false,"goroutine":false,"stacktrace":0,"LoadArgs":{"FollowPointers":true,"MaxVariableRecurse":1,"MaxStringLen":64,"MaxArrayValues":64,"MaxStructFields":-1},"LoadLocals":{"FollowPointers":true,"MaxVariableRecurse":1,"MaxStringLen":64,"MaxArrayValues":64,"MaxStructFields":-1},"WatchExpr":"","WatchType":0,"hitCount":{},"totalHitCount":0,"disabled":false,"RootFuncName":"","TraceFollowCalls":0}}]
SetBreakPointsResponse

DR
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 1135
0

Sorry, ale nie mam pomysłu

Zarejestruj się i dołącz do największej społeczności programistów w Polsce.

Otrzymaj wsparcie, dziel się wiedzą i rozwijaj swoje umiejętności z najlepszymi.