Vapor novice... config and general question

Hi all,
New to Vaporā€¦ Semi-new to Swift but making good progress (so I think)

Went and followed the tutorial on getting HelloVapor up and running. all good.
Followed another bit of document about adding a Config/server.json for a custom hostname and port number - and then settled on perhaps that just doesnā€™t work.
Then I saw;
http://beta.docs.vapor.codes/settings/config/#quickstart
and I was keen to try out custom configsā€¦ so I created a test.json file
{
ā€œtestConfigā€: ā€œsome value hereā€,
}
and then I added this to some code.
let customValue = drop.config[ā€œserversā€, ā€œtestConfigā€]?.string ?? ā€œdefaultā€

This didnā€™t compileā€¦ not surprised.
I then realised that I donā€™t have any variable called ā€˜dropā€™. I then realised that one of the first tutorials I saw created a Droplet instance called drop.
https://www.raywenderlich.com/5023-server-side-swift-with-vapor-getting-started
BUT the HelloVapor project I had created using a different tutorial didnā€™t do that at all. Confusion ensured. No such Droplet instance at all. just a public function app with a load of variables and initialisation in it.

So a couple of questionsā€¦ how come there are different approaches ā€¦ both tutorials created a new project (AFAIK) using the same vapor new xx command and vapor xcode etc commands. One creates a new instance of Droplet and the other does not at all.

Next questionā€¦
The documentation for ā€˜configā€™ says "For Vapor applications, configuration files are expected to be nested under a top level folder named Config
So I created a folder in my Xcode project called Config and then the xxx.json file etc. For some reason I just knew this probably wasnā€™t right but didnā€™t know what else that meant.
So - where is this Config folder structure suppose to be? and for a bonus point - how am I suppose to access .config method to read my custom settings from the config file.

Hope someone can helpā€¦

ps. I have official Vapor book on routeā€¦

@0xtim Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hey @drenwick apologies Iā€™ve been on vacation!

So the issue youā€™re hitting is that youā€™ve got a mix of Vapor 2 and Vapor 3 config tutorials there. Basically the JSON config is no longer a thing, itā€™s mostly done by environment variables these days. You can use the dotenv extension in the Vapor-ext repo to make it easy to add environment variables, thereā€™s a great gist here:

1 Like

This topic was automatically closed after 166 days. New replies are no longer allowed.