// 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)) }
func TestServer(t *testing.T) { Convey("server generator", t, func() { targetdir, err := ioutil.TempDir("", "") So(err, ShouldBeNil) Convey("Congo python server", func() { apiDef := new(raml.APIDefinition) _, err = raml.ParseReadFile("../fixtures/congo/api.raml", apiDef) So(err, ShouldBeNil) server := Server{ APIDef: apiDef, Title: apiDef.Title, APIDocsDir: "apidocs", WithMain: true, } err = server.Generate(targetdir) So(err, ShouldBeNil) // check drones API implementation s, err := testLoadFile(filepath.Join(targetdir, "drones.py")) So(err, ShouldBeNil) tmpl, err := testLoadFile("../fixtures/congo/python_server/drones.py") So(err, ShouldBeNil) So(s, ShouldEqual, tmpl) // check deliveries API implementation s, err = testLoadFile(filepath.Join(targetdir, "deliveries.py")) So(err, ShouldBeNil) tmpl, err = testLoadFile("../fixtures/congo/python_server/deliveries.py") So(err, ShouldBeNil) So(s, ShouldEqual, tmpl) // check main file s, err = testLoadFile(filepath.Join(targetdir, "app.py")) So(err, ShouldBeNil) tmpl, err = testLoadFile("../fixtures/congo/python_server/app.py") So(err, ShouldBeNil) So(s, ShouldEqual, tmpl) }) Reset(func() { os.RemoveAll(targetdir) }) }) }
// 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)) }
func TestLibrary(t *testing.T) { Convey("Library usage in server", t, func() { targetDir, err := ioutil.TempDir("", "") So(err, ShouldBeNil) apiDef := new(raml.APIDefinition) _, err = raml.ParseReadFile("../fixtures/libraries/api.raml", apiDef) So(err, ShouldBeNil) server := Server{ APIDef: apiDef, Title: apiDef.Title, WithMain: true, } err = server.Generate(targetDir) So(err, ShouldBeNil) rootFixture := "../fixtures/libraries/python_server" checks := []struct { Result string Expected string }{ {"Place.py", "Place.py"}, {"configs.py", "configs.py"}, {"libraries/security/oauth2_Dropbox.py", "libraries/security/oauth2_Dropbox.py"}, {"libraries/files/Directory.py", "libraries/files/Directory.py"}, } for _, check := range checks { s, err := testLoadFile(filepath.Join(targetDir, check.Result)) So(err, ShouldBeNil) tmpl, err := testLoadFile(filepath.Join(rootFixture, check.Expected)) So(err, ShouldBeNil) So(s, ShouldEqual, tmpl) } Reset(func() { os.RemoveAll(targetDir) }) }) }