Skip to content

Commit 4cc3c6c

Browse files
committed
Trying out crystal lang
1 parent 1838720 commit 4cc3c6c

File tree

7 files changed

+154
-41
lines changed

7 files changed

+154
-41
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*.cr]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 2
9+
trim_trailing_whitespace = true

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/docs/
2+
/lib/
3+
/bin/
4+
/.shards/
5+
*.dwarf

README.md

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,10 @@
22

33
Docker container state and health check.
44

5-
## Requirements
6-
7-
Tested on: Node v12; Express v4; and [apocas/dockerode] v3.
8-
9-
[apocas/dockerode]: https://github.com/apocas/dockerode
10-
115
## Example use
126

137
Returns status code 200 if and only if container is healthy.
148

159
Example use:
1610

1711
$ curl -I http://localhost:3000/node
18-
HTTP/1.1 200 OK
19-
X-Powered-By: Express
20-
Content-Type: application/json; charset=utf-8
21-
Content-Length: 5770
22-
ETag: W/"168a-5eUBcpHavWjKjs0OWb8kmeXZdVs"
23-
Date: Thu, 04 Jun 2020 13:48:10 GMT
24-
Connection: keep-alive
25-
26-
## TODO
27-
28-
Use a smaller framework than express, or no framework at all?

UNLICENSE

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
This is free and unencumbered software released into the public domain.
2+
3+
Anyone is free to copy, modify, publish, use, compile, sell, or
4+
distribute this software, either in source code form or as a compiled
5+
binary, for any purpose, commercial or non-commercial, and by any
6+
means.
7+
8+
In jurisdictions that recognize copyright laws, the author or authors
9+
of this software dedicate any and all copyright interest in the
10+
software to the public domain. We make this dedication for the benefit
11+
of the public at large and to the detriment of our heirs and
12+
successors. We intend this dedication to be an overt act of
13+
relinquishment in perpetuity of all present and future rights to this
14+
software under copyright law.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.
23+
24+
For more information, please refer to <http://unlicense.org/>

shard.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: docker-state
2+
version: 0.1.0
3+
4+
authors:
5+
- Henrik Holst <[email protected]>
6+
7+
targets:
8+
docker-healthz:
9+
main: src/docker-healthz.cr
10+
11+
dependencies:
12+
json_mapping:
13+
github: crystal-lang/json_mapping.cr
14+
router:
15+
github: tbrand/router.cr
16+
docker:
17+
github: hholst80/docker.cr
18+
19+
crystal: ">= 1.0.0"

src/app.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/docker-healthz.cr

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#require "http/server"
2+
require "router"
3+
require "json"
4+
require "docker"
5+
require "socket"
6+
7+
class WebServer
8+
include Router
9+
@client = Docker.client
10+
@server : HTTP::Server | Nil
11+
12+
def draw_routes
13+
get "/:id" do |context, params|
14+
id = params["id"]
15+
#context.response.print Docker.client.containers
16+
#res = Docker.client.post("/containers/#{id}")
17+
res = Docker.client.get "/v1.30/containers/#{id}/json"
18+
context.response.status_code = res.status_code
19+
if res.status_code != 200
20+
context.response.print res.body
21+
context
22+
else
23+
#res = @client.post("/containers/#{id}")
24+
#case res.status_code
25+
#when 404
26+
# raise Docker::Client::Exception.new("no such container")
27+
#when 500
28+
# raise Docker::Client::Exception.new("server error")
29+
#end
30+
state_json = Hash(String, JSON::Any).from_json(res.body)
31+
context.response.print state_json["State"].to_json, "\n"
32+
context
33+
end
34+
end
35+
end
36+
37+
def run
38+
@client = Docker::Client.new
39+
server = HTTP::Server.new(route_handler)
40+
address = server.bind_tcp 8080
41+
puts "Listening on http://#{address}"
42+
server.listen
43+
end
44+
end
45+
46+
#{'Dead': False,
47+
# 'Error': '',
48+
# 'ExitCode': 0,
49+
# 'FinishedAt': '2021-01-09T19:45:21.120602084Z',
50+
# 'OOMKilled': False,
51+
# 'Paused': False,
52+
# 'Pid': 171432,
53+
# 'Restarting': False,
54+
# 'Running': True,
55+
# 'StartedAt': '2021-06-05T11:24:27.11380333Z',
56+
# 'Status': 'running'}
57+
#
58+
59+
struct State
60+
include JSON::Serializable
61+
@Dead : Bool
62+
@Error : String
63+
@ExitCode : Int32
64+
@FinishedAt : String
65+
@OOMKilled : Bool
66+
@Paused : Bool
67+
@Pid : Int32
68+
@Restarting : Bool
69+
@Running : Bool
70+
@StartedAt : String
71+
@Status : String
72+
end
73+
74+
# Docker.client.info
75+
# #containers = Docker.client.containers
76+
# #info = Docker.client.info
77+
# res = Docker.client.get "/v1.30/containers/hello/json"
78+
# case res.status_code
79+
# when 404
80+
# pp "no such container"
81+
# when 500
82+
# pp "server error"
83+
# end
84+
# #puts res.body
85+
# #puts "--------------------"
86+
# state_json = Hash(String, JSON::Any).from_json(res.body)
87+
# p state_json["State"]
88+
# #puts State.from_json(state_json["State"])
89+
# puts "------------------------------"
90+
# #puts State.from_json(state_json)
91+
92+
#p containers.first
93+
#p info
94+
95+
web_server = WebServer.new
96+
web_server.draw_routes
97+
web_server.run

0 commit comments

Comments
 (0)