Heroku: how to read file?

302 views Asked by At

I have the following project layout (contracted):

project_root
└── Sources
    └── App
        ├── main.swift
        └── Models
            └── SQL
                └── createTables.sql

I'm trying to read the createTables.sql file at runtime but FileManager.fileExists() says it doesn't exist. I got the path by using #file in main.swift:

let path = URL(fileURLWithPath: #file).deletingLastPathComponent().path + "/Models/SQL/createTables.sql"

Printing out the path, it seems to be correct. I'm guessing that the source tree is inaccessible because of Heroku's ephemeral filesystem. How can I get around this and read the file at runtime?

1

There are 1 answers

0
tobygriffin On BEST ANSWER

Use drop.workdir to get the project directory after compile time.

try String(contentsOfFile: drop.workDir + "Resources/SQL/createTables.sql")

Note I've also moved your file out of Sources and into Resources. Vapor's project layout isn't set in stone, but currently the rule appears to be that the former is for Swift files and the latter for ancillary files.