2021-09-06 05:34:47 +03:00
|
|
|
#!/usr/bin/python3
|
2021-09-17 14:29:32 +03:00
|
|
|
from bottle import abort, route, run, request
|
2021-09-07 04:54:24 +03:00
|
|
|
import pymongo
|
2021-09-27 02:25:02 +03:00
|
|
|
import time
|
2021-09-20 02:18:03 +03:00
|
|
|
# TODO: auth to /admin
|
|
|
|
# TODO: timestamps to posts
|
|
|
|
# TODO: author to posts and multiple users
|
2021-09-27 02:19:19 +03:00
|
|
|
# TODO: add bottle's params to config
|
2021-09-20 02:18:03 +03:00
|
|
|
|
2021-09-06 05:34:47 +03:00
|
|
|
|
2021-09-17 15:29:50 +03:00
|
|
|
class Config:
|
|
|
|
"""
|
2021-09-20 02:18:03 +03:00
|
|
|
posts - table with posts
|
|
|
|
config structure:
|
|
|
|
DB:
|
|
|
|
- host
|
|
|
|
- port
|
|
|
|
- dbname
|
2021-09-17 15:29:50 +03:00
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
"""
|
2021-09-20 02:18:03 +03:00
|
|
|
Init config
|
2021-09-17 15:29:50 +03:00
|
|
|
"""
|
2021-09-17 15:50:18 +03:00
|
|
|
self.readConfig()
|
|
|
|
|
|
|
|
mongoclient = pymongo.MongoClient(self.host, self.port)
|
2021-09-27 02:19:19 +03:00
|
|
|
if self.dbname not in mongoclient.list_database_names():
|
|
|
|
print('DB not found, creating')
|
2021-09-17 15:50:18 +03:00
|
|
|
database = mongoclient[self.dbname]
|
|
|
|
|
|
|
|
# TODO: Create table if not exists
|
2021-09-27 02:19:19 +03:00
|
|
|
if 'posts' not in database.list_collection_names():
|
|
|
|
print('Table not fount, creating')
|
2021-09-17 15:29:50 +03:00
|
|
|
posts = database['posts']
|
|
|
|
self.posts = posts
|
2021-09-06 05:34:47 +03:00
|
|
|
|
2021-09-17 15:50:18 +03:00
|
|
|
def readConfig(self):
|
2021-09-17 14:29:32 +03:00
|
|
|
"""
|
2021-09-20 02:18:03 +03:00
|
|
|
Read config file, if not exists - call self.createConfig()
|
2021-09-17 14:29:32 +03:00
|
|
|
"""
|
2021-09-17 15:50:18 +03:00
|
|
|
import configparser
|
|
|
|
config = configparser.ConfigParser()
|
2021-09-17 15:57:50 +03:00
|
|
|
if not config.read('config.ini'):
|
|
|
|
self.createConfig(config)
|
2021-09-17 15:50:18 +03:00
|
|
|
|
|
|
|
db = config['DB']
|
|
|
|
|
|
|
|
self.host = db['host']
|
|
|
|
self.port = int(db['port'])
|
|
|
|
self.dbname = db['name']
|
2021-09-17 14:29:32 +03:00
|
|
|
|
2021-09-17 15:57:50 +03:00
|
|
|
def createConfig(self, config):
|
2021-09-17 14:29:32 +03:00
|
|
|
"""
|
|
|
|
Create config file
|
|
|
|
"""
|
2021-09-17 15:57:50 +03:00
|
|
|
config['DB'] = {}
|
|
|
|
db = config['DB']
|
|
|
|
db['host'] = 'localhost'
|
|
|
|
db['port'] = '27017'
|
|
|
|
db['name'] = 'pycms'
|
|
|
|
|
|
|
|
with open('config.ini', 'w') as cfgfile:
|
|
|
|
config.write(cfgfile)
|
2021-09-17 14:29:32 +03:00
|
|
|
|
|
|
|
|
2021-09-17 15:29:50 +03:00
|
|
|
class Back():
|
2021-09-17 14:29:32 +03:00
|
|
|
"""
|
|
|
|
All actions that will be triggered by http
|
|
|
|
"""
|
2021-09-17 15:29:50 +03:00
|
|
|
def getRootPost(self):
|
2021-09-17 14:29:32 +03:00
|
|
|
try:
|
|
|
|
return posts.find_one({'name': '_root_'})['text']
|
|
|
|
except TypeError:
|
|
|
|
return abort(404, 'No such page')
|
|
|
|
|
2021-09-17 15:29:50 +03:00
|
|
|
def getPost(self, name):
|
2021-09-17 14:29:32 +03:00
|
|
|
try:
|
|
|
|
return posts.find_one({'name': name})['text']
|
|
|
|
except TypeError:
|
|
|
|
return abort(404, 'No such page')
|
|
|
|
|
2021-09-17 15:29:50 +03:00
|
|
|
def getAllPosts(self):
|
2021-09-20 02:18:03 +03:00
|
|
|
# TODO: clear up output, remove '_id' and 'body',
|
|
|
|
# now there should be only
|
2021-09-17 15:29:50 +03:00
|
|
|
dict_posts = list()
|
|
|
|
for i in posts.find():
|
|
|
|
dict_posts.append(i)
|
|
|
|
return str(dict_posts)
|
|
|
|
|
|
|
|
def updatePost(self, name, body):
|
2021-09-20 02:18:03 +03:00
|
|
|
# TODO: return RESTful error/success result
|
2021-09-17 14:29:32 +03:00
|
|
|
# If post exists, update it
|
|
|
|
if posts.find_one({'name': name}):
|
|
|
|
newPost = {'$set': {'text': body}}
|
|
|
|
return str(posts.update_one({'name': name}, newPost))
|
|
|
|
# Else - create new
|
|
|
|
else:
|
2021-09-27 02:25:02 +03:00
|
|
|
newPost = {'name': name, 'text': body,
|
|
|
|
'create_timestamp': str(time.time())}
|
2021-09-17 14:29:32 +03:00
|
|
|
return str(posts.insert_one(newPost).inserted_id)
|
|
|
|
|
2021-09-17 15:29:50 +03:00
|
|
|
def deletePost(self, name):
|
2021-09-20 02:18:03 +03:00
|
|
|
# TODO: return RESTful error/success result
|
2021-09-18 04:48:04 +03:00
|
|
|
return bool(posts.delete_one({'name': name}).deleted_count)
|
2021-09-17 14:29:32 +03:00
|
|
|
|
|
|
|
|
2021-10-31 02:24:54 +03:00
|
|
|
class Metrics:
|
|
|
|
def alive():
|
|
|
|
return str("alive 1")
|
|
|
|
|
|
|
|
|
|
|
|
@route('/metrics')
|
|
|
|
def metrics():
|
|
|
|
return Metrics.alive()
|
|
|
|
|
|
|
|
|
2021-09-06 05:34:47 +03:00
|
|
|
@route('/post/<name>')
|
|
|
|
def post(name):
|
2021-09-07 05:17:29 +03:00
|
|
|
'''
|
|
|
|
Get post
|
|
|
|
'''
|
2021-09-17 15:29:50 +03:00
|
|
|
return str(back.getPost(name))
|
2021-09-06 05:34:47 +03:00
|
|
|
|
|
|
|
|
2021-09-18 04:48:04 +03:00
|
|
|
@route('/admin/post/<name>', method='POST')
|
2021-09-17 14:29:32 +03:00
|
|
|
def postUpd(name):
|
2021-09-07 05:17:29 +03:00
|
|
|
'''
|
|
|
|
Insert/Update post
|
|
|
|
'''
|
2021-09-06 05:34:47 +03:00
|
|
|
body = request.forms.get('body')
|
2021-09-17 15:29:50 +03:00
|
|
|
return back.updatePost(name=name, body=body)
|
2021-09-06 05:34:47 +03:00
|
|
|
|
|
|
|
|
2021-09-18 04:48:04 +03:00
|
|
|
@route('/admin/post/<name>', method='DELETE')
|
2021-09-17 14:29:32 +03:00
|
|
|
def postDel(name):
|
2021-09-07 05:17:29 +03:00
|
|
|
'''
|
|
|
|
Delete post by name
|
|
|
|
'''
|
2021-09-17 15:29:50 +03:00
|
|
|
return str(back.deletePost(name))
|
2021-09-17 14:29:32 +03:00
|
|
|
|
2021-09-06 05:34:47 +03:00
|
|
|
|
2021-09-17 14:29:32 +03:00
|
|
|
@route('/post')
|
2021-09-07 05:17:29 +03:00
|
|
|
def all_posts():
|
|
|
|
'''
|
|
|
|
Returns all posts
|
|
|
|
'''
|
2021-09-17 15:29:50 +03:00
|
|
|
return back.getAllPosts()
|
2021-09-06 05:34:47 +03:00
|
|
|
|
|
|
|
|
|
|
|
@route('/')
|
|
|
|
def index():
|
2021-09-17 15:29:50 +03:00
|
|
|
return back.getRootPost()
|
2021-09-06 05:34:47 +03:00
|
|
|
|
|
|
|
|
2021-09-07 06:35:22 +03:00
|
|
|
if __name__ == '__main__':
|
2021-10-18 18:16:02 +03:00
|
|
|
print("Init")
|
2021-09-17 15:29:50 +03:00
|
|
|
cfg = Config()
|
2021-10-18 18:16:02 +03:00
|
|
|
print("Configured")
|
2021-09-17 15:29:50 +03:00
|
|
|
back = Back()
|
|
|
|
posts = cfg.posts
|
2021-10-18 18:16:02 +03:00
|
|
|
run(host='0.0.0.0', port=8080, reloader=True, debug=True)
|