feat quickly
This commit is contained in:
@@ -23,7 +23,7 @@ type Settings struct {
|
||||
MysqlModelPath string `json:"mysqlModelPath"`
|
||||
DefaultQueryPackagePath string `json:"defaultQueryPackagePath"`
|
||||
ModelBasePath string `json:"modelBasePath"`
|
||||
SwaggerDir string `json:"swaggerDir"`
|
||||
SwaggerFilePath string `json:"swaggerFilePath"`
|
||||
Databases []DatabaseConfig `json:"databases"`
|
||||
Projects []ProjectConfig `json:"projects"`
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ func (a *App) loadSettings() {
|
||||
MysqlModelPath: "",
|
||||
DefaultQueryPackagePath: "",
|
||||
ModelBasePath: "",
|
||||
SwaggerDir: "",
|
||||
SwaggerFilePath: "",
|
||||
Databases: []DatabaseConfig{},
|
||||
Projects: []ProjectConfig{},
|
||||
}
|
||||
@@ -49,7 +49,7 @@ func (a *App) loadSettings() {
|
||||
MysqlModelPath: "",
|
||||
DefaultQueryPackagePath: "",
|
||||
ModelBasePath: "",
|
||||
SwaggerDir: "",
|
||||
SwaggerFilePath: "",
|
||||
Databases: []DatabaseConfig{},
|
||||
Projects: []ProjectConfig{},
|
||||
}
|
||||
@@ -69,7 +69,7 @@ func (a *App) loadSettings() {
|
||||
MysqlModelPath: "",
|
||||
DefaultQueryPackagePath: "",
|
||||
ModelBasePath: "",
|
||||
SwaggerDir: "",
|
||||
SwaggerFilePath: "",
|
||||
Databases: []DatabaseConfig{},
|
||||
Projects: []ProjectConfig{},
|
||||
}
|
||||
|
||||
@@ -63,6 +63,24 @@ func (a *App) GetSwaggerFiles(dirPath string) ([]SwaggerFile, error) {
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (a *App) GetSwaggerFileInfo(filePath string) (SwaggerFile, error) {
|
||||
if filePath == "" {
|
||||
return SwaggerFile{}, fmt.Errorf("file path is empty")
|
||||
}
|
||||
|
||||
info, err := os.Stat(filePath)
|
||||
if err != nil {
|
||||
return SwaggerFile{}, fmt.Errorf("failed to get file info: %w", err)
|
||||
}
|
||||
|
||||
return SwaggerFile{
|
||||
Name: info.Name(),
|
||||
Path: filePath,
|
||||
Size: info.Size(),
|
||||
ModifiedTime: info.ModTime().Format("2006-01-02 15:04:05"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a *App) ReadSwaggerFile(filePath string) (string, error) {
|
||||
if filePath == "" {
|
||||
return "", fmt.Errorf("file path is empty")
|
||||
@@ -76,9 +94,9 @@ func (a *App) ReadSwaggerFile(filePath string) (string, error) {
|
||||
return string(content), nil
|
||||
}
|
||||
|
||||
func (a *App) StartSwaggerServer(dirPath string) (string, error) {
|
||||
if dirPath == "" {
|
||||
return "", fmt.Errorf("directory path is empty")
|
||||
func (a *App) StartSwaggerServer(filePath string) (string, error) {
|
||||
if filePath == "" {
|
||||
return "", fmt.Errorf("file path is empty")
|
||||
}
|
||||
|
||||
swaggerServerMu.Lock()
|
||||
@@ -121,11 +139,8 @@ func (a *App) StartSwaggerServer(dirPath string) (string, error) {
|
||||
<script src="https://unpkg.com/swagger-ui-dist@5.10.5/swagger-ui-bundle.js"></script>
|
||||
<script>
|
||||
window.onload = function() {
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const fileParam = urlParams.get('file');
|
||||
const swaggerUrl = fileParam ? '/swagger.json?file=' + encodeURIComponent(fileParam) : '/swagger.json';
|
||||
const ui = SwaggerUIBundle({
|
||||
url: swaggerUrl,
|
||||
url: '/swagger.json',
|
||||
dom_id: '#swagger-ui',
|
||||
presets: [
|
||||
SwaggerUIBundle.presets.apis,
|
||||
@@ -148,29 +163,7 @@ func (a *App) StartSwaggerServer(dirPath string) (string, error) {
|
||||
})
|
||||
|
||||
mux.HandleFunc("/swagger.json", func(w http.ResponseWriter, r *http.Request) {
|
||||
files, err := a.GetSwaggerFiles(dirPath)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if len(files) == 0 {
|
||||
http.Error(w, "No swagger files found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
fileName := r.URL.Query().Get("file")
|
||||
selectedFile := files[0]
|
||||
if fileName != "" {
|
||||
for _, file := range files {
|
||||
if file.Name == fileName {
|
||||
selectedFile = file
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
content, err := os.ReadFile(selectedFile.Path)
|
||||
content, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user