Skip to content

Commit

Permalink
added processing time to response. cleaned files
Browse files Browse the repository at this point in the history
  • Loading branch information
nikirago committed Feb 6, 2020
1 parent baa503e commit f0b163c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 154 deletions.
109 changes: 3 additions & 106 deletions controllers/fsl_controllers.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,132 +8,29 @@

function process_time(){
$time = number_format( microtime(true) - LIM_START_MICROTIME, 6);
return("<BR>Controller Function Called: " . option('routecallback') . "<BR>Request processed in $time seconds");
return($time);
}

function hello_world()
{

fsl_session_set('crop','Yummy Limonade');
set_or_default('name', params('who'), "everybody");
$time = number_format( microtime(true) - LIM_START_MICROTIME, 6);

return html("Session Data Set: Yummy Limonade<BR>". process_time());
}

/*
*
* This is an example on how to create and decode JWT Tokens
*
*/

function jwt()
{
$token = array();
$token['id'] = "test123";
$testjwt = fsl_jwt_encode($token, "testkey");
$jwtdecode = fsl_jwt_decode($testjwt,"testkey");
$time = number_format( microtime(true) - LIM_START_MICROTIME, 6);
return html("Token To Encode: " . $token['id'] ." <BR>JWT: $testjwt<BR>Decoded JWT: " . $jwtdecode->id . "<BR>". process_time());
}

/*
*
* This is an example on how to make a RESTful JSON Response and Set A Status Code
* This is the jokes function
*
*/

function api()
{
//get jokes

$f_contents = file("controllers/jokes.txt");
//$f_contents = file("/var/www/yesdev/gitprojects/dad-jokes_microservice/controllers/jokes.txt");
$line = $f_contents[rand(0, count($f_contents) - 1)];
//explode line into array
$line = explode("<>", $line);
$arr = array('Joke' => array('Opener' => $line[0], 'Punchline' => $line[1]));
// status(202); //returns HTTP status code of 202
$arr = array('Joke' => array('Opener' => $line[0], 'Punchline' => $line[1], 'Processing Time' => process_time()));
status(202); //returns HTTP status code of 202
return json($arr);
}




function showip()
{
$ip = $_SERVER['REMOTE_ADDR'];
set_or_default('name', params('who'), "everybody");

//session data example

$session = (empty(fsl_session_check('crop'))) ? "No session exists" : fsl_session_check('crop');

//Encryption Example
$estring = fsl_encrypt($session);
$dstring = fsl_decrypt($estring);



return html("Your IP is $ip.<BR>Your session data: $session. <BR>Session Data encrypted.<BR>Encrypt session data: $estring<BR>Session Data decrypted.<BR>Decrypted session data: $dstring <BR>" . process_time());
}

function kill_session()
{
set_or_default('name', params('who'), "everybody");
session_destroy();
return html("Session Is Destroyed.<BR>" . process_time());
}

function welcome()
{
set_or_default('name', params('name'), "everybody");
return html("html_welcome");
}

function are_you_ok($name = null)
{
if(is_null($name))
{
$name = params('name');
if(empty($name)) halt(NOT_FOUND, "Undefined name.");

}
set('name', $name);
return html("Are you ok $name ?<BR>". process_time());
}

function how_are_you()
{
$name = params('name');
if(empty($name)) halt(NOT_FOUND, "Undefined name.");
# you can call an other controller function if you want
if(strlen($name) < 4) return are_you_ok($name);
set('name', $name);
return html("I hope you are fine, $name.<BR>". process_time());
}

function image_show()
{
$ext = file_extension(params('name'));
$filename = option('public_dir')."/".basename(params('name'), ".$ext");
if(params('size') == 'thumb') $filename .= ".thb";
$filename .= '.jpg';

if(!file_exists($filename)) halt(NOT_FOUND, "$filename doesn't exists");
render_file($filename);
}

function image_show_jpeg_only()
{
$ext = file_extension(params(0));
$filename = option('public_dir').params(0);
if(params('size') == 'thumb') $filename .= ".thb";
$filename .= '.jpg';

if(!file_exists($filename)) halt(NOT_FOUND, "$filename doesn't exists");
render_file($filename);
}

?>
49 changes: 1 addition & 48 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,40 +34,11 @@ function before($route)
#
# HTTP Method | Url path | Controller function
# -------------+-------------------+-------------------------------------------
# GET | / | hello_world
# GET | / | api


//basic hello world
dispatch('/', 'api');
/*
//example showing a json REST response
dispatch('/api', 'api');
//example showing JWT usage
dispatch('/jwt', 'jwt');
//show session
dispatch('/showip/:what/:who', 'showip');
//kill session
dispatch('/kill/:who', 'kill_session');
//HTTP POST route example. FSL also supports PUT, PATCH, DELETE
dispatch_post('/welcome/:name', 'welcome');
//other random examples
dispatch('/are_you_ok/:name', 'are_you_ok');
dispatch('/how_are_you/:name', 'how_are_you');
dispatch('/images/:name/:size', 'image_show');

dispatch('/*.jpg/:size', 'image_show_jpeg_only');
*/
##############################################################################
# run after function
##############################################################################
Expand Down Expand Up @@ -104,24 +75,6 @@ function after($output, $route)
# Layouts are autoloaded from views directory or can be referended
# as a function like below.

function html_my_layout($vars){ extract($vars);?>

<!doctype html>
<html lang="en">
<body>
Hello world!
</body>
</html>

<?php }

function html_welcome($vars){ extract($vars);?>
<h3>Hello <?php echo $name?>!</h3>
<p><a href="<?php echo url_for('/how_are_you/', $name)?>">How are you <?php echo $name?>?</a></p>
<hr>
<p><a href="<?php echo url_for('/images/soda_glass.jpg')?>">
<img src="<?php echo url_for('/images/soda_glass.jpg/thumb')?>"></a></p>
<?php }

##############################################################################
# custom error declaration
Expand Down

0 comments on commit f0b163c

Please sign in to comment.