Tntnet users guide Authors: Tommi Mäkitalo, Andreas Welchlin Tntnet users guide

Tntnet users guide Authors: Tommi Mäkitalo, Andreas Welchlin Tntnet users guide 1 Table of contents 1 Concept....................................................................................................................................... 3 2 Installing Tntnet.......................................................................................................................... 3 3 Create a simple application with Tntnet......................................................................................3 4 C++-content (processing, expressions, conditional expressions)............................................... 4 5 Query-arguments (scalar/vector, untyped/typed, default-value).................................................4 6 Components................................................................................................................................ 5 7 Component-parameters............................................................................................................... 5 8 Returning from components........................................................................................................6 9 Calling components (dynamic/static)..........................................................................................6 10 Calling components from c++.....................................................................................................7 11 Include ecpp-files........................................................................................................................7 12 Declaring subcomponents...........................................................................................................8 13 Passing parameters to components..............................................................................................8 14 Defining scoped variables...........................................................................................................9 14.1 Lifetime.................................................................................................................................. 9 14.2 Scope.................................................................................................................................... 10 15 Cookies......................................................................................................................................11 16 Component attributes................................................................................................................ 12 17 Configuration............................................................................................................................ 12 18 Accessing configuration variables............................................................................................ 13 19 Logging..................................................................................................................................... 14 20 Exception handling................................................................................................................... 14 21 Savepoints................................................................................................................................. 14 22 Binary content...........................................................................................................................15 23 Upload.......................................................................................................................................15 24 HTTP-Auth............................................................................................................................... 16 25 Using c++-classes..................................................................................................................... 16 26 Creating stand alone web applications......................................................................................17 27 Some notes about multi threading.............................................................................................18 Tntnet users guide 2 1 Concept Tntnet is a application server for web applications written in c++. A web application in tntnet is written with a template-language, which embeds c++- processing-instructions in html-pages. You can use external classes or libraries in these pages. That way programmers can concentrate on the html- result, when creating content and put processing in c++-classes. Applications written with the template-language called ecpp are compiled into c++-classes and linked into a dynamically loaded shared library. This is done at compile-time – not at runtime, like other template-systems often do. On runtime there is no compiler needed. 2 Installing Tntnet Tntnet runs on Linux/Unix. You need a c++-compiler to compile Tntnet and also to compile your web applications. As a prerequisite cxxtools (which is available through the Tntnet-homepage http://www.tntnet.org) is needed. Cxxtools is a collection of useful c++-classes. To install tntnet you have to: 1. install cxxtools 2. unpack the sources with “tar xzf tntnet-version.tar.gz” 3. cd to the source-directory “cd tntnet-version.tar.gz” 4. run “./configure” 5. run “make” 6. run “make install” This installs: The application server and tools for 1. tntnet – the web application server 2. ecppc – the ecpp-compiler 3. ecppl – the ecpp-language-extractor for internationalization 4. ecppll – the ecpp-language-linker for internationalization 5. some shared libraries 6. tntnet-config – a script, which gives you information about the installed Tntnet and helps you setting up a simple project. 3 Create a simple application with Tntnet The simplest way to create a tntnet-application is to use the tntnet-config- script. Just run “tntnet-config –project=projectname”. This creates a directory with the name of the project and: 1. projectname.ecpp – your first ecpp-file 2. Makefile – a simple makefile to build the application 3. tntnet.conf – a basic configuration file for tntnet Tntnet users guide 3 4. tntnet.properties – a basic configuration file for logging-configuration It used to be necessary to use a valid C++-class name as a project name, since it was used as a class name internally. This is not true for tntnet 2.0 any more. To build the project, change to the directory and run “make”. This runs the necessary steps to create a running application. You get a shared library projectname.so, which contains the web application. To run it enter “tntnet tntnet.conf” and navigate your browser to http://localhost:8000/projectname. You get a simple Web-page. 4 C++-content (processing, expressions, conditional expressions) There are some simple tags, with which you can embed c++-content into the page. The most important are processing-tags and output-tags. With processing-tags you insert c++-code into the page, which is processed on runtime. There are 3 alternatives to insert code: The most verbose version is <%cpp>...some c++-code...</%cpp>. A newline after </%cpp> is ignored. <{ starts an inline-block, which is terminated by }>. A newline after the closing tag is not ignored, but passed to the browser. The character '%' in the first column starts also c++-code until the end of line. A backslash disables the interpretation of the next character. You can embed the result of a c++-expression with <$ expr $> into the html- code. It is printed into the page. The type of the expression needs to have a output-operator for std::ostream. The output is automatically translated into html. Characters with special meaning in html are printed as their html entity counterparts. Often you need to print something depending on a condition. You can put a if- statement around it. As a shortcut there is a special tag <? cond ? output ?>. “cond” is evaluated as a c++-expression. If the result of this expression is true, output is printed. The output is translated like <$...$>. Another useful tag is <# ... #>, which is just a comment. The content is skipped by the ecpp-compiler. In C++-mode reply.out() returns a std::ostream, which writes text to the html- page. reply.sout() returns a std::ostream, which escapes characters with special meaning in html before writing to the page. 5 Query-arguments (scalar/vector, untyped/typed, default- value) Web-applications need to interact with the user. Therefore they send a html- form to the user. After the user submits the form, the application needs to Tntnet users guide 4 interpret the content of the form. To support this, ecpp has a tag: <%args>...</%args>. Between this pair you define the arguments of the form. Each argument is terminated with ';'. Ecpp generates c++-variables of type std::string, containing the content of the form. You can precede arguments with a c++-type to convert the parameter automatically into this type. This is done using the input-operator of std::istream. Since tntnet 2.0 a parameter defined with the type bool is interpretation as true for all non empty strings. This makes it easy to check, if a specific submit button was pressed. Ecpp supports multiple input-tags with the same name, e.g. multiple checkboxes or select-tag with attribute multiple. By appending [] to your argument a std::vector and a typedef will be generated. Writing name[] will be compiled to a typedef name_type and a vector name. Single arguments can have a default value using the syntax variable = default_value;. Examples: <%args> name; street; city = “New York”; int age; // content of text-input is converted to int bool button1; // true, if the submit button with name “button1” was used for // submission int sport[]; // multiple checkboxes with the same name </%args> <# use the vector like this: #> % for (sport_type::const_iterator it = sport.begin(); it != sport.end(); ++it) { <$ *it $> % } 6 Components Ecpp-pages are called components. They are identified by their name. The name is composed of the local-name and the library-name divided with '@'. The local name is by default the filename of the ecpp file without path and extension, but may be changed to whatever needed by passing a name using -n to the ecpp compiler ecppc. The -n switch can be used to pass full path names as component names. Components, which are called by Tntnet are called top-level-components. Components can contain internal subcomponents. The subcomponent-name is appended to the class-name divided by a dot. Subcomponents can't be called directly from tntnet but only explicitly using a component call. Examples are: mycomp@app Tntnet users guide 5 identifies a component with the name “mycomp” in the shared library “app.so” mycomp.subcomp@app identifies a subcomponent “subcomp” of “mycomp@app” foo/bar/baz@webapp identifies the component “foo/bar/baz” in the shared library “webapp.so” 7 Component-parameters Every component has 3 parameters called “request”, “reply” and “qparam”. The “request”-parameter contains information about the request, received from the browser. This includes information about http-headers, the peer-ip, cookies or multipart-components. The parameter is an instance of the class “tnt::httpRequest”. The “reply”-parameter is used to build the answer to the request. The reply- object is an instance of the class “tnt::httpReply”. It contains methods to manipulate the http-headers and an output-stream for writing to the http- body. “qparam” specifies the query-parameters of the component. For top-level- components it contains the query-parameters of the form. As described above, the parameters are normally accessed using the <%args>-block. 8 Returning from components Top-level-components return the http-response-code as an unsigned integer. If a explicit return-statement is not specified the constant HTTP_OK is returned. Constants, which define http-response-codes are defined in the header tnt/http.h. As a special case the web application may return the constant DECLINED. It tells Tntnet, that the current components is not interested in processing the reply further and Tntnet should continue in the url mapping table (see configuration section). Exceptions may be thrown if something goes wrong. All exceptions are catched by Tntnet and sent with an internal server error to the client. A exception of class tnt::HttpError carry a http return code, which is used, when this type is thrown. Additional http headers can be set also. This is useful for generating special http responses like redirects. But to make it even easier there are special methods for redirects and also authorization. Both are handled using exceptions. To make a redirect, you may call reply.redirect with the new location as a parameter: Example: <%cpp> return reply.redirect(“/new/location.html”); </%cpp> Tntnet users guide 6 sends a http-redirect message to the client Note that that the redirect method never returns but throws a tnt::HttpError. It is still defined as returning an unsigned, so that the return uploads/Philosophie/ tntnet-users-guide.pdf

  • 173
  • 0
  • 0
Afficher les détails des licences
Licence et utilisation
Gratuit pour un usage personnel Attribution requise
Partager