CodeVetter · Repo Unpack · Behavior traces
gin-gonic/gin
Three principal flows were followed through implementation: server startup and request dispatch, JSON binding and response serialization, and multipart upload persistence. Additional traces cover authentication, route misses, panic recovery, and static files. Ordering and branch behavior below come from source reads rather than runtime execution.
- Startup: mode.init reads GIN_MODE and calls SetMode; Default calls New, installs Logger/Recovery, and With applies options. Unspecified mode becomes test when test.v exists, otherwise debug.
- Registration and listening: GET reaches RouterGroup.handle → calculateAbsolutePath → combineHandlers → Engine.addRoute → node.addRoute; Run updates trees, resolves explicit address or PORT/:8080, builds http.Server, and calls ListenAndServe.
- Successful dispatch: ServeHTTP → writer.reset → Context.reset → handleHTTPRequest → node.getValue → Context.Next → WriteHeaderNow → pool.Put; the matching chain receives Params and FullPath before execution.
- JSON action: ShouldBindWith → jsonBinding.Bind → decodeJSON → decoder.Decode → validate → defaultValidator.ValidateStruct; a handler's JSON response then reaches Context.Render → render.JSON.Render → WriteJSON → writer.Write.
- Upload persistence: FormFile conditionally parses multipart using Engine.MaxMultipartMemory, obtains and closes the initial file handle, then SaveUploadedFile opens the FileHeader, creates directories, conditionally chmods a newly created destination directory, creates the output, and copies bytes.
- Basic authentication: BasicAuth → BasicAuthForRealm → processAccounts precomputes headers; request-time searchCredential compares credentials, then either sets AuthUserKey or writes a challenge and aborts with 401.
- Route miss: dispatch first considers enabled redirects; with HandleMethodNotAllowed it searches other method trees and emits Allow plus 405, otherwise installs allNoRoute and calls serveError for 404.
- Recovery wraps c.Next in a deferred recover; broken connections attach an error and abort, while other panics invoke the configured recovery callback. StaticFS separately opens the requested file, switches to noRoute on open failure, or delegates to a prefix-stripped FileServer.