Where to store cgi app's db connection string?
Posted on 2006-06-07
What's the best way to load the database connection string into a cgi application?
The cgi app is an executable compiled from C++, written in-house. It runs on IIS (cgi and ISAPI) & Apache on Windows, Apache on *nix. Multiple sites are running the same app on the same server with different configurations.
So where to put the connection string?
- Hard-coding is out because different sites access different databases.
- Other config settings are in the database but the database is not connected yet...
- The app runs on Windows & Linux so the registry is out.
- My favourite solution would be a MYAPP_DB environment variable but apparently environment variables cannot be set on a per-website basis - shame.
So the only thing left is a configuration file. But where to put the file?
- There are multiple sites so putting it with the executable or hard-coded to 'C:\Program files\MyApp' is out.
- It can't go in the webspace (e.g. website root) because it's readable by all and it contains passwords, so that's a security risk
The best I can think of is an environment variable (or hard-code) with a path relative to the website root.
But I don't like this solution because:
- The file must be read for every hit. It would be cached by the disk controller if read frequently enough but still...Is there a way to keep the database string in memory?
- Seems a bit over-complex for such a simple problem.
This must be an archetypal problem. What do developers normally do in this situation? Is there a better way?