Merge: Basename fix
[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 ...) is old_style_init do
44 for i in interfaces do self.interfaces.add_from_string i
45 end
46 end
47
48 # An interface composed of a `name`:`port`
49 class Interface
50 # Name of this interface (such as "localhost", "example.org", etc.)
51 var name: String
52
53 # The port to open
54 var port: Int
55
56 redef fun to_s do return "{name}:{port}"
57 end
58
59 # A route to an `Action` according to a `path`
60 class Route
61 # Path to this action present in the URI
62 var path: nullable String
63
64 # `Action` to activate when this route is traveled
65 var handler: Action
66 end
67
68 # Action executed to answer a request
69 abstract class Action
70 end
71
72 ### Intelligent lists ###
73
74 # A list of interfaces with dynamic port listeners
75 class Interfaces
76 super Array[Interface]
77
78 # Back reference to the associtated `VirtualHost`
79 var vh: VirtualHost
80
81 # Add an `Interface` described by `text` formatted as `interface.name.com:port`
82 fun add_from_string(text: String)
83 do
84 assert text.chars.count(':') <= 1
85
86 var words = text.split(':')
87 var name = words[0]
88 var port
89 if words.length > 1 then
90 port = words[1].to_i
91 else port = 80
92
93 add new Interface(name, port)
94 end
95 end
96
97 # A list of virtual hosts with dynamic port listeners
98 class VirtualHosts
99 super Array[VirtualHost]
100
101 # Back reference to the server config
102 var config: ServerConfig
103
104 redef fun add(e)
105 do
106 super
107
108 e.server_config = config
109 end
110 end
111
112 # A list of routes with the search method `[]`
113 class Routes
114 # Back reference to the config of the virtual host
115 var config: VirtualHost
116
117 # Internal routes array.
118 protected var routes = new Array[Route]
119
120 # Add `e` to `self`
121 fun add(e: Route) do routes.add e
122
123 # Remove `e` from `self`
124 fun remove(e: Route) do routes.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 routes 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