From 54a304276fb361f95332c5c1f8c6f67026b1d96c Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Fri, 31 Jul 2015 13:43:19 +0200 Subject: made it possible to run standalone without proxying webserver: - deliver static pages - some default redirects pointing to the main landing page --- config.js | 1 + src/master.cpp | 30 +++++++++++++++++++++++++++++- src/master.hpp | 2 ++ src/strusCms.cpp | 10 ++++++++++ src/strusCms.hpp | 2 ++ 5 files changed, 44 insertions(+), 1 deletion(-) diff --git a/config.js b/config.js index b161536..df53d7c 100644 --- a/config.js +++ b/config.js @@ -14,6 +14,7 @@ "service" : { "api" : "http", + "ip" : "0.0.0.0", "port" : 8080 }, "http" : { diff --git a/src/master.cpp b/src/master.cpp index 086ebe3..bd3e5df 100644 --- a/src/master.cpp +++ b/src/master.cpp @@ -4,6 +4,8 @@ #include #include +#include + namespace apps { master::master( strusCms &cms ) @@ -28,14 +30,40 @@ void master::ini( content::master &c ) void master::register_common_pages( ) { + // only deliver static content here matching the static files only! + cms.dispatcher( ).assign( "(" + cms.media_root( ) + "/basic-profile.css)", &master::serve_file, this, 1 ); + cms.dispatcher( ).assign( "(" + cms.media_root( ) + "/images/strus_big.jpg)", &master::serve_file, this, 1 ); + + // some catch all redirects pointing to the main page + cms.dispatcher( ).assign( "/", &master::redirect_to_master, this ); + cms.dispatcher( ).assign( "", &master::redirect_to_master, this ); + + // everything else in an error cms.dispatcher( ).assign( ".*", &master::not_found_404, this ); } +void master::serve_file( std::string file_name ) +{ + std::ifstream f( ( "./" + file_name ).c_str( ) ); + if( !f ) { + not_found_404( ); + return; + } + + response( ).content_type( "application/octet-stream" ); + response( ).out( ) << f.rdbuf( ); +} + +void master::redirect_to_master( ) +{ + response( ).set_redirect_header( cms.root( ) ); +} + void master::not_found_404( ) { content::master c; ini( c ); - c.url = cms.request( ).script_name( ) + "/" + cms.request( ).path_info( ); + c.url = cms.request( ).script_name( ) + cms.request( ).path_info( ); render( "not_found_404", c ); } diff --git a/src/master.hpp b/src/master.hpp index 520f258..d195601 100644 --- a/src/master.hpp +++ b/src/master.hpp @@ -17,6 +17,8 @@ class master : public cppcms::application { master( strusCms &cms ); void ini( content::master &c ); void register_common_pages( ); + void serve_file( std::string file_name ); + void redirect_to_master( ); void not_found_404( ); }; diff --git a/src/strusCms.cpp b/src/strusCms.cpp index 3a108f5..9d7945b 100644 --- a/src/strusCms.cpp +++ b/src/strusCms.cpp @@ -36,6 +36,7 @@ strusCms::strusCms( cppcms::service &srv ) { locale_name = "en"; script = settings( ).get( "strusCms.script" ); + media = settings( ).get( "strusCms.media" ); cppdb::session sql( conn ); sql.once( setup_dbconnection ); @@ -58,4 +59,13 @@ std::string strusCms::root( std::string l ) //~ return script + "/" + l; } +std::string strusCms::media_root( std::string l ) +{ + return media; + // TODO: localization later + //~ if( l.empty( ) ) l = locale_name; + //~ return script + "/" + l; +} + + } diff --git a/src/strusCms.hpp b/src/strusCms.hpp index 285f026..83d4c8c 100644 --- a/src/strusCms.hpp +++ b/src/strusCms.hpp @@ -14,6 +14,7 @@ class strusCms : public cppcms::application { public: strusCms( cppcms::service &srv ); std::string root( std::string locale_name = "" ); + std::string media_root( std::string locale_name = "" ); public: apps::master master; @@ -25,6 +26,7 @@ class strusCms : public cppcms::application { private: std::string script; + std::string media; std::string locale_name; }; -- cgit v1.2.3-54-g00ecf