3

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?

Blaszard
  • 30,954
  • 51
  • 153
  • 233
  • Vapor uses a different structure to an app bundle. What did you want to do with this Markdown file, serve it as-is, parse it, interpret it as a template file? – tobygriffin Oct 25 '16 at 20:48
  • @tobygriffin I just want to read the file, parse, and embed it into another template file to return to the user. – Blaszard Oct 27 '16 at 16:05
  • @Blaszard did you get this figured out? The other commenters are correct, just load the file and return. I want to mention that I don't believe `Bundle` apis are available on Linux. You'll have to do a bit more of a manual lookup as you would a macos file or something. – Logan Nov 04 '16 at 19:26

1 Answers1

4

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.

tobygriffin
  • 5,339
  • 4
  • 36
  • 61
  • Ah, thanks. I'm not sure why the use of Bundle fails, though. Is it not working for a project like Vapor? – Blaszard Nov 07 '16 at 23:39
  • 2
    That's right. A Bundle is a macOS-specific folder format that wraps a binary executable along with various other resources to represent an application. Vapor, on macOS and on Linux, simply produces the binary executable and therefore has no concept of a Bundle. – tobygriffin Nov 07 '16 at 23:41