@@ -10,7 +10,6 @@ import (
10
10
"mime/multipart"
11
11
"net/http"
12
12
"net/url"
13
- "path"
14
13
"reflect"
15
14
"regexp"
16
15
"strings"
@@ -181,6 +180,15 @@ type Config struct {
181
180
// blank and attach it directly to the router or adapter.
182
181
DocsPath string
183
182
183
+ // DocsAdapter is an optional [Adapter] that will be used to serve the API
184
+ // documentation. If unset, the main adapter will be used.
185
+ // Setting this allows you to use a different router or middleware stack for
186
+ // the documentation.
187
+ // Note that if this [Adapter] has a different path than the main one, the
188
+ // default CreateHook set in [DefaultConfig] needs to be modified to account
189
+ // for it.
190
+ DocsAdapter Adapter
191
+
184
192
// SchemasPath is the path to the API schemas. If set to `/schemas` it will
185
193
// allow clients to get `/schemas/{schema}` to view the schema in a browser
186
194
// or for use in editors like VSCode to provide autocomplete & validation.
@@ -362,7 +370,7 @@ func getAPIPrefix(oapi *OpenAPI) string {
362
370
// config := huma.DefaultConfig("Example API", "1.0.0")
363
371
// api := huma.NewAPI(config, adapter)
364
372
func NewAPI (config Config , a Adapter ) API {
365
- for i := 0 ; i < len ( config .CreateHooks ); i ++ {
373
+ for i := range config .CreateHooks {
366
374
config = config.CreateHooks [i ](config )
367
375
}
368
376
@@ -400,9 +408,14 @@ func NewAPI(config Config, a Adapter) API {
400
408
newAPI .formatKeys = append (newAPI .formatKeys , k )
401
409
}
402
410
411
+ docsAdapter := newAPI .adapter
412
+ if config .DocsAdapter != nil {
413
+ docsAdapter = config .DocsAdapter
414
+ }
415
+
403
416
if config .OpenAPIPath != "" {
404
417
var specJSON []byte
405
- a .Handle (& Operation {
418
+ docsAdapter .Handle (& Operation {
406
419
Method : http .MethodGet ,
407
420
Path : config .OpenAPIPath + ".json" ,
408
421
}, func (ctx Context ) {
@@ -413,7 +426,7 @@ func NewAPI(config Config, a Adapter) API {
413
426
ctx .BodyWriter ().Write (specJSON )
414
427
})
415
428
var specJSON30 []byte
416
- a .Handle (& Operation {
429
+ docsAdapter .Handle (& Operation {
417
430
Method : http .MethodGet ,
418
431
Path : config .OpenAPIPath + "-3.0.json" ,
419
432
}, func (ctx Context ) {
@@ -424,7 +437,7 @@ func NewAPI(config Config, a Adapter) API {
424
437
ctx .BodyWriter ().Write (specJSON30 )
425
438
})
426
439
var specYAML []byte
427
- a .Handle (& Operation {
440
+ docsAdapter .Handle (& Operation {
428
441
Method : http .MethodGet ,
429
442
Path : config .OpenAPIPath + ".yaml" ,
430
443
}, func (ctx Context ) {
@@ -435,7 +448,7 @@ func NewAPI(config Config, a Adapter) API {
435
448
ctx .BodyWriter ().Write (specYAML )
436
449
})
437
450
var specYAML30 []byte
438
- a .Handle (& Operation {
451
+ docsAdapter .Handle (& Operation {
439
452
Method : http .MethodGet ,
440
453
Path : config .OpenAPIPath + "-3.0.yaml" ,
441
454
}, func (ctx Context ) {
@@ -448,14 +461,10 @@ func NewAPI(config Config, a Adapter) API {
448
461
}
449
462
450
463
if config .DocsPath != "" {
451
- a .Handle (& Operation {
464
+ docsAdapter .Handle (& Operation {
452
465
Method : http .MethodGet ,
453
466
Path : config .DocsPath ,
454
467
}, func (ctx Context ) {
455
- openAPIPath := config .OpenAPIPath
456
- if prefix := getAPIPrefix (newAPI .OpenAPI ()); prefix != "" {
457
- openAPIPath = path .Join (prefix , openAPIPath )
458
- }
459
468
ctx .SetHeader ("Content-Type" , "text/html" )
460
469
title := "Elements in HTML"
461
470
if config .Info != nil && config .Info .Title != "" {
@@ -476,7 +485,7 @@ func NewAPI(config Config, a Adapter) API {
476
485
<body style="height: 100vh;">
477
486
478
487
<elements-api
479
- apiDescriptionUrl="` + openAPIPath + `.yaml"
488
+ apiDescriptionUrl="` + strings . TrimPrefix ( config . OpenAPIPath , "/" ) + `.yaml"
480
489
router="hash"
481
490
layout="sidebar"
482
491
tryItCredentialsPolicy="same-origin"
@@ -488,7 +497,7 @@ func NewAPI(config Config, a Adapter) API {
488
497
}
489
498
490
499
if config .SchemasPath != "" {
491
- a .Handle (& Operation {
500
+ docsAdapter .Handle (& Operation {
492
501
Method : http .MethodGet ,
493
502
Path : config .SchemasPath + "/{schema}" ,
494
503
}, func (ctx Context ) {
0 commit comments