Search code examples
swiftxcodensbundlevapor

Where should I add my own Markdown file in Vapor project?


I want to add my own Markdown file, a file named profile.md. However, I don't know where I should add the file in my Vapor project, which is developed in Xcode.

I tried adding it to Sources/ or just the root directory, but it cannot be searched by Bundle.main.url(forResource:) function, like:

guard let url = Bundle.main.url(forResource: "profile", withExtension: ".md") else { ... }

Also, I found that I already added it under Copy Bundle Resources tab.

In the normal app (for iOS and macOS), the function above works properly.

How can I add my own file to be used in Vapor project developed in Xcode?


Solution

  • This is a very basic example of loading the contents of a file from a custom location using Foundation rather than Vapor's template view loading mechanism, and returning it as a text response.

    drop.get { req in
      let templateString = try String(contentsOfFile: drop.workDir + "Resources/profile.md")
      return templateString
    }
    

    I'm not sure exactly what you mean by "parse, and embed it into another template file" but you'd put that bit in between the lines above.