Skip to content

Commit 93bfeba

Browse files
author
Jamie Curnow
committed
Allow apisprout to return the schema given
This is useful when spinning up fake services and generating the swagger-ui documentation using that very same service. A url of should not conflict with general use.
1 parent db11a30 commit 93bfeba

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

apisprout.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ func server(cmd *cobra.Command, args []string) {
587587

588588
var err error
589589
var data []byte
590+
dataType := strings.Trim(strings.ToLower(filepath.Ext(uri)), ".")
590591

591592
// Load either from an HTTP URL or from a local file depending on the passed
592593
// in value.
@@ -712,6 +713,21 @@ func server(cmd *cobra.Command, args []string) {
712713
log.Printf("Health check")
713714
})
714715

716+
// Another custom handler to return the exact swagger document given to us
717+
http.HandleFunc("/__schema", func(w http.ResponseWriter, req *http.Request) {
718+
if !viper.GetBool("disable-cors") {
719+
corsOrigin := req.Header.Get("Origin")
720+
if corsOrigin == "" {
721+
corsOrigin = "*"
722+
}
723+
w.Header().Set("Access-Control-Allow-Origin", corsOrigin)
724+
}
725+
726+
w.Header().Set("Content-Type", fmt.Sprintf("application/%v; charset=utf-8", dataType))
727+
w.WriteHeader(http.StatusOK)
728+
fmt.Fprint(w, string(data))
729+
})
730+
715731
// Register our custom HTTP handler that will use the router to find
716732
// the appropriate OpenAPI operation and try to return an example.
717733
http.Handle("/", handler(rr))

0 commit comments

Comments
 (0)