TIER Environment Variable

All Web Services systems provide an environment variable accessible to the environment’s scripting/programming languages that identifies the service tier of the system. With the exception of our Tomcat environment, this variable is simply called “TIER”. In Tomcat, it is called “edu.purdue.itap.itso.webservices.Tier” and is accessed as a property. Regardless of the environment, the value of the variable will be one of:

Tier Values
Value Service Tier
dev Development
qa QA
prod Production

You may use this information for any tier-specific functionality you may require. However, we recommend that you keep such code to a minimum as it will not be tested except on each service tier. Good examples of things to use this for include:

  • Setting different database connection information (server, password, etc.) to be used in non-tier-specific code
  • Setting a different return URL for CAS authentication
  • Setting a watermark in the background of your site to identify development and qa sites

 

ASP.Net Coding Example

Set up your web.config file to have connection strings for each tier:

<configuration>
    <connectionStrings>
        <add name="dev" connectionString="server=devserver.itap.purdue.edu;database=mydb;uid=myuser;pwd=mydevpw" />
        <add name="qa" connectionString="server=qaserver.itap.purdue.edu;database=mydb;uid=myuser;pwd=myqapw" />
        <add name="prod" connectionString="server=prodserver.itap.purdue.edu;database=mydb;uid=myuser;pwd=myprodpw" />
    </connectionStrings>
</configuration>

In your web page where you plan to use the database, use code like this. Please note, this was written in C#.

SqlConnection conn = null;
string SQLServer = null;
// Figure out which tier this server is
string webServerTier = Environment.GetEnvironmentVariable("TIER");

try {
    // Connection String to Database
    String connString = ConfigurationManager.ConnectionStrings[webServerTier].ToString();
    conn = new SqlConnection(connString);

    // set up my SQL command
    SqlCommand cmd = new SqlCommand("SELECT FName, LName FROM NAMES", conn);

    conn.Open();

    // SQL Reader to read the information obtained from the SQL command
    SqlDataReader rdr = cmd.ExecuteReader();

    while (rdr.Read() == true) {
        String FName = (string)rdr["FName"];
        String LName = (string)rdr["LName"];

    // do something with the LName and FName variables, etc.

    }

    rdr.Close();
    rdr.Dispose();
    conn.Close();
    conn.Dispose();
    conn = null;
}
catch (Exception ex) {
    ErrorMessage.Text = ex.Message;
    ErrorMessage.Visible = true;
}

PHP Coding Example

Create a file in your DATA directory similar to the following:

<?php
/** Find what TIER we're on and set host/password */
if (array_key_exists("TIER", $_SERVER)) {
    switch ($_SERVER['TIER']) {
        case "dev":
            $db_host = 'myadmdv01.itap.purdue.edu';
            $db_name = 'DEV_DB_NAME_GOES_HERE';
            // Usually, db_name = db_username
            $db_username = $db_name;
            $db_password = 'DEV_DB_PASSWORD_GOES_HERE';
            $my_domain = 'dev.www.purdue.edu';
            break;
        case "qa":
            $db_host = 'myadmdv01.itap.purdue.edu';
            $db_name = 'QA_DB_NAME_GOES_HERE';
            // Usually, db_name = db_username
            $db_username = $db_name;
            $db_password = 'QA_DB_PASSWORD_GOES_HERE';
            $my_domain = 'qa.www.purdue.edu';
            break;
        case "prod":
            $db_host = 'myadmpr01.itap.purdue.edu';
            $db_name = 'PROD_DB_NAME_GOES_HERE';
            // Usually, db_name = db_username
            $db_username = $db_name;
            $db_password = 'PROD_DB_PASSWORD_GOES_HERE';
            $my_domain = 'www.purdue.edu';
            break;
        default:
            print "'TIER' set to unknown value \"".$_SERVER['TIER']."\"<br>\n$";
            $db_host = 'localhost';
            $db_password = '';
    }
}
else {
    print "'TIER' not set!<br>\n";
    $db_host = 'localhost';
    $db_password = '';
}

Then, where you intend to use your database in your main code, do something like:

<?php
/* include my database information */
include "path/to/data/database.php";

/* connect to the database */
$con = mysqli_connect($db_host, $db_username, $db_password, $db_name) 
           or DIE ("DATABASE FAILED TO RESPOND."); 

/* use the database connection */
...

/* close the database */
mysqli_close($con);
?>