Skip to content

Commit bd449d5

Browse files
Merge pull request #56 from JumboInteractiveLimited/return-schema
Allow apisprout to return the schema given
2 parents adbe0b4 + 93bfeba commit bd449d5

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
@@ -590,6 +590,7 @@ func server(cmd *cobra.Command, args []string) {
590590

591591
var err error
592592
var data []byte
593+
dataType := strings.Trim(strings.ToLower(filepath.Ext(uri)), ".")
593594

594595
// Load either from an HTTP URL or from a local file depending on the passed
595596
// in value.
@@ -715,6 +716,21 @@ func server(cmd *cobra.Command, args []string) {
715716
log.Printf("Health check")
716717
})
717718

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

0 commit comments

Comments
 (0)