nitcorn: use a default virtual host when none are configure to handle a request
[nit.git] / lib / nitcorn / server_config.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Classes and services to configure the server
18 #
19 # The classes of interest are `VirtualHost`, `Interface`, `Route` and `Action`
20 module server_config
21
22 # Server instance configuration
23 class ServerConfig
24 # `VirtualHost`s served by this server
25 var virtual_hosts = new VirtualHosts(self)
26
27 # Default `VirtualHost` to respond to requests not handled by any of the `virtual_hosts`
28 var default_virtual_host: nullable VirtualHost = null
29 end
30
31 # A `VirtualHost` configuration
32 class VirtualHost
33 # Back reference to the associated server configuration
34 var server_config: nullable ServerConfig = null
35
36 # Interfaces on which `self` is active
37 var interfaces = new Interfaces(self)
38
39 # Routes and thus `Action`s active on `self`
40 var routes = new Routes(self)
41
42 # Create a virtual host from interfaces as strings
43 init(interfaces: String ...)
44 do
45 for i in interfaces do self.interfaces.add_from_string i
46 end
47 end
48
49 # An interface composed of a `name`:`port`
50 class Interface
51 # Name of this interface (such as "localhost", "example.org", etc.)
52 var name: String
53
54 # The port to open
55 var port: Int
56
57 redef fun to_s do return "{name}:{port}"
58 end
59
60 # A route to an `Action` according to a `path`
61 class Route
62 # Path to this action present in the URI
63 var path: nullable String
64
65 # `Action` to activate when this route is traveled
66 var handler: Action
67 end
68
69 # Action executed to answer a request
70 abstract class Action
71 end
72
73 ### Intelligent lists ###
74
75 # A list of interfaces with dynamic port listeners
76 class Interfaces
77 super Array[Interface]
78
79 # Back reference to the associtated `VirtualHost`
80 var vh: VirtualHost
81
82 # Add an `Interface` described by `text` formatted as `interface.name.com:port`
83 fun add_from_string(text: String)
84 do
85 assert text.chars.count(':') <= 1
86
87 var words = text.split(':')
88 var name = words[0]
89 var port
90 if words.length > 1 then
91 port = words[1].to_i
92 else port = 80
93
94 add new Interface(name, port)
95 end
96 end
97
98 # A list of virtual hosts with dynamic port listeners
99 class VirtualHosts
100 super Array[VirtualHost]
101
102 # Back reference to the server config
103 var config: ServerConfig
104
105 redef fun add(e)
106 do
107 super
108
109 e.server_config = config
110 end
111 end
112
113 # A list of routes with the search method `[]`
114 class Routes
115 # Back reference to the config of the virtual host
116 var config: VirtualHost
117
118 private var array = new Array[Route]
119
120 # Add `e` to `self`
121 fun add(e: Route) do array.add e
122
123 # Remove `e` from `self`
124 fun remove(e: Route) do array.remove e
125
126 # Get the first `Route` than has `key` as prefix to its path
127 fun [](key: String): nullable Route
128 do
129 for route in array do
130 var path = route.path
131 if path == null or key.has_prefix(path) then return route
132 end
133 return null
134 end
135 end