Compare commits
26 Commits
76282e9e28
...
master
Author | SHA1 | Date | |
---|---|---|---|
93ff836e08 | |||
cd47d4aa84 | |||
2785d3d6b6 | |||
f4732836ad | |||
b0bd579357 | |||
fd341bd9df | |||
4608125fdf | |||
8e52e543f9 | |||
80aeafa013 | |||
66d68701c2 | |||
94a626e7f1 | |||
0ba3b5f037 | |||
7544e3c447 | |||
96790be6f7 | |||
89dcffa6bb | |||
4f736f763e | |||
f84994999a | |||
74cd6c6d4b | |||
49f981708a | |||
54139d6f76 | |||
4ab80616a6 | |||
04905c59db | |||
c4602b9d4e | |||
2f5867028e | |||
1aa4be2e47 | |||
3aa77129ec |
25
.drone.yml
Normal file
25
.drone.yml
Normal file
@ -0,0 +1,25 @@
|
||||
kind: pipeline
|
||||
type: exec
|
||||
name: build
|
||||
|
||||
steps:
|
||||
- name: build
|
||||
commands:
|
||||
- docker-compose -p pycms build
|
||||
- docker-compose -p pycms push
|
||||
|
||||
---
|
||||
name: test
|
||||
type: docker
|
||||
kind: pipeline
|
||||
steps:
|
||||
- name: run db
|
||||
image: mongo
|
||||
detach: true
|
||||
|
||||
- name: run_tests
|
||||
image: pycms_server
|
||||
commands:
|
||||
- bash ./test.sh
|
||||
depends_on:
|
||||
- build
|
@ -2,4 +2,7 @@
|
||||
host = db
|
||||
port = 27017
|
||||
name = pycms
|
||||
|
||||
[App]
|
||||
host = 0.0.0.0
|
||||
port = 8080
|
||||
debug = True
|
||||
|
@ -4,9 +4,15 @@ services:
|
||||
build: .
|
||||
ports:
|
||||
- 8080:8080
|
||||
volumes:
|
||||
- ./config.ini:/app/config.ini
|
||||
depends_on:
|
||||
- db
|
||||
volumes:
|
||||
- ./config.ini:/app/config.ini
|
||||
db:
|
||||
image: mongo
|
||||
tests:
|
||||
build: .
|
||||
command: bash ./test.sh
|
||||
depends_on:
|
||||
- db
|
||||
- server
|
||||
|
55
main.py
55
main.py
@ -16,6 +16,10 @@ class Config:
|
||||
- host
|
||||
- port
|
||||
- dbname
|
||||
App:
|
||||
- host
|
||||
- port
|
||||
- debug
|
||||
"""
|
||||
def __init__(self):
|
||||
"""
|
||||
@ -28,9 +32,8 @@ class Config:
|
||||
print('DB not found, creating')
|
||||
database = mongoclient[self.dbname]
|
||||
|
||||
# TODO: Create table if not exists
|
||||
if 'posts' not in database.list_collection_names():
|
||||
print('Table not fount, creating')
|
||||
print('Table not found, creating')
|
||||
posts = database['posts']
|
||||
self.posts = posts
|
||||
|
||||
@ -44,11 +47,15 @@ class Config:
|
||||
self.createConfig(config)
|
||||
|
||||
db = config['DB']
|
||||
|
||||
self.host = db['host']
|
||||
self.port = int(db['port'])
|
||||
self.dbname = db['name']
|
||||
|
||||
app = config['App']
|
||||
self.apphost = app['host']
|
||||
self.appport = int(app['port'])
|
||||
self.appdebug = bool(app['debug'])
|
||||
|
||||
def createConfig(self, config):
|
||||
"""
|
||||
Create config file
|
||||
@ -59,6 +66,12 @@ class Config:
|
||||
db['port'] = '27017'
|
||||
db['name'] = 'pycms'
|
||||
|
||||
config['App'] = {}
|
||||
app = config['App']
|
||||
app['port'] = '8080'
|
||||
app['debug'] = 'True'
|
||||
app['host'] = '0.0.0.0'
|
||||
|
||||
with open('config.ini', 'w') as cfgfile:
|
||||
config.write(cfgfile)
|
||||
|
||||
@ -88,20 +101,38 @@ class Back():
|
||||
return str(dict_posts)
|
||||
|
||||
def updatePost(self, name, body):
|
||||
# TODO: return RESTful error/success result
|
||||
# If post exists, update it
|
||||
if posts.find_one({'name': name}):
|
||||
newPost = {'$set': {'text': body}}
|
||||
return str(posts.update_one({'name': name}, newPost))
|
||||
newPostJson = {'$set': {'text': body}}
|
||||
newPost = posts.update_one({'name': name}, newPostJson)
|
||||
result = dict(
|
||||
status=200, state='updated', count=newPost.matched_count
|
||||
)
|
||||
# Else - create new
|
||||
else:
|
||||
newPost = {'name': name, 'text': body,
|
||||
'create_timestamp': str(time.time())}
|
||||
return str(posts.insert_one(newPost).inserted_id)
|
||||
newPostJson = {'name': name, 'text': body,
|
||||
'create_timestamp': str(time.time())}
|
||||
newPost = posts.insert_one(newPostJson).inserted_id
|
||||
result = dict(status=200, state='new')
|
||||
return str(result)
|
||||
|
||||
def deletePost(self, name):
|
||||
# TODO: return RESTful error/success result
|
||||
return bool(posts.delete_one({'name': name}).deleted_count)
|
||||
delete = posts.delete_one({'name': name}).deleted_count
|
||||
if not delete:
|
||||
result = dict(status=500, count=delete)
|
||||
else:
|
||||
result = dict(status=200, count=delete)
|
||||
return str(result)
|
||||
|
||||
|
||||
class Metrics:
|
||||
def alive():
|
||||
return str("alive 1")
|
||||
|
||||
|
||||
@route('/metrics')
|
||||
def metrics():
|
||||
return Metrics.alive()
|
||||
|
||||
|
||||
@route('/post/<name>')
|
||||
@ -148,4 +179,4 @@ if __name__ == '__main__':
|
||||
print("Configured")
|
||||
back = Back()
|
||||
posts = cfg.posts
|
||||
run(host='0.0.0.0', port=8080, reloader=True, debug=True)
|
||||
run(host=cfg.apphost, port=cfg.appport, reloader=cfg.appdebug, debug=cfg.appdebug)
|
||||
|
13
test.sh
Normal file
13
test.sh
Normal file
@ -0,0 +1,13 @@
|
||||
#!/bin/bash
|
||||
curl_cmd='curl -s -w :%{http_code}'
|
||||
#curl_cmd='curl -s -w "%{http_code}\t%{stdout}" -o /dev/null'
|
||||
url='server:8080'
|
||||
$curl_cmd -X POST $url/admin/post/test -F 'body=testpage' && echo ": Create success" || echo ': create fail'
|
||||
|
||||
$curl_cmd $url/post/test && echo ": get Success" || echo ': get fail'
|
||||
|
||||
$curl_cmd -X POST $url/admin/post/test -F 'body=testpage2' && echo ": Update success" || echo ': update fail'
|
||||
|
||||
$curl_cmd -X DELETE $url/admin/post/test && echo ": delete success" || echo ': delete fail'
|
||||
|
||||
$curl_cmd $url/metrics && echo ": metrics available" || echo ': metrics unavailable'
|
Loading…
Reference in New Issue
Block a user