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
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 ...)
45 for i
in interfaces
do self.interfaces
.add_from_string i
49 # An interface composed of a `name`:`port`
51 # Name of this interface (such as "localhost", "example.org", etc.)
57 redef fun to_s
do return "{name}:{port}"
60 # A route to an `Action` according to a `path`
62 # Path to this action present in the URI
63 var path
: nullable String
65 # `Action` to activate when this route is traveled
69 # Action executed to answer a request
73 ### Intelligent lists ###
75 # A list of interfaces with dynamic port listeners
77 super Array[Interface]
79 # Back reference to the associtated `VirtualHost`
82 # Add an `Interface` described by `text` formatted as `interface.name.com:port`
83 fun add_from_string
(text
: String)
85 assert text
.chars
.count
(':') <= 1
87 var words
= text
.split
(':')
90 if words
.length
> 1 then
94 add
new Interface(name
, port
)
98 # A list of virtual hosts with dynamic port listeners
100 super Array[VirtualHost]
102 # Back reference to the server config
103 var config
: ServerConfig
109 e
.server_config
= config
113 # A list of routes with the search method `[]`
115 # Back reference to the config of the virtual host
116 var config
: VirtualHost
118 private var array
= new Array[Route]
121 fun add
(e
: Route) do array
.add e
123 # Remove `e` from `self`
124 fun remove
(e
: Route) do array
.remove e
126 # Get the first `Route` than has `key` as prefix to its path
127 fun [](key
: String): nullable Route
129 for route
in array
do
130 var path
= route
.path
131 if path
== null or key
.has_prefix
(path
) then return route