package saasSwagger import ( "context" "fmt" "html/template" "net/http" "regexp" "strings" "geeks-accelerator/oss/saas-starter-kit/internal/platform/web" "geeks-accelerator/oss/saas-starter-kit/internal/platform/web/weberror" "github.com/geeks-accelerator/files" "github.com/geeks-accelerator/swag" "github.com/pborman/uuid" "github.com/pkg/errors" ) var ( // ErrNotFound used when a file doesn't exist. ErrNotFound = errors.New("File not found") ) // Config stores echoSwagger configuration variables. type Config struct { //The url pointing to API definition (normally swagger.json or swagger.yaml). Default is `doc.json`. URL string } // URL presents the url pointing to API definition (normally swagger.json or swagger.yaml). func URL(url string) func(c *Config) { return func(c *Config) { c.URL = url } } // WrapHandler wraps swaggerFiles.Handler and returns web.Handler var WrapHandler = SaasWrapHandler() // SaasWrapHandler wraps `http.Handler` into `web.Handler`. func SaasWrapHandler(confs ...func(c *Config)) web.Handler { handler := swaggerFiles.Handler config := &Config{ URL: "doc.json", } for _, c := range confs { c(config) } // create a template with name t := template.New("swagger_index.html") index, _ := t.Parse(indexTempl) type pro struct { Host string } var re = regexp.MustCompile(`(.*)(index\.html|doc\.json|favicon-16x16\.png|favicon-32x32\.png|/oauth2-redirect\.html|swagger-ui\.css|swagger-ui\.css\.map|swagger-ui\.js|swagger-ui\.js\.map|swagger-ui-bundle\.js|swagger-ui-bundle\.js\.map|swagger-ui-standalone-preset\.js|swagger-ui-standalone-preset\.js\.map)[\?|.]*`) // Create the handler that will be attached in the middleware chain. h := func(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error { var ( path string prefix string ) matches := re.FindStringSubmatch(r.RequestURI) if len(matches) == 3 { path = matches[2] prefix = matches[1] } else if len(matches) > 0 { err := errors.WithMessagef(ErrNotFound, "page %s not found", r.RequestURI) return weberror.NewError(ctx, err, http.StatusNotFound) } // Default to index page. if path == "" { path = "index.html" prefix = r.RequestURI } // Set the http prefix. handler.Prefix = prefix switch path { case "index.html": index.Execute(w, config) case "doc.json": doc, err := swag.ReadDoc() if err != nil { return weberror.NewError(ctx, err, http.StatusInternalServerError) } // Replace the dynamic placeholder {RANDOM_UUID} for { if !strings.Contains(doc, "{RANDOM_UUID}") { break } doc = strings.Replace(doc, "{RANDOM_UUID}", uuid.NewRandom().String(), 1) } // Replace the dynamic placeholder {RANDOM_EMAIL} for { if !strings.Contains(doc, "{RANDOM_EMAIL}") { break } randEmail := fmt.Sprintf("%s@example.com", uuid.NewRandom().String()) doc = strings.Replace(doc, "{RANDOM_EMAIL}", randEmail, 1) } return web.RespondJson(ctx, w, []byte(doc), http.StatusOK) default: if strings.HasSuffix(path, ".html") { w.Header().Set("Content-Type", "text/html; charset=utf-8") } else if strings.HasSuffix(path, ".css") { w.Header().Set("Content-Type", "text/css; charset=utf-8") } else if strings.HasSuffix(path, ".js") { w.Header().Set("Content-Type", "application/javascript") } else if strings.HasSuffix(path, ".json") { w.Header().Set("Content-Type", "application/json") } handler.ServeHTTP(w, r) } return nil } return h } const indexTempl = ` Swagger UI
`