Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 121 lines (107 sloc) 3.527 kb
76cf43d Adam Koprowski Initial code for manual tutorials on web services.
akoprow authored
1 import stdlib.tools.markdown
4380cd5 Frederic Ye use bootstrap
Aqua-Ye authored
2 import stdlib.themes.bootstrap
76cf43d Adam Koprowski Initial code for manual tutorials on web services.
akoprow authored
3
4 function uri_for_topic(topic) {
de93d16 Adam Koprowski New coding conventions of MLstate.
akoprow authored
5 Uri.of_absolute(
6 {Uri.default_absolute with
7 schema : {some: "http"},
8 domain : "localhost",
9 port : {some: 8080},
10 path : ["_rest_", topic]
11 }
12 )
76cf43d Adam Koprowski Initial code for manual tutorials on web services.
akoprow authored
13 }
14
4380cd5 Frederic Ye use bootstrap
Aqua-Ye authored
15 function load_source(topic) {
de93d16 Adam Koprowski New coding conventions of MLstate.
akoprow authored
16 match (WebClient.Get.try_get(uri_for_topic(topic))) {
17 case { failure : _ }: "Error, could not connect";
18 case ~{ success }:
19 match (WebClient.Result.get_class(success)) {
20 case { success }: success.content;
21 default: "Error {success.code}";
22 }
23 }
76cf43d Adam Koprowski Initial code for manual tutorials on web services.
akoprow authored
24 }
25
26 exposed function load_rendered(topic) {
de93d16 Adam Koprowski New coding conventions of MLstate.
akoprow authored
27 source = load_source(topic);
28 Markdown.xhtml_of_string(Markdown.default_options, source);
76cf43d Adam Koprowski Initial code for manual tutorials on web services.
akoprow authored
29 }
30
31 exposed function save_source(topic, source) {
de93d16 Adam Koprowski New coding conventions of MLstate.
akoprow authored
32 match (WebClient.Post.try_post(uri_for_topic(topic), source)) {
33 case { failure: _ }: {failure: "Could not reach the distant server"};
2377c36 Adam Koprowski Simplified pattern matching syntax
akoprow authored
34 case { success: s }:
de93d16 Adam Koprowski New coding conventions of MLstate.
akoprow authored
35 match (WebClient.Result.get_class(s)) {
36 case { success }: {success : load_rendered(topic)};
37 default: {failure : "Error {s.code}"};
38 }
39 }
76cf43d Adam Koprowski Initial code for manual tutorials on web services.
akoprow authored
40 }
41
42 function remove_topic(topic) {
de93d16 Adam Koprowski New coding conventions of MLstate.
akoprow authored
43 _ = WebClient.Delete.try_delete(uri_for_topic(topic));
44 void;
76cf43d Adam Koprowski Initial code for manual tutorials on web services.
akoprow authored
45 }
46
47 function edit(topic) {
de93d16 Adam Koprowski New coding conventions of MLstate.
akoprow authored
48 #show_messages = <></>;
49 Dom.set_value(#edit_content, load_source(topic));
50 Dom.hide(#show_content);
51 Dom.show(#edit_content);
52 Dom.give_focus(#edit_content);
76cf43d Adam Koprowski Initial code for manual tutorials on web services.
akoprow authored
53 }
54
55 function save(topic) {
de93d16 Adam Koprowski New coding conventions of MLstate.
akoprow authored
56 match (save_source(topic, Dom.get_value(#edit_content))) {
2377c36 Adam Koprowski Simplified pattern matching syntax
akoprow authored
57 case { ~success }:
de93d16 Adam Koprowski New coding conventions of MLstate.
akoprow authored
58 #show_content = success;
59 Dom.hide(#edit_content);
60 Dom.show(#show_content);
2377c36 Adam Koprowski Simplified pattern matching syntax
akoprow authored
61 case {~failure}:
de93d16 Adam Koprowski New coding conventions of MLstate.
akoprow authored
62 #show_messages = <>{failure}</>;
63 }
76cf43d Adam Koprowski Initial code for manual tutorials on web services.
akoprow authored
64 }
65
66 function display(topic) {
de93d16 Adam Koprowski New coding conventions of MLstate.
akoprow authored
67 Resource.styled_page(
68 "About {topic}", ["/resources/css.css"],
4380cd5 Frederic Ye use bootstrap
Aqua-Ye authored
69 <div class="navbar navbar-fixed-top">
70 <div class="navbar-inner">
71 <div class="container">
72 <span id=#logo class="brand"/><span class="navbar-text">About {topic}</span>
73 </div>
74 </div>
75 </div>
76 <div class="content container">
77 <div class="show_content" id=#show_content ondblclick={function(_) { edit(topic) }}>{load_rendered(topic)}</div>
78 </div>
79 <div class="show_messages" id=#show_messages />
80 <textarea class="edit_content" id=#edit_content style="display:none" cols="40" rows="30" onblur={function(_) { save(topic) }}></>
de93d16 Adam Koprowski New coding conventions of MLstate.
akoprow authored
81 );
76cf43d Adam Koprowski Initial code for manual tutorials on web services.
akoprow authored
82 }
83
84 function rest(topic) {
de93d16 Adam Koprowski New coding conventions of MLstate.
akoprow authored
85 match (HttpRequest.get_method()) {
2377c36 Adam Koprowski Simplified pattern matching syntax
akoprow authored
86 case {some: method}:
de93d16 Adam Koprowski New coding conventions of MLstate.
akoprow authored
87 match (method) {
88 case { post }:
89 _ = save_source(topic, `?`(HttpRequest.get_body(), ""));
90 Resource.raw_status({success});
91 case { delete }: remove_topic(topic);
92 Resource.raw_status({success});
93 case { get }: Resource.source(load_source(topic), "text/plain");
94 default: Resource.raw_status({method_not_allowed});
95 }
96 default: Resource.raw_status({bad_request});
97 }
76cf43d Adam Koprowski Initial code for manual tutorials on web services.
akoprow authored
98 }
99
100 function topic_of_path(path) {
de93d16 Adam Koprowski New coding conventions of MLstate.
akoprow authored
101 String.capitalize(
102 String.to_lower(List.to_string_using("", "", "::", path))
103 );
76cf43d Adam Koprowski Initial code for manual tutorials on web services.
akoprow authored
104 }
105
106 function start(url) {
de93d16 Adam Koprowski New coding conventions of MLstate.
akoprow authored
107 match (url) {
108 case { path : [], ... }: display("Hello");
109 case { path : ["rest" | path], ... }: rest(topic_of_path(path));
110 case ~{ path, ... }: display(topic_of_path(path));
111 }
112
76cf43d Adam Koprowski Initial code for manual tutorials on web services.
akoprow authored
113 }
114
de93d16 Adam Koprowski New coding conventions of MLstate.
akoprow authored
115 Server.start(
116 Server.http,
117 [ {resources: @static_include_directory("resources")}
118 , {dispatch: start}
119 ]
76cf43d Adam Koprowski Initial code for manual tutorials on web services.
akoprow authored
120 )
Something went wrong with that request. Please try again.