?
Solved

Where to store cgi app's db connection string?

Posted on 2006-06-07
10
Medium Priority
?
529 Views
Last Modified: 2013-12-25
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.
e.g.
MYAPP_CONFIG=../config/myapp.cfg

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?

Thanks
Julian

0
Comment
Question by:jul17pri
  • 6
  • 4
10 Comments
 

Author Comment

by:jul17pri
ID: 16850802
Also, the MYAPP_CONFIG soluton doesn't enable the same app to run twice on the same site, and it might be necessary to run both /test and and /training and /live side-by-side.  Could always use the app folder name as the config file name e.g.
MYAPP_CONFIG=../config
... so the app uses one of...
c:\inetpub\wwwroot\SiteA\config\test .cfg
c:\inetpub\wwwroot\SiteA\config\training .cfg
c:\inetpub\wwwroot\SiteA\config\live.cfg

Alternatively, instead of environment variables, the path to the master configuration file could go in another config file in the application root folder e.g.
  /live/slave.cfg
contains
 config=c:\inetpub\wwwroot\SiteA\private\live.cfg
But now we're reading 2 files.  There must be an easier way...
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16858908
I'd vote for a config file outside the diretory tree accessable by the web server.
Then pass the path to that config as argument to your CGI.
0
 

Author Comment

by:jul17pri
ID: 16858944

How to pass the path is the main problem.  When you say 'pass the path to that config as argument', how is that done, in practice?

Thanks,
Julian
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 51

Accepted Solution

by:
ahoffmann earned 2000 total points
ID: 16863183
oops, it's a CGI called via URL, right?
Then I'd write a small wrapper to be called via URL which then calls the cgi with the path.
But it makes thinks more complicated by adding another layer. Sigh.

How about writing a small function in your cgi which uses a heuristic (anything you know how to acces a path) to find the proper configuration file? Something like:
   if exist /usr/app/my.conf then
     read it
   else if exist c:\my.conf then
     read it
   else ...
   else
      echo "dumb"
     exit
  fi
0
 

Author Comment

by:jul17pri
ID: 16867952
> Then I'd write a small wrapper to be called via URL which then calls the cgi with the path.
So each site has a tiny wrapper script that calls the executable

e.g.
cgi.exe "c:\wherever\my.conf"

Hadn't thought of that one.  Thanks for the sugestion.  Works for CGI on Windows & Linux although not for ISAPI.

But what your answer tells me is that there isn't another obvious standard way that everyone else uses but I don't know about...

...unless someone else has a better idea.  As CGI isn't a busy board I'll leave this question open until the end of the day...
0
 

Author Comment

by:jul17pri
ID: 16876518

Another option on Windows is to use a string rosource.  I think these can be compiled and linked to an executable seperately from the main build.  If not, it can be compiled into a resource DLL that sits with the executable.  This way, the connection string is loaded into memory along with the executable.  For ISAPIs it stays in memory so there's no need for a per-hit disk read.  Anyway, that's windows only so no good in this case...

Going back to config files, if the cgi/isapi is mapped to a file extension then the file being processed could be anywhere in the webspace.
e.g. if /cgi-bin/mycgi.exe is mapped to *.myhtml then the file being processed could be /this.myhtml, /sub/that.myhtml or /sub/the/other.myhtml

so the config file needs to be relative to some known, fixed point like
- c:\
- / (web root)
- the location of the executable

That narrows it down.

I still can't believe that web servers don't provide somewhere to store a database connection string, preferably encrypted on disk, loaded into memory & cached, available to the executable.  The majority of web apps need it after all.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16878116
> .. although not for ISAPI.
ISAPI is proprietary "professional" software, you already paid for it, go and use the tools this software recommends.
You do not expect that standards (like CGI) work flawless with ISAPI, do you?

If you have a working CGI on windoze also, why do you care about ISAPI?

> - / (web root)
It's a bad idea to have config files in/below DocuemntRoot
0
 

Author Comment

by:jul17pri
ID: 16892760

> You do not expect that standards (like CGI) work flawless with ISAPI, do you?
My applications interract with the web server via interfaces (c++ virtual classes) that are implemented for both ISAPI and CGI.  For each application, the Make file creates both CGI & ISAPI, and my customer can use whichever they prefer.

> If you have a working CGI on windoze also, why do you care about ISAPI?
Customers might prefer to use ISAPI because the executable is cached, no new processes are created, so it is faster. This is why ISAPI was created.  If they are running on *nix then they probably use the CGI because creating processes is not so expensive on *nix.

> It's a bad idea to have config files in/below DocuemntRoot
Good point.  When I say 'relative to...' I mean appending '..' to the web server root, so mapping

    /../config

...to...

    c:\inetpub\config

It's relative to the web server's root, but outside the webspace.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16892784
it makes sense this way
0
 

Author Comment

by:jul17pri
ID: 16892855
Thankyou for joining me in this little discussion
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses
Course of the Month16 days, 11 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question