Merge: doc: fixed some typos and other misc. corrections
[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 is writable
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 init
65 do
66 var path = path
67 if path != null then self.path = "/" / path
68 end
69
70 # `Action` to activate when this route is traveled
71 var handler: Action
72 end
73
74 # Action executed to answer a request
75 abstract class Action
76 end
77
78 ### Intelligent lists ###
79
80 # A list of interfaces with dynamic port listeners
81 class Interfaces
82 super Array[Interface]
83
84 # Back reference to the associtated `VirtualHost`
85 var virtual_host: VirtualHost
86
87 # Add an `Interface` described by `text` formatted as `interface.name.com:port`
88 fun add_from_string(text: String)
89 do
90 assert text.chars.count(':') <= 1
91
92 var words = text.split(':')
93 var name = words[0]
94 var port
95 if words.length > 1 then
96 port = words[1].to_i
97 else port = 80
98
99 add new Interface(name, port)
100 end
101 end
102
103 # A list of virtual hosts with dynamic port listeners
104 class VirtualHosts
105 super Array[VirtualHost]
106
107 # Back reference to the server config
108 var config: ServerConfig
109
110 redef fun add(e)
111 do
112 super
113
114 e.server_config = config
115 end
116 end
117
118 # A list of routes with the search method `[]`
119 class Routes
120 # Back reference to the config of the virtual host
121 var config: VirtualHost
122
123 # Internal routes array.
124 protected var routes = new Array[Route]
125
126 # Add `e` to `self`
127 fun add(e: Route) do routes.add e
128
129 # Remove `e` from `self`
130 fun remove(e: Route) do routes.remove e
131
132 # Get the first `Route` than has `key` as prefix to its path
133 fun [](key: String): nullable Route
134 do
135 for route in routes do
136 var path = route.path
137 if path == null or key.has_prefix(path) then return route
138 end
139 return null
140 end
141 end