-
Notifications
You must be signed in to change notification settings - Fork 17
/
simplebin.py
executable file
·77 lines (57 loc) · 1.68 KB
/
simplebin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python3
import random
import string
import sys
from pathlib import Path
ROOT_PATH = Path(__file__).parent
sys.path.append(ROOT_PATH)
import bottle
bottle.TEMPLATE_PATH = [ROOT_PATH]
### Configuration ###
HTTP_HOST = 'localhost'
HTTP_PORT = 8000
DEFAULT_NAME_LENGTH = 6
STORAGE_PATH = ROOT_PATH.joinpath('storage')
STORAGE_PATH.mkdir(exist_ok=True)
### Storage ###
class Snippet:
def __init__(self, name, code):
self.name = name
self.code = code
@classmethod
def get_by_name(cls, name):
try:
code = STORAGE_PATH.joinpath(name).read_text()
except FileNotFoundError as exc:
raise ValueError(f"Snippet {name!r} not found") from exc
return cls(name, code)
def save(self):
STORAGE_PATH.joinpath(self.name).write_text(self.code)
@staticmethod
def new_name(length=DEFAULT_NAME_LENGTH):
name = ''.join(random.choices(string.ascii_letters, k=length))
if STORAGE_PATH.joinpath(name).exists():
raise ValueError("Cannot create a new unique id.")
return name
### API ###
@bottle.route('/status')
def status():
return "alive"
@bottle.route('/')
def new():
return bottle.template('newbin') # newbin.tpl
@bottle.route('/<name>')
def get(name):
try:
snippet = Snippet.get_by_name(name)
except ValueError:
bottle.abort(404, "Snippet not found")
return snippet.code
@bottle.route('/save', method='POST')
def save():
snippet = Snippet(Snippet.new_name(), bottle.request.forms.code)
snippet.save()
return bottle.redirect(f'/{snippet.name}')
### Server ###
if __name__ == '__main__':
bottle.run(host=HTTP_HOST, port=HTTP_PORT)