Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[feature] server, resource: easily register an other doctype + regist…

…er type accepts a list of registrable. Check example below:

Server.start(
 Server.http,
 [
  {register: [{ doctype : {html5} }, { css : [ /resource/style.css ]}]},
  {title:mypage, page:function(){<div>Hello world</div>}}
 ]
 )
  • Loading branch information...
commit ce4c3516659af287fd7f28e4b80146dfb62d4cc8 1 parent 845ab80
@cedricss cedricss authored
View
12 stdlib/core/web/resource/resource.opa
@@ -111,7 +111,12 @@ type resource_content = external
*/
-
+/**
+ * The doctype of an html page.
+ * You can specify the default doctype in the [Server.handler] list.
+ * Read [Server.start]
+ */
+type Resource.doctype = html_resource_doctype
/**
* {1 About this module}
@@ -719,6 +724,11 @@ export_data({~rc_content rc_status=_ rc_headers=_}: resource)=
register_external_favicon(favicon : Favicon.t) : void = Resource_private.register_external_favicon(favicon)
/**
+ * Set the default doctype of html pages.
+ */
+ register_default_doctype(d : html_resource_doctype) : void = Resource_private.register_default_doctype(d)
+
+ /**
* Removes an external javascript file (identified by its url) to the default_customizers of ALL resources if it exists.
* @param url An url (as a string) to a javascript file
*/
View
12 stdlib/core/web/resource/resource_private.opa
@@ -46,7 +46,8 @@ import stdlib.core.compare
*/
type html_resource_doctype =
{html5} /
- {xhtml1_1}
+ {xhtml1_1} /
+ {custom:string}
/**
* The actual contents of a resource.
@@ -132,6 +133,7 @@ Resource_private =
match doctype with
{xhtml1_1} -> shared_xhtml1_1_header
{html5} -> shared_html5_header
+ {~custom} -> custom
get_lastm(resource) =
check(x) =
@@ -635,12 +637,13 @@ required_customizer_for_opa_ad =
@private autoloaded_js = Mutable.make([] : list(string))
@private autoloaded_css = Mutable.make([] : list(string))
@private autoloaded_favicon = Mutable.make([] : list(Favicon.t))
+@private default_doctype = Mutable.make({xhtml1_1} : html_resource_doctype)
@package register_external_js(url : string) = autoloaded_js.set([url | autoloaded_js.get()])
@package unregister_external_js(url : string) = autoloaded_js.set(List.remove(url, autoloaded_js.get()))
@package register_external_css(url : string) = autoloaded_css.set([url | autoloaded_css.get()])
@package unregister_external_css(url : string) = autoloaded_css.set(List.remove(url, autoloaded_css.get()))
@package register_external_favicon(f:Favicon.t) = autoloaded_favicon.set([f | autoloaded_favicon.get()])
-
+@package register_default_doctype(d:html_resource_doctype) = default_doctype.set(d)
customizer_autoloaded : platform_customization =
_ -> some(
{ custom_body = none
@@ -943,7 +946,10 @@ export_resource(external_css_files: list(string),
<></>, base_url)
ready_head = <head>{base}{head_without_id}{global_variable}</head>
- html_doctype = match doctype with {some=d} -> html_doctype_to_string(d) {none} -> shared_xhtml1_1_header
+ html_doctype =
+ match doctype with
+ | {some=d} -> html_doctype_to_string(d)
+ | {none} -> html_doctype_to_string(default_doctype.get())
page = Xhtml.of_string_unsafe(html_doctype) <+>
<html xmlns="http://www.w3.org/1999/xhtml">{ready_head}{ready_body}</html>
View
49 stdlib/core/web/server/server.opa
@@ -93,11 +93,10 @@ type Server.registrable_resource =
{favicon : list(Favicon.t)}
/ {css : list(string)}
/ {js : list(string)}
-/ list(string)
+/ {doctype : Resource.doctype}
+/ list(Server.registrable_resource)
+
-/**
- * Different types of request handler.
- */
type Server.handler =
/** The most simple request handler. It replies to all incoming
@@ -235,35 +234,27 @@ Server = {{
| ~{hd=_ tl=_} as e -> flatten(e)
| {nil} -> head
)
+ rec register(r) =
+ match r
+ | ~{favicon} -> List.iter(f -> Resource.register_external_favicon(f), favicon)
+ | ~{js} -> List.iter(f -> Resource.register_external_js(f), js)
+ | ~{css} -> List.iter(f -> Resource.register_external_css(f), css)
+ | ~{doctype} -> Resource.register_default_doctype(doctype)
+ | ~{hd tl} -> List.iter(register, ~{hd tl})
+ | {nil} -> void
match handler with
| ~{hd; tl} -> (
all = List.map(handler_to_parser, hd+>tl)
Rule.of_parsers(all)
)
| {nil} -> Rule.fail
- | ~{register} ->
- do match register
- | ~{favicon} -> List.iter(f -> Resource.register_external_favicon(f), favicon)
- | ~{js} -> List.iter(f -> Resource.register_external_js(f), js)
- | ~{css} -> List.iter(f -> Resource.register_external_css(f), css)
- | ~{hd tl} ->
- List.iter(file ->
- if String.has_suffix(".css", file) then
- Resource.register_external_css(file)
- else if String.has_suffix(".js", file) then
- Resource.register_external_js(file)
- else
- Log.error("Server", "Unknown type of file, the resource \"{file}\" will not registered")
- , ~{hd tl})
- | {nil} -> void
- Rule.fail
+ | {register=r} -> do register(r) Rule.fail
| {custom=_} as e
| {title=_; page=_} as e
| {dispatch=_} as e
| {filter=_ dispatch=_} as e
| {resources=_} as e -> simple_to_parser(e)
-
/**
* {2 Starting a server}
*/
@@ -285,7 +276,7 @@ Server = {{
/**
* Default [http] configuration with port equals to 8080 and the server name is "http".
*/
- http = { port = 8080; netmask = 0.0.0.0; encryption = {no_encryption}; name = "http"}
+ http : Server.conf = { port = 8080; netmask = 0.0.0.0; encryption = {no_encryption}; name = "http"}
/**
@@ -293,7 +284,7 @@ Server = {{
* name is "https". SSL certificate should be at ./service.crt and
* SSL key should be at ./service.key.
*/
- https = { port = 8080; netmask = 0.0.0.0; encryption = {certificate = "service.crt" private_key="service.key" password=""}; name = "https"}
+ https : Server.conf = { port = 8080; netmask = 0.0.0.0; encryption = {certificate = "service.crt" private_key="service.key" password=""}; name = "https"}
/**
* {2 Constructing a server}
@@ -384,6 +375,18 @@ Server = {{
make(blind_url_parser)
)
+ /**
+ * Register a list of file name, based on the extension (".css" and ".js" are supported).
+ */
+ register_from_extension(l : list(string)): void =
+ List.iter(file ->
+ if String.has_suffix(".css", file) then
+ Resource.register_external_css(file)
+ else if String.has_suffix(".js", file) then
+ Resource.register_external_js(file)
+ else
+ Log.error("Server", "Unknown type of file, the resource \"{file}\" will not registered")
+ , l)
Filter =
{{
Please sign in to comment.
Something went wrong with that request. Please try again.