1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Classes and services to configure the server
19 # The classes of interest are `VirtualHost`, `Interface`, `Route` and `Action`
22 # Server instance configuration
24 # `VirtualHost`s served by this server
25 var virtual_hosts
= new VirtualHosts(self)
27 # Default `VirtualHost` to respond to requests not handled by any of the `virtual_hosts`
28 var default_virtual_host
: nullable VirtualHost = null is writable
31 # A `VirtualHost` configuration
33 # Back reference to the associated server configuration
34 var server_config
: nullable ServerConfig = null
36 # Interfaces on which `self` is active
37 var interfaces
= new Interfaces(self)
39 # Routes and thus `Action`s active on `self`
40 var routes
= new Routes(self)
42 # Create a virtual host from interfaces as strings
43 init(interfaces
: String ...) is old_style_init
do
44 for i
in interfaces
do self.interfaces
.add_from_string i
48 # An interface composed of a `name`:`port`
50 # Name of this interface (such as "localhost", "example.org", etc.)
56 redef fun to_s
do return "{name}:{port}"
59 # A route to an `Action` according to a `path`
61 # Path to this action present in the URI
62 var path
: nullable String
67 if path
!= null then self.path
= "/" / path
70 # `Action` to activate when this route is traveled
74 # Action executed to answer a request
78 ### Intelligent lists ###
80 # A list of interfaces with dynamic port listeners
82 super Array[Interface]
84 # Back reference to the associtated `VirtualHost`
85 var virtual_host
: VirtualHost
87 # Add an `Interface` described by `text` formatted as `interface.name.com:port`
88 fun add_from_string
(text
: String)
90 assert text
.chars
.count
(':') <= 1
92 var words
= text
.split
(':')
95 if words
.length
> 1 then
99 add
new Interface(name
, port
)
103 # A list of virtual hosts with dynamic port listeners
105 super Array[VirtualHost]
107 # Back reference to the server config
108 var config
: ServerConfig
114 e
.server_config
= config
118 # A list of routes with the search method `[]`
120 # Back reference to the config of the virtual host
121 var config
: VirtualHost
123 # Internal routes array.
124 protected var routes
= new Array[Route]
127 fun add
(e
: Route) do routes
.add e
129 # Remove `e` from `self`
130 fun remove
(e
: Route) do routes
.remove e
132 # Get the first `Route` than has `key` as prefix to its path
133 fun [](key
: String): nullable Route
135 for route
in routes
do
136 var path
= route
.path
137 if path
== null or key
.has_prefix
(path
) then return route