Compare commits
35 Commits
ee18b5535c
...
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 | |||
| 32a2678170 | |||
| 6cd33f25be | |||
| 7c7db5bca0 | |||
| 755e7c5ce4 | |||
| 85a860eb1a | |||
| 5aecf2d643 | |||
| f72c941a3a | |||
| 539d53d209 | |||
| 28b98a9127 |
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
|
||||||
9
Dockerfile
Normal file
9
Dockerfile
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
FROM python:latest
|
||||||
|
|
||||||
|
ADD . /app
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
RUN pip3 install --upgrade pip && pip3 install -r requirements.txt
|
||||||
|
EXPOSE 8080
|
||||||
|
CMD python3 main.py
|
||||||
8
config.ini
Normal file
8
config.ini
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
[DB]
|
||||||
|
host = db
|
||||||
|
port = 27017
|
||||||
|
name = pycms
|
||||||
|
[App]
|
||||||
|
host = 0.0.0.0
|
||||||
|
port = 8080
|
||||||
|
debug = True
|
||||||
18
docker-compose.yml
Normal file
18
docker-compose.yml
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
version: '3'
|
||||||
|
services:
|
||||||
|
server:
|
||||||
|
build: .
|
||||||
|
ports:
|
||||||
|
- 8080:8080
|
||||||
|
depends_on:
|
||||||
|
- db
|
||||||
|
volumes:
|
||||||
|
- ./config.ini:/app/config.ini
|
||||||
|
db:
|
||||||
|
image: mongo
|
||||||
|
tests:
|
||||||
|
build: .
|
||||||
|
command: bash ./test.sh
|
||||||
|
depends_on:
|
||||||
|
- db
|
||||||
|
- server
|
||||||
154
main.py
154
main.py
@@ -1,60 +1,138 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
from bottle import abort, route, run, request
|
from bottle import abort, route, run, request
|
||||||
import pymongo
|
import pymongo
|
||||||
|
import time
|
||||||
mongoclient = pymongo.MongoClient('localhost', 27017)
|
# TODO: auth to /admin
|
||||||
database = mongoclient['pycms']
|
# TODO: timestamps to posts
|
||||||
posts = database['posts']
|
# TODO: author to posts and multiple users
|
||||||
|
# TODO: add bottle's params to config
|
||||||
|
|
||||||
|
|
||||||
class Init:
|
class Config:
|
||||||
def readConfig():
|
|
||||||
"""
|
"""
|
||||||
Read config file, if not exists - call Init.createConfig()
|
posts - table with posts
|
||||||
|
config structure:
|
||||||
|
DB:
|
||||||
|
- host
|
||||||
|
- port
|
||||||
|
- dbname
|
||||||
|
App:
|
||||||
|
- host
|
||||||
|
- port
|
||||||
|
- debug
|
||||||
"""
|
"""
|
||||||
pass
|
def __init__(self):
|
||||||
|
"""
|
||||||
|
Init config
|
||||||
|
"""
|
||||||
|
self.readConfig()
|
||||||
|
|
||||||
def createConfig():
|
mongoclient = pymongo.MongoClient(self.host, self.port)
|
||||||
|
if self.dbname not in mongoclient.list_database_names():
|
||||||
|
print('DB not found, creating')
|
||||||
|
database = mongoclient[self.dbname]
|
||||||
|
|
||||||
|
if 'posts' not in database.list_collection_names():
|
||||||
|
print('Table not found, creating')
|
||||||
|
posts = database['posts']
|
||||||
|
self.posts = posts
|
||||||
|
|
||||||
|
def readConfig(self):
|
||||||
|
"""
|
||||||
|
Read config file, if not exists - call self.createConfig()
|
||||||
|
"""
|
||||||
|
import configparser
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
if not config.read('config.ini'):
|
||||||
|
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
|
Create config file
|
||||||
"""
|
"""
|
||||||
pass
|
config['DB'] = {}
|
||||||
|
db = config['DB']
|
||||||
|
db['host'] = 'localhost'
|
||||||
|
db['port'] = '27017'
|
||||||
|
db['name'] = 'pycms'
|
||||||
|
|
||||||
def setupConnect():
|
config['App'] = {}
|
||||||
"""
|
app = config['App']
|
||||||
Setup MongoDB connection
|
app['port'] = '8080'
|
||||||
"""
|
app['debug'] = 'True'
|
||||||
pass
|
app['host'] = '0.0.0.0'
|
||||||
|
|
||||||
|
with open('config.ini', 'w') as cfgfile:
|
||||||
|
config.write(cfgfile)
|
||||||
|
|
||||||
|
|
||||||
class Back:
|
class Back():
|
||||||
"""
|
"""
|
||||||
All actions that will be triggered by http
|
All actions that will be triggered by http
|
||||||
"""
|
"""
|
||||||
def getRootPost():
|
def getRootPost(self):
|
||||||
try:
|
try:
|
||||||
return posts.find_one({'name': '_root_'})['text']
|
return posts.find_one({'name': '_root_'})['text']
|
||||||
except TypeError:
|
except TypeError:
|
||||||
return abort(404, 'No such page')
|
return abort(404, 'No such page')
|
||||||
|
|
||||||
def getPost(name):
|
def getPost(self, name):
|
||||||
try:
|
try:
|
||||||
return posts.find_one({'name': name})['text']
|
return posts.find_one({'name': name})['text']
|
||||||
except TypeError:
|
except TypeError:
|
||||||
return abort(404, 'No such page')
|
return abort(404, 'No such page')
|
||||||
|
|
||||||
def updatePost(name, body):
|
def getAllPosts(self):
|
||||||
|
# TODO: clear up output, remove '_id' and 'body',
|
||||||
|
# now there should be only
|
||||||
|
dict_posts = list()
|
||||||
|
for i in posts.find():
|
||||||
|
dict_posts.append(i)
|
||||||
|
return str(dict_posts)
|
||||||
|
|
||||||
|
def updatePost(self, name, body):
|
||||||
# If post exists, update it
|
# If post exists, update it
|
||||||
if posts.find_one({'name': name}):
|
if posts.find_one({'name': name}):
|
||||||
newPost = {'$set': {'text': body}}
|
newPostJson = {'$set': {'text': body}}
|
||||||
return str(posts.update_one({'name': name}, newPost))
|
newPost = posts.update_one({'name': name}, newPostJson)
|
||||||
|
result = dict(
|
||||||
|
status=200, state='updated', count=newPost.matched_count
|
||||||
|
)
|
||||||
# Else - create new
|
# Else - create new
|
||||||
else:
|
else:
|
||||||
newPost = {'name': name, 'text': body}
|
newPostJson = {'name': name, 'text': body,
|
||||||
return str(posts.insert_one(newPost).inserted_id)
|
'create_timestamp': str(time.time())}
|
||||||
|
newPost = posts.insert_one(newPostJson).inserted_id
|
||||||
|
result = dict(status=200, state='new')
|
||||||
|
return str(result)
|
||||||
|
|
||||||
def deletePost(name):
|
def deletePost(self, name):
|
||||||
return posts.delete_one({'name': 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)
|
||||||
|
|
||||||
|
|
||||||
|
class Metrics:
|
||||||
|
def alive():
|
||||||
|
return str("alive 1")
|
||||||
|
|
||||||
|
|
||||||
|
@route('/metrics')
|
||||||
|
def metrics():
|
||||||
|
return Metrics.alive()
|
||||||
|
|
||||||
|
|
||||||
@route('/post/<name>')
|
@route('/post/<name>')
|
||||||
@@ -62,25 +140,24 @@ def post(name):
|
|||||||
'''
|
'''
|
||||||
Get post
|
Get post
|
||||||
'''
|
'''
|
||||||
return Back.getPost(name)
|
return str(back.getPost(name))
|
||||||
|
|
||||||
|
|
||||||
@route('/post/<name>', method='POST')
|
@route('/admin/post/<name>', method='POST')
|
||||||
def postUpd(name):
|
def postUpd(name):
|
||||||
'''
|
'''
|
||||||
Insert/Update post
|
Insert/Update post
|
||||||
'''
|
'''
|
||||||
body = request.forms.get('body')
|
body = request.forms.get('body')
|
||||||
return Back.updatePost(name=name, body=body)
|
return back.updatePost(name=name, body=body)
|
||||||
|
|
||||||
|
|
||||||
@route('/post/<name>', method='DELETE')
|
@route('/admin/post/<name>', method='DELETE')
|
||||||
def postDel(name):
|
def postDel(name):
|
||||||
'''
|
'''
|
||||||
Delete post by name
|
Delete post by name
|
||||||
'''
|
'''
|
||||||
# return str(posts.delete_one({'name':name}))
|
return str(back.deletePost(name))
|
||||||
return str(Back.deletePost(name))
|
|
||||||
|
|
||||||
|
|
||||||
@route('/post')
|
@route('/post')
|
||||||
@@ -88,17 +165,18 @@ def all_posts():
|
|||||||
'''
|
'''
|
||||||
Returns all posts
|
Returns all posts
|
||||||
'''
|
'''
|
||||||
dict_posts = list()
|
return back.getAllPosts()
|
||||||
for i in posts.find():
|
|
||||||
dict_posts.append(i)
|
|
||||||
|
|
||||||
return str(dict_posts)
|
|
||||||
|
|
||||||
|
|
||||||
@route('/')
|
@route('/')
|
||||||
def index():
|
def index():
|
||||||
return Back.getRootPost()
|
return back.getRootPost()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
run(host='0.0.0.0', port=8081, reloader=True, debug=True)
|
print("Init")
|
||||||
|
cfg = Config()
|
||||||
|
print("Configured")
|
||||||
|
back = Back()
|
||||||
|
posts = cfg.posts
|
||||||
|
run(host=cfg.apphost, port=cfg.appport, reloader=cfg.appdebug, debug=cfg.appdebug)
|
||||||
|
|||||||
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
bottle==0.12.19
|
||||||
|
pymongo==3.12.0
|
||||||
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'
|
||||||
Reference in New Issue
Block a user