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;
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.
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.