Esempio n. 1
0
func assembleRoutes() {
	var err error

	// If bundling is enabled, read the RAML
	// from bundle.go
	if os.Getenv("BUNDLE_ASSETS") == "1" {
		ramlFile := os.Getenv("RAMLFILE_NAME")

		if ramlFile == "" {
			ramlFile = "api.raml"
		}

		bundle, err := CardBundle.Open(ramlFile)
		if err != nil {
			log.Fatal(err)
		}
		data, err := ioutil.ReadAll(bundle)
		if err != nil {
			log.Fatal(err)
		}
		err = yaml.Unmarshal(data, &api)
		if err != nil {
			log.Fatal(err)
		}
		// Otherwise, read the file from the filesystem.
	} else {
		ramlPath := os.Getenv("RAMLFILE_PATH")

		if ramlPath == "" {
			ramlPath = "api.raml"
		}

		api, err = ramlapi.ProcessRAML(ramlPath)
		if err != nil {
			log.Fatal(err)
		}
	}
	journal.LogChannel("raml-processor", fmt.Sprintf("processing API spec for %s", api.Title))
	journal.LogChannel("raml-processor", fmt.Sprintf("base URI at %s", api.BaseUri))
	ramlapi.Build(api, routerFunc)
}
Esempio n. 2
0
func assembleRoutes(r *mux.Router, f string) {
	var err error
	// If bundling is enabled, read the RAML
	// from bundle.go
	if os.Getenv("BUNDLE_ASSETS") != "" {
		bundle, _ := MainBundle.Open("api.raml")
		data, _ := ioutil.ReadAll(bundle)
		err = yaml.Unmarshal(data, &api)
		if err != nil {
			log.Fatal(err)
		}
		// Otherwise, read the file from the filesystem.
	} else {
		api, err = ramlapi.ProcessRAML(f)
		if err != nil {
			log.Fatal(err)
		}
	}
	journal.LogChannel("raml-processor", fmt.Sprintf("processing API spec for %s", api.Title))
	journal.LogChannel("raml-processor", fmt.Sprintf("base URI at %s", api.BaseUri))
	ramlapi.Build(api, routerFunc)
}
Esempio n. 3
0
// Parse a RAML file. Returns a raml.APIDefinition value or an error if
// everything is something went wrong.
// This is the main entry point to the RAML parser.
func ParseFile(filePath string) (*APIDefinition, error) {

	// Get the working directory
	workingDirectory, fileName := filepath.Split(filePath)

	// Read original file contents into a byte array
	mainFileBytes, err := readFileContents(workingDirectory, fileName)

	if err != nil {
		return nil, err
	}

	// Get the contents of the main file
	mainFileBuffer := bytes.NewBuffer(mainFileBytes)

	// Verify the YAML version
	var ramlVersion string
	if firstLine, err := mainFileBuffer.ReadString('\n'); err != nil {
		return nil, fmt.Errorf("Problem reading RAML file (Error: %s)", err.Error())
	} else {

		// We read some data...
		if len(firstLine) >= 10 {
			ramlVersion = firstLine[:10]
		}

		// TODO: Make this smart. We probably won't support multiple RAML
		// versions in the same package - we'll have different branches
		// for different versions. This one is hard-coded to 0.8.
		// Still, would be good to think about this.
		if ramlVersion != "#%RAML 0.8" {
			return nil, errors.New("Input file is not a RAML 0.8 file. Make " +
				"sure the file starts with #%RAML 0.8")
		}
	}

	// Pre-process the original file, following !include directive
	preprocessedContentsBytes, err :=
		preProcess(mainFileBuffer, workingDirectory)

	if err != nil {
		return nil,
			fmt.Errorf("Error preprocessing RAML file (Error: %s)", err.Error())
	}

	//pretty.Println(string(preprocessedContentsBytes))

	// Unmarshal into an APIDefinition value
	apiDefinition := new(APIDefinition)
	apiDefinition.RAMLVersion = ramlVersion

	// Go!
	err = yaml.Unmarshal(preprocessedContentsBytes, apiDefinition)

	// Any errors?
	if err != nil {

		// Create a RAML error value
		ramlError := new(RamlError)

		// Copy the YAML errors into it..
		if yamlErrors, ok := err.(*yaml.TypeError); ok {
			populateRAMLError(ramlError, yamlErrors)
		} else {
			// Or just any other error, though this shouldn't happen.
			ramlError.Errors = append(ramlError.Errors, err.Error())
		}

		return nil, ramlError
	}

	PostProcess(apiDefinition)

	// Good.
	return apiDefinition, nil
}