-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestServer.vala
148 lines (120 loc) · 4.7 KB
/
RestServer.vala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using Gee;
using GLib;
using Posix;
using Soup;
namespace ValaRest {
/**
* Class to contain REST server mappings.
*/
public class RestMapping {
public string mapping { get; set; }
public RestObject obj { get; set; }
} // end class RestMapping
public interface RestObject : GLib.Object {
public string params{ get; set; }
public string resource{ get; set; }
public string get_mapping ( RestObject obj ) {
foreach ( RestMapping m in mappings ) {
if (m.obj == obj) {
return m.mapping;
}
} // end foreach mappings
return "";
} // end get_mapping
public void handler (Soup.Server server, Soup.Message msg,
string path, GLib.HashTable? query, Soup.ClientContext client) {
string subpath = path.replace(get_mapping(this), "");
string? id = (subpath != null && subpath.has_prefix("/")) ? subpath.substring(1) : null;
if (msg.method == "GET") {
// GET: retrieve record
string respuesta = serialize(this);
msg.set_response (RESPONSE_TYPE, Soup.MemoryUse.COPY, respuesta.data);
} else if (msg.method == "POST") {
// POST: new record
msg.set_response (RESPONSE_TYPE, Soup.MemoryUse.COPY, "POST".data);
} else if (msg.method == "DELETE") {
// DELETE: remove record
msg.set_response (RESPONSE_TYPE, Soup.MemoryUse.COPY, "DELETE".data);
if ( id != null ) {
delete_object( id );
}
} else {
msg.set_response (RESPONSE_TYPE, Soup.MemoryUse.COPY, ERROR_RESPONSE.printf("Invalid method.").data);
}
} // end handler
/**
* Delete an object by primary key.
*/
public abstract void delete_object ( string pk );
/**
* Deserialize an object from a JSON string.
*/
public abstract RestObject deserialize ( string data );
/**
* Serialize a JSON string from an object.
*/
public abstract string serialize ( RestObject obj );
} // end interface RestObject
protected class detallestado: Object, RestObject{
public void delete_object(string pk){
printf("Objeto borrado: " + pk);
}
public RestObject deserialize (string data){
return null;
}
public string serialize(RestObject obj){
return "{}";
}
}
public static const string RESPONSE_TYPE = "text/json";
public static const string ERROR_RESPONSE = "{\"status\":\"error\",\"message\":\"%s\"}";
protected Soup.Server server = null;
protected ArrayList<RestMapping> mappings = null;
protected detallestado estado = null;
public class Server {
public Server () {
mappings = new ArrayList<RestMapping>();
estado = new detallestado();
add_mapping("/estado", estado);
} // end constructor
/**
* Add server object mapping. "mapping" should start with a slash
* and represent an absolute path for this object.
*/
public void add_mapping ( string mapping, RestObject obj ) {
RestMapping m = new RestMapping();
m.mapping = mapping;
m.obj = obj;
mappings.add( m );
} // end add_mapping
public void default_handler (Soup.Server server, Soup.Message msg, string path,
GLib.HashTable? query, Soup.ClientContext client) {
StringBuilder sb = new StringBuilder();
sb.append("""
<html>
<head>
<title>REST Server</title>
</head>
<body>
<ul>REST Mappings
""");
foreach ( RestMapping m in mappings ) {
sb.append("<li>" + m.mapping + "</li>");
} // end foreach mappings
sb.append("""
</ul>
</body>
</html>
""");
msg.set_response ("text/html", Soup.MemoryUse.COPY, sb.str.data);
} // end default_handler
public async void start ( int port ) {
server = new Soup.Server( Soup.SERVER_PORT, port );
server.add_handler("/", default_handler);
foreach ( RestMapping m in mappings ) {
server.add_handler( m.mapping, m.obj.handler );
} // end foreach mappings
server.run();
} // end start
} // end Server
} // end namespace ValaRest