Ejemplo n.º 1
0
// GenerateServer generates API server files
func GenerateServer(ramlFile, dir, packageName, lang, apiDocsDir, rootImportPath string, generateMain bool) error {
	apiDef := new(raml.APIDefinition)
	// parse the raml file
	ramlBytes, err := raml.ParseReadFile(ramlFile, apiDef)
	if err != nil {
		return err
	}

	// global variables
	globAPIDef = apiDef
	globRootImportPath = rootImportPath

	// create directory if needed
	if err := checkCreateDir(dir); err != nil {
		return err
	}

	// create base server
	sd := server{
		PackageName: packageName,
		Title:       apiDef.Title,
		apiDef:      apiDef,
		APIDocsDir:  apiDocsDir,
		withMain:    generateMain,
	}
	switch lang {
	case langGo:
		if rootImportPath == "" {
			return fmt.Errorf("invalid import path = empty")
		}
		gs := goServer{server: sd, RootImportPath: rootImportPath}
		err = gs.generate(dir)
	case langPython:
		ps := pythonServer{server: sd}
		err = ps.generate(dir)
	default:
		return errInvalidLang
	}

	if err != nil {
		return err
	}

	if sd.APIDocsDir == "" {
		return nil
	}

	log.Infof("Generating API Docs to %v endpoint", sd.APIDocsDir)

	return apidocs.Generate(ramlBytes, filepath.Join(dir, sd.APIDocsDir))
}
Ejemplo n.º 2
0
// GenerateServer generates API server files
func GenerateServer(ramlFile, dir, packageName, lang, apiDocsDir, rootImportPath string, generateMain bool) error {
	apiDef := new(raml.APIDefinition)
	// parse the raml file
	ramlBytes, err := raml.ParseReadFile(ramlFile, apiDef)
	if err != nil {
		return err
	}

	// create directory if needed
	if err := commons.CheckCreateDir(dir); err != nil {
		return err
	}

	switch lang {
	case langGo:
		if rootImportPath == "" {
			return fmt.Errorf("invalid import path = empty")
		}
		gs := golang.NewServer(apiDef, packageName, apiDocsDir, rootImportPath, generateMain)
		err = gs.Generate(dir)
	case langPython:
		ps := python.NewServer(apiDef, apiDocsDir, generateMain)
		err = ps.Generate(dir)
	case langNim:
		ns := nim.NewServer(apiDef, apiDocsDir, dir)
		err = ns.Generate()
	default:
		return errInvalidLang
	}
	if err != nil {
		return err
	}

	if apiDocsDir == "" {
		return nil
	}

	if lang == langNim {
		apiDocsDir = "public/" + apiDocsDir
	}

	log.Infof("Generating API Docs to %v", apiDocsDir)

	return apidocs.Generate(ramlBytes, filepath.Join(dir, apiDocsDir))
}