dev-resources.site
for different kinds of informations.
Versioning in Go Huma
Published at
1/10/2025
Categories
go
documentation
Author
programmingdecoded
Author
18 person written this
programmingdecoded
open
We want to have a separate documentation for each version in Go Huma, e.g. /v1/docs, /v2/docs, etc.
This can be done by setting the docs path like this:
config.DocsPath = "/{version}/docs"
We can use middleware to get the version from the path in the request and load the description used in the docs depending on the version:
config := huma.DefaultConfig("API V"+versionNumberString, versionNumberString+".0.0")
overviewFilePath := filepath.Join("/app/docs", fmt.Sprintf("v%s", versionNumberString), "overview.md")
overview, err := ioutil.ReadFile(overviewFilePath)
if err != nil {
log.Fatalf("Error reading file: %v", err)
}
config.Info.Description = string(overview)
And we can import all routes that we want to display in the docs based on the version. This will also load them so that they can be used as actual endpoints.
This is the complete code for routing:
router := chi.NewMux()
router.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
urlPathParts := strings.Split(r.URL.Path, "/")
versions := []string{"v1", "v2", "v3"}
if helpers.Contains(versions, urlPathParts[1]) {
versionPath := urlPathParts[1]
if versionPath == "" {
http.Error(w, "version does not exist", http.StatusInternalServerError)
}
versionNumberString := strings.TrimPrefix(versionPath, "v")
versionNumber, _ := strconv.Atoi(versionNumberString)
config := huma.DefaultConfig("API V"+versionNumberString, versionNumberString+".0.0")
overviewFilePath := fmt.Sprintf("docs/v%s/overview.md", versionNumberString)
overview, err := ioutil.ReadFile(overviewFilePath)
if err != nil {
log.Fatalf("Error reading file: %v", err)
}
config.Info.Description = string(overview)
api := humachi.New(router, config)
switch versionNumber {
case 1:
api = v1handlers.AddV1Middleware(api)
v1handlers.AddV1Routes(api)
case 2:
api = v2handlers.AddV2Middleware(api)
v2handlers.AddV2Routes(api)
default:
api = v3handlers.AddV3Middleware(api)
router = v3handlers.AddV3ErrorResponses(router)
v3handlers.AddV3Routes(api)
}
}
next.ServeHTTP(w, r)
})
})
config := huma.DefaultConfig("API V3", "3.0.0")
config.DocsPath = "/{version}/docs"
humachi.New(router, config)
go Article's
30 articles in total
A tรฉcnica dos dois ponteiros
read article
Preventing SQL Injection with Raw SQL and ORM in Golang
read article
๐น Golang Integration with Kafka and Uber ZapLog ๐จ
read article
๐ Building Golang RESTful API with Gin, MongoDB ๐ฑ
read article
Golang e DSA
read article
Prevent Race Conditions Like a Pro with sync.Mutex in Go!
read article
tnfy.link - What's about ID?
read article
Developing a Simple RESTful API with Gin, ginvalidator, and validatorgo
read article
Desbravando Go: Capรญtulo 1 โ Primeiros Passos na Linguagem
read article
Compile-Time Assertions in Go (Golang)
read article
Mastering GoFrame Logging: From Zero to Hero
read article
GoFr: An Opinionated Microservice Development Framework
read article
The Struggle of Finding a Free Excel to PDF Converter: My Journey and Solution
read article
Setting Up Your Go Environment
read article
External Merge Problem - Complete Guide for Gophers
read article
Mastering Go's encoding/json: Efficient Parsing Techniques for Optimal Performance
read article
Golang with Colly: Use Random Fake User-Agents When Scraping
read article
Versioning in Go Huma
currently reading
Go Basics: Syntax and Structure
read article
Interesting feedback on Fuego!
read article
Making Beautiful API Keys
read article
Building a Semantic Search Engine with OpenAI, Go, and PostgreSQL (pgvector)
read article
Go's Concurrency Decoded: Goroutine Scheduling
read article
Golang: Struct, Interface And Dependency Injection(DI)
read article
Desvendando Subprocessos: Criando um Bot de Mรบsica com Go
read article
go
read article
๐ New Article Alert: Master sync.Pool in Golang! ๐
read article
Week Seven Recap of #100DaysOfCode
read article
Ore: Advanced Dependency Injection Package for Go
read article
Golang vs C++: A Modern Alternative for High-Performance Applications
read article
Featured ones: