Mercurial > hg > orthanc
annotate OrthancServer/Resources/Samples/Lua/CallWebService.js @ 5018:eb8ca3403983 lua-heart-beat
fix: lock mutex when accessing state in HeartBeatThread
author | Alain Mazy <am@osimis.io> |
---|---|
date | Tue, 14 Jun 2022 17:19:16 +0200 |
parents | 43e613a7756b |
children | 0ea402b4d901 |
rev | line source |
---|---|
1055
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1 /** |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2 * Orthanc - A Lightweight, RESTful DICOM Store |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1055
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
4870
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium |
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
1055
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
7 * |
1375
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
8 * This program is free software: you can redistribute it and/or |
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
9 * modify it under the terms of the GNU General Public License as |
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
10 * published by the Free Software Foundation, either version 3 of the |
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
11 * License, or (at your option) any later version. |
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
12 * |
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
13 * This program is distributed in the hope that it will be useful, but |
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
14 * WITHOUT ANY WARRANTY; without even the implied warranty of |
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
16 * General Public License for more details. |
1055
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
17 * |
1375
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
18 * You should have received a copy of the GNU General Public License |
3d76e26b3865
fix licensing terms of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
1055
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
20 **/ |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
21 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
22 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
23 /** |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
24 * This file is a simple echo Web service implemented using |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
25 * "node.js". Whenever it receives a POST HTTP query, it echoes its |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
26 * body both to stdout and to the client. Credentials are checked. |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
27 **/ |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
28 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
29 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
30 // Parameters of the ECHO server |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
31 var port = 8000; |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
32 var username = 'alice'; |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
33 var password = 'alicePassword'; |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
34 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
35 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
36 var http = require('http'); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
37 var authorization = 'Basic ' + new Buffer(username + ':' + password).toString('base64') |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
38 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
39 var server = http.createServer(function(req, response) { |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
40 // Check the credentials |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
41 if (req.headers.authorization != authorization) |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
42 { |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
43 console.log('Bad credentials, access not allowed'); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
44 response.writeHead(401); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
45 response.end(); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
46 return; |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
47 } |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
48 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
49 switch (req.method) |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
50 { |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
51 case 'POST': |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
52 { |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
53 var body = ''; |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
54 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
55 req.on('data', function (data) { |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
56 response.write(data); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
57 body += data; |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
58 }); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
59 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
60 req.on('end', function () { |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
61 console.log('Message received: ' + body); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
62 response.end(); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
63 }); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
64 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
65 break; |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
66 } |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
67 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
68 default: |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
69 console.log('Method ' + req.method + ' is not supported by this ECHO Web service'); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
70 response.writeHead(405, {'Allow': 'POST'}); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
71 response.end(); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
72 } |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
73 }); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
74 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
75 server.listen(port); |