Changeset 1e37648


Ignore:
Timestamp:
Aug 28, 2015, 3:29:08 PM (11 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
master
Children:
b858e6f
Parents:
851179e
Message:
  • Rewrites are now working
    • Setup is now accessable via Slim too
    • Added 404 template to Slim app
Location:
src/webservice
Files:
5 added
1 deleted
11 edited

Legend:

Unmodified
Added
Removed
  • src/webservice/CMakeLists.txt

    r851179e r1e37648  
    2121
    2222SET(WEBSERVICE_OUTPUT_DIRECTORY "${WEB_OUTPUT_DIRECTORY}/webservice")
     23SET(WEBSERVICE_OUTPUT_APP_DIRECTORY "${WEBSERVICE_OUTPUT_DIRECTORY}/${WEBSERVICE_API_VERSION}")
    2324SET(WEBSERVICE_TEST_OUTPUT_DIRECTORY "${WEB_TEST_OUTPUT_DIRECTORY}/webservice")
    2425
    … …  
    3738
    3839CONFIGURE_FILE("config/config.php.in" "${WEBSERVICE_OUTPUT_DIRECTORY}/config/config.php")
     40CONFIGURE_FILE("config/internalconfig.php.in" "${WEBSERVICE_OUTPUT_APP_DIRECTORY}/inc/internalconfig.php")
    3941# workaround to copy "hidden" file .htaccess. copy command ignores it
    40 CONFIGURE_FILE("htaccess.in" "${WEBSERVICE_OUTPUT_DIRECTORY}/.htaccess")
     42CONFIGURE_FILE("htaccess.in" "${WEBSERVICE_OUTPUT_APP_DIRECTORY}/.htaccess")
    4143
    4244FILE(GLOB src_HTDOCS "htdocs/*.*")
    4345ADD_CUSTOM_TARGET(webservice-prepare ALL
    44         COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_CURRENT_SOURCE_DIR}/htdocs" "${WEBSERVICE_OUTPUT_DIRECTORY}"
     46        COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_CURRENT_SOURCE_DIR}/htdocs" "${WEBSERVICE_OUTPUT_APP_DIRECTORY}"
    4547#       DEPENDS "${WEBSERVICE_OUTPUT_DIRECTORY}/${STAFF_WSDL_FILE}"
    4648        COMMENT "Preparing webservice"
    4749        SOURCES ${src_HTDOCS}
    4850)
    49 # ADD_CUSTOM_TARGET(webservice-install ALL
    50 #       COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_CURRENT_SOURCE_DIR}/htdocs" "${WEBSERVICE_OUTPUT_DIRECTORY}"
    51 # #     DEPENDS "${WEBSERVICE_OUTPUT_DIRECTORY}/${STAFF_WSDL_FILE}"
    52 #       COMMENT "Preparing webclient"
    53 #       SOURCES ${src_HTDOCS}
    54 # )
    55 
    56 
    57 
    58 
    59 
    60 
    61 
    62 
    6351
    6452#######################
  • src/webservice/config/config.php.in

    r851179e r1e37648  
    2020
    2121$config = [
    22         "version" => "@VERSION@",
    23         "webserviceApiVersion" => "@WEBSERVICE_API_VERSION@",
    2422        "mode" => "@WEBSERVICE_MODE@",
    25         "webserviceBaseUrl" => "http://localhost@WEBSERVICE_BASE_URL@/",
     23        "webserviceBaseUrl" => "http://localhost@WEBSERVICE_BASE_URL@@WEBSERVICE_API_VERSION@",
    2624        "spatialiteExtension" => "mod_spatialite.so",
    2725        "geoipservice" => "http://freegeoip.net/json",
  • src/webservice/htaccess.in

    r851179e r1e37648  
    2222# Requires AllowOverride All directive in your apache config
    2323RewriteEngine On
    24 RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
    25 RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
    26 RewriteRule ^@WEBSERVICE_API_VERSION@ @WEBSERVICE_BASE_URL@/index.php [QSA,L]
     24RewriteBase @WEBSERVICE_BASE_URL@@WEBSERVICE_API_VERSION@
     25RewriteCond %{REQUEST_FILENAME} !-f
     26RewriteCond %{REQUEST_FILENAME} !-d
     27RewriteRule ^ index.php$1 [QSA,NC,L]
  • src/webservice/htdocs/SiWeather/Webservice/Setup.php

    r851179e r1e37648  
    306306       
    307307        public function apiCheckWithRewrite(&$errorMessage){
    308                 $apiVersion = $this->_webserviceConfig->getConfig("webserviceApiVersion");
    309                 if(!$apiVersion || empty($apiVersion)){
    310                         $errorMessage = _("Could not determine API version");
    311                         return false;
    312                 }
    313                
    314                 return $this->apiCheck($apiVersion ."/stations", $errorMessage);
     308                return $this->apiCheck("stations", $errorMessage);
    315309        }
    316310
    … …  
    326320       
    327321        private function apiCheck($url, &$errorMessage){
    328                 $baseUrl = $this->_webserviceConfig->getConfig("webserviceBaseUrl");
     322                $baseUrl = $this->_webserviceConfig->webserviceBaseUrl();
    329323                if(!$baseUrl || empty($baseUrl)){
    330324                        $errorMessage = _("No webservice base url given!");
    … …  
    332326                }
    333327
     328                # base_url needs to end with a /, otherwise the last element is chopped
    334329                $client = new \GuzzleHttp\Client
    335330                (
    … …  
    347342                $result = $response->getStatusCode();
    348343                if($result != 200){
    349                         $errorMessage = \Translator::swGettext("Got http error %s (%s)", $result, $response->getReasonPhrase());
     344                        $errorMessage = \Translator::swGettext("Got http error %s (%s) at url %s", $result, $response->getReasonPhrase(), $baseUrl ."/". $url);
    350345                        return false;
    351346                }
  • src/webservice/htdocs/SiWeather/Webservice/WebserviceConfig.php

    r851179e r1e37648  
    2424
    2525        function __construct(){
    26                 require(__DIR__ ."/../../config/config.php");
     26                require(__DIR__ ."/../../../config/config.php");
    2727                $this->_config = $config;
    2828                $this->loadAdditionalConfiguration();
    … …  
    3535        private function loadAdditionalConfiguration(){
    3636                $this->_config["dataDirectory"] = "data";
     37               
     38                require(__DIR__ ."/../../inc/internalconfig.php");
     39                foreach(array_keys($internalconfig) as $key){
     40                        if(array_key_exists($key, $this->_config)){
     41                                error_log("Internal config entry ". $key ." already exists --> Keeping existing value and discard internal config value");
     42                        }
     43                        else{
     44                                $this->_config[$key] = $internalconfig[$key];
     45                        }
     46                }
    3747        }
    3848
    … …  
    4757                        return $this->dataDirectory();
    4858                }
     59                if($key == "webserviceBaseUrl"){
     60//                      error_log("Use dataDirectory() function to access this value");
     61                        return $this->webserviceBaseUrl();
     62                }
    4963               
    5064                return $this->_config[$key];
    … …  
    5266
    5367        public function dataDirectory(){
    54                 return __DIR__ ."/../../". $this->_config["dataDirectory"];
     68                return __DIR__ ."/../../../". $this->_config["dataDirectory"];
     69        }
     70       
     71        public function webserviceBaseUrl(){
     72                $baseUrl = $this->_config["webserviceBaseUrl"];
     73                if(substr($baseUrl, strlen($baseUrl) - 1, 1) != "/"){
     74                        $baseUrl .= "/";
     75                }
     76                return $baseUrl;
    5577        }
    5678
  • src/webservice/htdocs/inc/envsetup.php

    r851179e r1e37648  
    2020
    2121// set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ ."/../../vendor/");
    22 $composerAutoloader = require(__DIR__ ."/../../vendor/autoload.php");
     22$composerAutoloader = require(__DIR__ ."/../../../vendor/autoload.php");
    2323if(is_object($composerAutoloader)){
    2424        $composerAutoloader->add("SiWeather\\Webservice\\", __DIR__ ."/../");
  • src/webservice/htdocs/index.php

    r851179e r1e37648  
    4646        array
    4747        (
    48                 "mode" => "production"
     48                "mode" => $settings->getConfig("mode"),
    4949        )
    5050);
    51 $app->setName("siweather");
     51$app->setName("SiWeather Webservice");
    5252
    5353// Only invoked if mode is "production"
    5454$app->configureMode('production', function () use ($app) {
    5555    $app->config(array(
    56         'log.enable' => true,
    57         'debug' => false
     56        "log.enable" => true,
     57        "log.level" => \Slim\Log::WARN,
     58        "debug" => false,
    5859    ));
    5960});
    … …  
    6162$app->configureMode('development', function () use ($app) {
    6263    $app->config(array(
    63         'log.enable' => false,
    64         'debug' => true
     64        "log.enable" => true,
     65        "log.level" => \Slim\Log::DEBUG,
     66        "debug" => true
    6567    ));
    6668});
    6769
    68 // $app->notFound(function () use ($app) {
    69 //     $app->render(getConfig("webserviceBaseUrl")+ "404.html");
    70 // });
     70$app->notFound(function () use ($app) {
     71        $html = "";
     72    if($app->config("debug")){
     73        ob_start();
     74        phpinfo( INFO_VARIABLES ^ INFO_ENVIRONMENT );
     75        $html = ob_get_contents();
     76        ob_end_clean();
     77    }
     78    $app->render("404.php", array("dump" => $html));
     79});
    7180
    72 // $app->get("/stations", "stations");
     81$app->get("/setup", function () use ($app){
     82        $app->render("setup.php");
     83});
     84
    7385$app->get("/stations", "stations");
    7486$app->get("/stations/:uuid", function ($uuid){
    7587        echo "stations 123";
    7688});
     89
     90
     91
    7792$app->run();
    7893
  • src/webservice/htdocs/resources/css/webservice.css

    r851179e r1e37648  
    11.success {
    2         color: #45A545;
     2        color: #66BD66;
    33        font-weight: bold;
    44}
    … …  
    77        color: #8C1C00;
    88        font-weight: bold;
     9}
     10
     11.lierror {
     12        list-style-type: none;
     13        background-image: url("../icons/dialog-cancel.png");
     14        background-repeat: no-repeat;
     15        background-position: left center;
     16        background-size: auto 1em;
     17        padding-left: 1.5em;
     18}
     19
     20.lisuccess {
     21        list-style-type: none;
     22        background-image: url("../icons/dialog-ok-apply.png");
     23        background-repeat: no-repeat;
     24        background-position: left center;
     25        background-size: auto 1em;
     26        padding-left: 1.5em;
     27}
     28
     29.header {
     30        margin-top: 1em;
     31        padding-top: 0.5em;
     32        padding-left: 2em;
     33        color: #ACDFAC;
     34        text-align: left;
     35        font-size: 9pt;
     36        border-width: 0;
     37        border-bottom-width: 2px;
     38        border-color: #ACDFAC;
     39        border-style: solid;
     40}
     41
     42.header h1 {
     43        font-size: 16pt;
    944}
    1045
    … …  
    2661}
    2762
    28 .footnotes a {
    29         color: #ACDFAC;
     63a {
     64        color: #6BA3A3;
     65        text-decoration: none;
     66/*      font-weight: bold; */
    3067}
     68
     69a:hover {
     70        color: #6BA3A3;
     71        text-decoration: underline;
     72}
  • src/webservice/htdocs/setup.php

    r851179e r1e37648  
    1919*/
    2020
    21 require(__DIR__ ."/inc/envsetup.php");
    22 
    23 function printError($message)
    24 {
    25         echo "<div class=\"error\">\n";
    26         echo "  <h3>". _("The following error occured:") ."</h3>\n";
    27         echo $message;
    28         echo "</div>\n";
    29 }
    30 
    31 function printResult($result, $message)
    32 {
    33         $class = "success";
    34         $icon = "dialog-ok-apply.png";
    35         if(!$result){
    36                 $class = "error";
    37                 $icon = "dialog-cancel.png";
    38         }
    39         echo "<li class=\"". $class ."\"><img src=\"resources/icons/". $icon ."\"/>". $message ."</li>\n";
    40 }
     21require(__DIR__ ."/templates/setup.php");
    4122
    4223?>
    43 
    44 <html>
    45         <head>
    46                 <title><?php _("SiWeather Webservice Configuration"); ?></title>
    47                 <link rel="stylesheet" type="text/css" href="resources/css/webservice.css"/>
    48         </head>
    49         <body>
    50                 <h1><?php _("Welcome for the setup of the webservice backend for SiWeather"); ?></h1>
    51                 <ul>
    52 <?php
    53 
    54 $setup = new SiWeather\Webservice\Setup();
    55 
    56 $errorMessage = "";
    57 $result = $setup->dataDirectoryCheck($errorMessage);
    58 if($result == SiWeather\Webservice\DataDirectoryChecks::NotExist && $result != SiWeather\Webservice\DataDirectoryChecks::Success){
    59         $result = $setup->dataDirectoryCreate($errorMessage);
    60 }
    61 
    62 if($result != SiWeather\Webservice\DataDirectoryChecks::Success){
    63         printResult(false, myGettext("Data directory not accessible (%s)", $errorMessage));
    64 }
    65 else{
    66         printResult(true, _("Data directory checked"));
    67         $result = $setup->databaseDirectoryCheck($errorMessage);
    68         if($result != SiWeather\Webservice\DatabaseChecks::Success){
    69                 printResult(false, $errorMessage);
    70         }
    71         else{
    72                 printResult(true, _("Database directory checked"));
    73                 $result = $setup->databaseFileCheck($errorMessage);
    74                 if($result != SiWeather\Webservice\DatabaseChecks::Success){
    75                         printResult(false, $errorMessage);
    76                 }
    77                 else{
    78                         printResult(true, _("Database file checked"));
    79                         if(!$setup->databaseConnection($errorMessage)){
    80                                 printResult(false, $errorMessage);
    81                         }
    82                         else{
    83                                 $prefix = "Database up and running";
    84                                 $versionSqlite = $setup->databaseVersionSqlite($errorMessage);
    85                                 if($versionSqlite <= 0){
    86                                         printResult(false,  Translator::swGettext("%s (Could not determine SQLite version: %s)", $prefix, $errorMessage));
    87                                 }
    88                                 else{
    89                                         $versionSpatalite = $setup->databaseVersionSpatalite($errorMessage);
    90                                         if($versionSpatalite <= 0){
    91                                                 printResult(false, Translator::swGettext("%s (Could not determine Spatalite version: %s)", $prefix, $errorMessage));
    92                                         }
    93                                         else{
    94                                                 printResult(true, Translator::swGettext("%s (SQLite: %s, Spatalite: %s)", $prefix, $versionSqlite, $versionSpatalite));
    95                                         }
    96                                 }
    97                         }
    98                 }
    99         }
    100 }
    101 
    102 $webserviceConfig = new SiWeather\Webservice\WebserviceConfig();
    103 if(!$setup->apiCheckWithoutRewrite($errorMessage)){
    104         printResult(false, Translator::swGettext("Could not connect to webservice without rewrite (%s)", $errorMessage));
    105 }
    106 else{
    107         $url = $webserviceConfig->getConfig("webserviceBaseUrl") ."/index.php/";
    108         printResult(true, Translator::swGettext("Webservice withpout rewrite accessible at %s", "<a href=\"". $url ."\">". $url ."</a>"));
    109 }
    110 if(!$setup->apiCheckWithRewrite($errorMessage)){
    111         printResult(false, Translator::swGettext("Could not connect to webservice with rewrite (%s)", $errorMessage));
    112 }
    113 else{
    114         $url = $webserviceConfig->getConfig("webserviceBaseUrl") ."/". $webserviceConfig->getConfig("webserviceApiVersion");
    115         printResult(true, Translator::swGettext("Webservice withp rewrite accessible at %s", "<a href=\"". $url ."\">". $url ."</a>"));
    116 }
    117 
    118 ?>
    119                 </ul>
    120 <?php
    121 //      $webserviceConfig = new SiWeather\Webservice\WebserviceConfig();
    122         $apiVersion = $webserviceConfig->getConfig("webserviceApiVersion");
    123         echo "          <div class=\"footer\">\n";
    124         echo "                  ". Translator::swGettext("SiWeather webservice %s with API version %s), ", $webserviceConfig->getConfig("version"), $apiVersion) ."<br/>\n";
    125         echo "                  ". Translator::swGettext("Webservice accessible at %s/%s/ or", $webserviceConfig->getConfig("webserviceBaseUrl"), $apiVersion) ."<br/>\n";
    126         echo "                  ". $webserviceConfig->getConfig("webserviceBaseUrl") ."/index.php/\n";
    127         echo "                  <div class=\"footnotes\">\n";
    128         echo "                          ". _("This site uses icons from Oxygen icon theme &#40;License: LGPLv3,") ."<a href=\"http://www.kde.org/\">http://www.kde.org</a>&#41;\n";
    129         echo "                  </div>\n";
    130         echo "          </div>\n";
    131 ?>
    132         </body>
    133 </html>
  • src/webservice/testconfig/testconfig.php.in

    r851179e r1e37648  
    2020
    2121$testconfig = [
    22         "webserviceDirectory" => "@WEBSERVICE_OUTPUT_DIRECTORY@",
     22        "webserviceDirectory" => "@WEBSERVICE_OUTPUT_APP_DIRECTORY@",
    2323];
    2424
  • src/webservice/tests/StationsTest.php

    r851179e r1e37648  
    2929                $this->assertNotNull($this->_testconfig);
    3030               
     31                require($testconfig["webserviceDirectory"] ."/inc/envsetup.php");
    3132                require_once($testconfig["webserviceDirectory"] ."/SiWeather/Webservice/WebserviceConfig.php");
    3233               
    … …  
    4546        public function testStationRewrite()
    4647        {
    47                 $apiVersion = $this->_webserviceConfig->getConfig("webserviceApiVersion");
    48                 $this->assertInternalType('string', $apiVersion);
    49                 $this->assertTrue(!empty($apiVersion));
    50 
    51                 $stations = $this->query($apiVersion ."/stations");
     48                $stations = $this->query("stations");
    5249
    5350                $this->assertInternalType("array", $stations);
    … …  
    5653       
    5754        protected function query($url){
    58                 $baseUrl = $this->_webserviceConfig->getConfig("webserviceBaseUrl");
    59                 $this->assertInternalType('string', $baseUrl);
    60                 $this->assertTrue(!empty($baseUrl));
     55                $baseUrl = $this->_webserviceConfig->webserviceBaseUrl();
     56                $this->assertInternalType('string', $baseUrl, "baseUrl is not of type string: ". $baseUrl);
     57                $this->assertTrue(!empty($baseUrl), "baseUrl is empty: ". $baseUrl);
    6158                $client = new GuzzleHttp\Client
    6259                (
    … …  
    6865                );
    6966                $response = $client->get($url);
    70                 $this->assertNotNull($response);
    71                 $this->assertEquals($response->getStatusCode(), 200);
    72                 $this->assertEquals($response->getReasonPhrase(), "OK");
     67                $this->assertNotNull($response, "Response is empty. Url: ". $baseUrl . $url);
     68                $this->assertEquals($response->getStatusCode(), 200, "Status code != 200. Url: ". $baseUrl . $url);
     69                $this->assertEquals($response->getReasonPhrase(), "OK", "Reason phrase not empty. Url: ". $baseUrl . $url);
    7370
    7471                $body = $response->getBody();
    75                 $this->assertTrue(!empty($body));
     72                $this->assertTrue(!empty($body), "Response body is empty. Url: ". $baseUrl . $url);
    7673                $json = json_decode($body);
    77                 $this->assertNotNull($json);
     74                $this->assertNotNull($json, "Json decode returned null. Url: ". $baseUrl . $url ."\nBody: ". $body);
    7875                return $json;
    7976        }
Note: See TracChangeset for help on using the changeset viewer.