Archive for the configuration Category

Afin de mieux appréhender ViM, j’ai fait une présentation à ceux qui étaient intéressés chez AF83. Je propose les slides pour ceux qui veulent garder une trace, ou se faire une idée.

Click to continue reading

I looked for a nice clean class implementation in PHP for embedding videos from youtube and such and could not find anything that was nice enough. So here is my take on embedding videos in php.
As the embed code is reconstructed it should be safe enough put probably some more checks need to be done after extracting the id to see there is nothing hostile there.

Configuration:
This class requires the SpyC library to read the cobnfiguration file. The library is assumed to be in the SITEBASE/include/yaml/ directory.

PHP:
  1. define('SITEBASE', '/var/www/mysite'); // path to the root of the site (not forcefully public)
  2. define('VIDEO_EMBED_CONFIG_FILE', SITEBASE.'/config/video_embed.yaml'); //path of video embed config file
  3. define('DEBUG', true); //to activate debug mode and false for production usage. it will write to a log file when something goes wrong but should not produce exceptions in production enviroment

USAGE
note: The embed code may either be embed or url

PHP:
  1. $embed='http://www.youtube.com/watch?v=h2EUW_rgDVo';
  2. $videoEmbed = new VideoEmbed($embed); //optional width and height may be passed to the constructor
  3. print($videoEmbed->embed);
  4. $videoEmbed->width = 240; // resize
  5. $videoEmbed->height = 120;
  6. print($videoEmbed->embed); // resized video
  7. print($videoEmbed->thumb); // get thumb url

the other public properties are: ->id, ->type, ->url, ->width and ->height
note that magic getters and setters are used to make ->id, ->type, ->url read only

TODO: thumbnails should be cached locally
TODO: Create unit tests (for the moment test_VideoEmbed(); does some testing)

The video services are configured in the configuration file (video_embed.yaml), form:

---
embedTemplate: default embed template
defaultWidth: default width
defaultHeight: default height
services:
    servicename:
        urlPattern: pattern to distinguis between services
        embedUrlTemplate: template (used with sprintf) for construvting the player url
        thumbnailUrlTemplate: template to find thumnbail by video ID (used with sprintf)
        thumbnailUrlExtractPattern: if present the thumbnailUrlTemplate is assumed to be a text resource, and this is a regexp to extract the thumnbail from it
        extractPattern: regexp pattern to extract
        apiUrl: api url (not used for the currently supported services)
        defaultWidth: service default width
        defaultHeight: service default height
		embedTemplate: specific embed template (not used for the current supported services)

Example configuration file for google video youtube and dailymotion (if you configure it for other services please post the config)... :)

---
embedTemplate: <object width="%2$s" height="%3$s" ><param name="movie" value="%1$s"></param><param name="wmode" value="transparent"></param><embed src="%1$s" type="application/x-shockwave-flash" wmode="transparent" width="%2$s" height="%3$s"></embed></object>
defaultWidth: 425
defaultHeight: 350
services:
    youtube:
        urlPattern: youtube.com
        embedUrlTemplate: http://www.youtube.com/v/%1$s&rel=1
        thumbnailUrlTemplate: http://i.ytimg.com/vi/%1$s/default.jpg
        extractPattern: /youtube\.com\/(v\/|watch\?v=)([\w\-]+)/
        apiUrl: http://www.youtube.com/api2_rest
        defaultWidth: 425
        defaultHeight: 350
    google:
        urlPattern: video.google
        extractPattern: /docid=([^&]*)/i
        embedUrlTemplate: http://video.google.com/googleplayer.swf?docId=%1$s
        thumbnailUrlTemplate: http://video.google.com/videofeed?docid=%s
        thumbnailUrlExtractPattern: '/

And here is the code :Complete class code with readme and configuration file

The code referes to a debug function, you can use:

PHP:
  1. function debug_log($msg, $file = "debug")
  2. {
  3.     $dbg = "";
  4.     if (SITE != '[PROD]') {
  5.         $bts = debug_backtrace();
  6.         foreach($bts as $bt) {
  7.             $path = str_replace(SITEBASE, '', $bt ['file']);
  8.             $dbg .= $path . " line " . $bt['line'] . " (function " . $bt['function'] . ")\n";
  9.         }
  10.         $handle = fopen(SITEBASE . "/../log/{$file}.log", "a");
  11.         fwrite($handle, strftime("%Y-%m-%d %H:%M:%S  ") . $dbg . $msg . "\n------------------\n");
  12.         fclose($handle);
  13.     }
  14. }

Click to continue reading

How to run several instances of a rails app with a single code base?

What is the problem?

Sometimes you want to run several instances of the same application on your server. How to easily do this in rails?

The solution :)

Rails offers us the environments, allowing to run an application under different status: development, test or production environment.

We just have to create in the database.yml a specific database connection for each new environment:

prod1:
  adapter: mysql
  database: prod_one
  username: root
  password: password

and for another instance

prod2:
  adapter: mysql
  database: prod_two
  username: root
  password: password

and to create a specific configuration file 'prod1.yml' in the directory config/environments/.

After this, you just generate the database schema with the following command:

rake db:migrate RAILS_ENV=prod1

And run the server on a specified port:

./script/server -p 4001 -e prod1

Et voilà! an instance running on the specified port (4000).

If you want another one (let's say prod2).

Let's create the database instance... rake db:migrate RAILS_ENV=prod2

./script/server -p 4002 -e prod2 ... and another instance of the same code base is now running on port 4002.

Bliss.

Click to continue reading

Sometimes (often) you would want your script to have different configurations based on the environment in which it runs (dev, test, prod). Sometimes the same script may even be run from the console and you really want to have a different configuration for that. Now you may run into some trouble using just the $_SERVER["HTTP_HOST"] variable. Even more so if you are running behind an proxy (such as mod_proxy for apache).

Here is a small snippet that will help you cover almost every instance... and tell you what is the execution context of the script so you can change the db setup, turn on or off the debugging etc...

Note that in this case the $vhost here may very well contain a list of hosts separated by commas.

PHP:
  1. if ((isset($_SERVER["SESSIONNAME"]) && $_SERVER["SESSIONNAME"] =="Console") || (isset ($_SERVER["TERM"])))
  2. {
  3.     $vhost="console";
  4. } else
  5.     {
  6.     if (isset($_SERVER["HTTP_X_FORWARDED_HOST"])){$vhost=$_SERVER["HTTP_X_FORWARDED_HOST"];}
  7.         else
  8.             {
  9.             if (isset ($_SERVER["HTTP_HOST"])) $vhost = $_SERVER["HTTP_HOST"];
  10.                 else 
  11.             $vhost = $_SERVER["SERVER_NAME"];
  12.             }
  13.     }
  14. switch($vhost){
  15.     case 'myproductionhost.com':
  16.         //setup configuration   
  17.     break;
  18.     case 'myproductionhost.com, someotherhostusingmodproxy.com':
  19.         //setup configuration
  20.     break;
  21.     case 'mytesthost.com':
  22.         //setup configuration
  23.     break;
  24.     case 'console':
  25.         //setup configuration
  26.     break;
  27.     case 'locahost':
  28.         //setup configuration
  29.     break;
  30.     }

Click to continue reading

Creative Commons License
This work is licensed under a Creative Commons Attribution 2.0 License.