Compare commits

..

No commits in common. "2785d3d6b67e3f1c1ab721217c701670d267897d" and "96790be6f783492c504c680857cd793121da89f0" have entirely different histories.

4 changed files with 16 additions and 41 deletions

View File

@ -2,7 +2,4 @@
host = db
port = 27017
name = pycms
[App]
host = 0.0.0.0
port = 8080
debug = True

View File

@ -4,10 +4,10 @@ services:
build: .
ports:
- 8080:8080
depends_on:
- db
volumes:
- ./config.ini:/app/config.ini
depends_on:
- db
db:
image: mongo
tests:

45
main.py
View File

@ -16,10 +16,6 @@ class Config:
- host
- port
- dbname
App:
- host
- port
- debug
"""
def __init__(self):
"""
@ -32,8 +28,9 @@ 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 found, creating')
print('Table not fount, creating')
posts = database['posts']
self.posts = posts
@ -47,15 +44,11 @@ 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
@ -66,12 +59,6 @@ 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)
@ -101,28 +88,20 @@ 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}):
newPostJson = {'$set': {'text': body}}
newPost = posts.update_one({'name': name}, newPostJson)
result = dict(
status=200, state='updated', count=newPost.matched_count
)
newPost = {'$set': {'text': body}}
return str(posts.update_one({'name': name}, newPost))
# Else - create new
else:
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)
newPost = {'name': name, 'text': body,
'create_timestamp': str(time.time())}
return str(posts.insert_one(newPost).inserted_id)
def deletePost(self, name):
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)
# TODO: return RESTful error/success result
return bool(posts.delete_one({'name': name}).deleted_count)
class Metrics:
@ -179,4 +158,4 @@ if __name__ == '__main__':
print("Configured")
back = Back()
posts = cfg.posts
run(host=cfg.apphost, port=cfg.appport, reloader=cfg.appdebug, debug=cfg.appdebug)
run(host='0.0.0.0', port=8080, reloader=True, debug=True)

View File

@ -1,6 +1,5 @@
#!/bin/bash
curl_cmd='curl -s -w :%{http_code}'
#curl_cmd='curl -s -w "%{http_code}\t%{stdout}" -o /dev/null'
curl_cmd='curl -s -w "%{http_code}" -o /dev/null'
url='server:8080'
$curl_cmd -X POST $url/admin/post/test -F 'body=testpage' && echo ": Create success" || echo ': create fail'