Compare commits
12 Commits
f72c941a3a
...
drone
| Author | SHA1 | Date | |
|---|---|---|---|
| 4ab80616a6 | |||
| 04905c59db | |||
| c4602b9d4e | |||
| 2f5867028e | |||
| 1aa4be2e47 | |||
| 3aa77129ec | |||
| 32a2678170 | |||
| 6cd33f25be | |||
| 7c7db5bca0 | |||
| 755e7c5ce4 | |||
| 85a860eb1a | |||
| 5aecf2d643 |
20
.drone.yml
Normal file
20
.drone.yml
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
kind: pipeline
|
||||||
|
type: exec
|
||||||
|
name: default
|
||||||
|
|
||||||
|
platform:
|
||||||
|
os: linux
|
||||||
|
arch: amd64
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: build
|
||||||
|
commands:
|
||||||
|
- docker-compose -p pycms build
|
||||||
|
- name: test
|
||||||
|
commands:
|
||||||
|
- docker-compose -p pycms up --build --abort-on-container-exit
|
||||||
|
- name: run
|
||||||
|
commands:
|
||||||
|
- docker-compose -p pycms up -d
|
||||||
|
|
||||||
|
|
||||||
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
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
[DB]
|
[DB]
|
||||||
host = localhost
|
host = db
|
||||||
port = 27017
|
port = 27017
|
||||||
name = pycms
|
name = pycms
|
||||||
|
|
||||||
|
|||||||
18
docker-compose.yml
Normal file
18
docker-compose.yml
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
version: '3'
|
||||||
|
services:
|
||||||
|
server:
|
||||||
|
build: .
|
||||||
|
ports:
|
||||||
|
- 8080:8080
|
||||||
|
volumes:
|
||||||
|
- ./config.ini:/app/config.ini
|
||||||
|
depends_on:
|
||||||
|
- db
|
||||||
|
db:
|
||||||
|
image: mongo
|
||||||
|
tests:
|
||||||
|
build: .
|
||||||
|
command: bash ./test.sh
|
||||||
|
depends_on:
|
||||||
|
- db
|
||||||
|
- server
|
||||||
42
main.py
42
main.py
@@ -1,32 +1,42 @@
|
|||||||
#!/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
|
||||||
|
# TODO: auth to /admin
|
||||||
|
# TODO: timestamps to posts
|
||||||
|
# TODO: author to posts and multiple users
|
||||||
|
# TODO: add bottle's params to config
|
||||||
|
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
"""
|
"""
|
||||||
posts - public, posts table from MongoClient
|
posts - table with posts
|
||||||
config: private, config for DB
|
config structure:
|
||||||
config.db.host
|
DB:
|
||||||
config.db.port
|
- host
|
||||||
config.db.dbname
|
- port
|
||||||
|
- dbname
|
||||||
"""
|
"""
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""
|
"""
|
||||||
Init init
|
Init config
|
||||||
"""
|
"""
|
||||||
self.readConfig()
|
self.readConfig()
|
||||||
|
|
||||||
mongoclient = pymongo.MongoClient(self.host, self.port)
|
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]
|
database = mongoclient[self.dbname]
|
||||||
|
|
||||||
# TODO: Create table if not exists
|
# TODO: Create table if not exists
|
||||||
|
if 'posts' not in database.list_collection_names():
|
||||||
|
print('Table not fount, creating')
|
||||||
posts = database['posts']
|
posts = database['posts']
|
||||||
self.posts = posts
|
self.posts = posts
|
||||||
|
|
||||||
def readConfig(self):
|
def readConfig(self):
|
||||||
"""
|
"""
|
||||||
Read config file, if not exists - call Init.createConfig()
|
Read config file, if not exists - call self.createConfig()
|
||||||
"""
|
"""
|
||||||
import configparser
|
import configparser
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
@@ -70,23 +80,28 @@ class Back():
|
|||||||
return abort(404, 'No such page')
|
return abort(404, 'No such page')
|
||||||
|
|
||||||
def getAllPosts(self):
|
def getAllPosts(self):
|
||||||
|
# TODO: clear up output, remove '_id' and 'body',
|
||||||
|
# now there should be only
|
||||||
dict_posts = list()
|
dict_posts = list()
|
||||||
for i in posts.find():
|
for i in posts.find():
|
||||||
dict_posts.append(i)
|
dict_posts.append(i)
|
||||||
return str(dict_posts)
|
return str(dict_posts)
|
||||||
|
|
||||||
def updatePost(self, name, body):
|
def updatePost(self, name, body):
|
||||||
|
# TODO: return RESTful error/success result
|
||||||
# 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}}
|
newPost = {'$set': {'text': body}}
|
||||||
return str(posts.update_one({'name': name}, newPost))
|
return str(posts.update_one({'name': name}, newPost))
|
||||||
# Else - create new
|
# Else - create new
|
||||||
else:
|
else:
|
||||||
newPost = {'name': name, 'text': body}
|
newPost = {'name': name, 'text': body,
|
||||||
|
'create_timestamp': str(time.time())}
|
||||||
return str(posts.insert_one(newPost).inserted_id)
|
return str(posts.insert_one(newPost).inserted_id)
|
||||||
|
|
||||||
def deletePost(self, name):
|
def deletePost(self, name):
|
||||||
return posts.delete_one({'name': name})
|
# TODO: return RESTful error/success result
|
||||||
|
return bool(posts.delete_one({'name': name}).deleted_count)
|
||||||
|
|
||||||
|
|
||||||
@route('/post/<name>')
|
@route('/post/<name>')
|
||||||
@@ -97,7 +112,7 @@ def post(name):
|
|||||||
return str(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
|
||||||
@@ -106,12 +121,11 @@ def postUpd(name):
|
|||||||
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))
|
||||||
|
|
||||||
|
|
||||||
@@ -129,7 +143,9 @@ def index():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
print("Init")
|
||||||
cfg = Config()
|
cfg = Config()
|
||||||
|
print("Configured")
|
||||||
back = Back()
|
back = Back()
|
||||||
posts = cfg.posts
|
posts = cfg.posts
|
||||||
run(host='0.0.0.0', port=8081, reloader=True, debug=True)
|
run(host='0.0.0.0', port=8080, reloader=True, debug=True)
|
||||||
|
|||||||
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
bottle==0.12.19
|
||||||
|
pymongo==3.12.0
|
||||||
10
test.sh
Normal file
10
test.sh
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
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'
|
||||||
|
|
||||||
|
$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'
|
||||||
Reference in New Issue
Block a user