Я тут потихоньку ковыряю веб-фреймворк на scala...
От: dimgel Россия https://github.com/dimgel
Дата: 25.06.09 05:51
Оценка: 18 (1)
Всем привет.

В общем, сабж. Будет опен-сорц, public domain. Там всё очень сыро, не утрясены даже некоторые ключевые архитектурные решения. Но парой примерчиков хочется поделиться. Один сегодня, один завтра-послезавтра. То, что утрясено. Пока что в виде статей, без ссылок на скачу исходников (вылью как утрясу кое-что, если появятся заинтересованные). Всё по-английски, т.к. планирую интервенцию в scala community.

файл .../lib/web/example/hello01/Main.scala
package ru.dimgel.lib.web.example.hello01

import ru.dimgel.lib.web.core
import core.request.Request
import core.response.HTMLResponse

/**
 * DISCLAIMER ONE: I have no time yet to format scaladoc blocks appropriately.
 *                 Please read all docs in source code.
 * 
 * DISCLAIMER TWO: It's all in early development phase. Examples itself do work, 
 *                 but stuff mentioned in comments probably does not yet.
 * 
 * This is simplest "Hello world!" example. To run it standalone (outside example 
 * project), ensure your WEB-INF/web.xml looks like this:
 * 
 * <?xml version="1.0" encoding="utf-8"?>
 * <!DOCTYPE web-app PUBLIC 
 *     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
 *     "http://java.sun.com/dtd/web-app_2_3.dtd">
 * <web-app>
 *     <servlet>
 *         <servlet-name>servlet</servlet-name>
 *         <servlet-class>ru.dimgel.lib.web.core.Servlet</servlet-class>
 *         <init-param>
 *             <param-name>main</param-name>
 *             <param-value>ru.dimgel.lib.web.example.hello01.Main</param-value>
 *         </init-param>
 *        </servlet>
 *        <servlet-mapping>
 *         <servlet-name>servlet</servlet-name>
 *         <url-pattern>/ *</url-pattern>
 *     </servlet-mapping>
 * </web-app>
 * 
 * NOTE: There must be no space between / and * chars in <url-pattern>. 
 *       I added space only to avoid them being parsed as nested comment.
 * 
 * Your subclass of lib.web.core.Main class is entry point into your web application.
 * The service() method dispatches incoming requests. For now, we'll just generate
 * simple HTMLResponse for all requests.
 * 
 * Lib.web framework heavily uses Scala's type system. Truly, the main design goal was
 * to enforce as much is possible via typization, to provide maximum support from IDE.
 * 
 * One of major consequences of this approach is using Scala's built-in XML support 
 * instead of external interpreted templates. In fact, there's no template system at all. 
 * I only plan to add some HTMLResponse transformations (e.g. head merging). Anyway,
 * you can (make and) use your own template engine and even introduce another response 
 * class for seamless integration with lib.web.
 * 
 * There are other response classes besides HTMLResponse. Actually, there are hierarchies
 * of requests and responses. The goal main goal is to enforce API restrictions via type
 * system. For example, access to request InputStream is provided only by RawRequest, 
 * but interface to "multipart/form-data" parts is provided by MultipartRequest.
 * The service() method can pattern-match request class (and request attributes) to
 * dispatch request, as demonstrated in the next example.
 * 
 * Response objects are created by application code during request processing. This is a 
 * functional approach - to look at service() method as function (Request) => Response. 
 * It allows easy use of response class hierarchy to provide different APIs for different 
 * response types. It's used in particluar by error handling (shown in the next example).
 * 
 * Oh, and by the way, I prefer factory methods to "new" operator. And have enforced it
 * too. Just coinciseness. If you try writing "new HTMLResponse(...)", you'll get
 * compiler error. =)
 */
class Main(servlet: core.Servlet) extends core.Main(servlet) {
  override def service(rq: Request) = HTMLResponse(rq,
    <html>
      <head>
        <title>{getClass.getCanonicalName}</title>
        <link rel="stylesheet" type="text/css" href="../../lib.web/example.css"/>
      </head>
      <body>
        <h1>Hello world!</h1>
        <p>This is simplest <span class="libweb">lib.web</span> example ever.</p>
      </body>
    </html>
  )
}
... << RSDN@Home 1.1.4 stable SR1 rev. 568>>
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.