Skip to content

Commit f757b4b

Browse files
committed
Initial commit
0 parents  commit f757b4b

File tree

10 files changed

+223
-0
lines changed

10 files changed

+223
-0
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.gitignore

.gitignore

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Backup files
2+
*.~
3+
4+
# Byte-compiles / optimizied
5+
__pycache__/
6+
*.py[cod]
7+
8+
# C extensions
9+
*.so
10+
11+
# Distribution / packaging
12+
bin/
13+
build/
14+
develop-eggs/
15+
dist/
16+
eggs/
17+
.eggs/
18+
lib/
19+
lib64/
20+
parts/
21+
sdist/
22+
var/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
MANIFEST
27+
28+
# Installer logs
29+
pip-log.txt
30+
pip-delete-this-directory.txt
31+
32+
# Unit test / coverage reports
33+
.tox/
34+
.coverage
35+
.cache
36+
nosetests.xml
37+
coverage.xml
38+
39+
# Translations
40+
*.mo
41+
42+
# Sphinx documentation
43+
docs/_build/
44+
45+
# Ignore sublime's project (for fans)
46+
.ropeproject/
47+
*.sublime-project
48+
*.sublime-workspace
49+
50+
# Ignore virtualenvs (who places it near)
51+
.venv/
52+
53+
# Various shit from the OS itself
54+
.DS_Store
55+
56+
# building of Docker images
57+
.docker

Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM python:latest
2+
MAINTAINER The XSnippet Team <[email protected]>
3+
4+
COPY . /app
5+
WORKDIR /app
6+
RUN pip install -e .
7+
8+
EXPOSE 5000
9+
ENTRYPOINT ["python", "-m", "xsnippet.web_backend"]

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 The XSnippet Team
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
====================
2+
XSnippet Web Backend
3+
====================
4+
5+
XSnippet is a simple web-service for sharing code snippets on the Internet.
6+
Years ago it was started as educational project, and nothing changed since
7+
then. That's why the fourth reincarnation is written in Python using
8+
``asyncio``.
9+
10+
xsnippet-web-backend is a web-service, that is meant to be deployed alongside
11+
the xsnippet-web SPA and helps to implement the features, which can't be
12+
implemented purely on the client side (e.g. "raw" content of the snippets
13+
served as plain text over HTTP or "embed" mode, etc).
14+
15+
The source code is distributed under MIT license. Feel free to contribute by
16+
sending us pull requests or patches. Your feedback is welcome as well!
17+
18+
19+
Links
20+
=====
21+
22+
* Source: https://github.com/xsnippet/xsnippet-web-backend
23+
* Bugs: https://github.com/xsnippet/xsnippet-web-backend/issues

setup.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
4+
import os
5+
from setuptools import setup, find_packages as _find_packages
6+
7+
from xsnippet.web_backend import __version__ as version
8+
from xsnippet.web_backend import __license__ as license
9+
10+
11+
here = os.path.dirname(__file__)
12+
with open(os.path.join(here, 'README.rst'), 'r', encoding='utf-8') as f:
13+
long_description = f.read()
14+
15+
16+
# Unfortunately setuptools.find_packages() doesn't support PEP-420 namespace
17+
# packages so we need our own implementation that does. All this shit happened
18+
# due to desperate @ikalnytskyi's desire to use namespace packages.
19+
def find_packages(namespace):
20+
return ['%s.%s' % (namespace, pkg) for pkg in _find_packages(namespace)]
21+
22+
23+
setup(
24+
name='xsnippet-web-backend',
25+
version=version,
26+
description=(
27+
'XSnippet is a simple web-service for sharing code snippets on the '
28+
'Internet. Written for fun using bleeding edge technologies.'),
29+
long_description=long_description,
30+
license=license,
31+
url='https://github.com/xsnippet/xsnippet-web-backend/',
32+
keywords='web-service restful-api snippet storage',
33+
author='The XSnippet Team',
34+
author_email='[email protected]',
35+
packages=find_packages('xsnippet'),
36+
zip_safe=False,
37+
setup_requires=[
38+
'pytest-runner',
39+
],
40+
install_requires=[
41+
'aiohttp >= 2.3.5',
42+
],
43+
tests_require=[
44+
'pytest >= 2.8.7',
45+
'pytest-aiohttp >= 0.3.0',
46+
],
47+
classifiers=[
48+
'Environment :: Web Environment',
49+
'License :: OSI Approved :: MIT License',
50+
'Operating System :: OS Independent',
51+
'Intended Audience :: End Users/Desktop',
52+
'Intended Audience :: Information Technology',
53+
'Topic :: Internet :: WWW/HTTP',
54+
'Programming Language :: Python',
55+
'Programming Language :: Python :: 3',
56+
'Programming Language :: Python :: 3.5',
57+
'Programming Language :: Python :: 3.6',
58+
],
59+
)

xsnippet/web_backend/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
__version__ = '4.0.0'
2+
__license__ = 'MIT'

xsnippet/web_backend/__main__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import logging
2+
3+
import aiohttp
4+
5+
from . import conf
6+
from .app import create_app
7+
8+
9+
# write access/error log to stderr
10+
logging.basicConfig()
11+
logger = logging.getLogger('aiohttp').setLevel(logging.INFO)
12+
13+
# create an app instance and listen forever
14+
app = create_app(conf)
15+
aiohttp.web.run_app(app, host=conf.LISTEN_HOST, port=conf.LISTEN_PORT)

xsnippet/web_backend/app.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import aiohttp
2+
import aiohttp.web
3+
4+
5+
async def get_raw_snippet_content(request):
6+
try:
7+
_id = int(request.match_info['id'])
8+
except (IndexError, ValueError):
9+
raise aiohttp.web.HTTPBadRequest('id must be specified and must be an integer')
10+
11+
# fetch the snippet from the API
12+
async with aiohttp.ClientSession() as session:
13+
url = request.app.conf.API_URL + '/snippets/' + str(_id)
14+
async with session.get(url) as response:
15+
if response.status == 404:
16+
raise aiohttp.web.HTTPNotFound
17+
18+
snippet = await response.json()
19+
20+
return aiohttp.web.Response(text=snippet['content'])
21+
22+
23+
def create_app(conf):
24+
app = aiohttp.web.Application()
25+
app.conf = conf
26+
27+
app.router.add_get('/snippets/{id}/raw', get_raw_snippet_content)
28+
29+
return app

xsnippet/web_backend/conf.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import os
2+
3+
4+
API_URL = os.environ.get('XSNIPPET_API_URL', 'http://api.xsnippet.org')
5+
6+
LISTEN_HOST = os.environ.get('XSNIPPET_WEB_PROXY_HOST', '0.0.0.0')
7+
LISTEN_PORT = os.environ.get('XSNIPPET_WEB_PROXY_PORT', 5000)

0 commit comments

Comments
 (0)