first commit

This commit is contained in:
root 2021-01-06 11:07:44 +01:00
commit 267bd9d888
4 changed files with 84 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
env/

8
README.md Normal file
View File

@ -0,0 +1,8 @@
# VPN GOLEM
endpoint | method | body | description
---------+--------+------+-------------
/users | GET | | get list of users
/users | POST | ```{ "name": "johndoe" }``` | create new user

68
main.py Normal file
View File

@ -0,0 +1,68 @@
import sqlite3
import sys
from flask import Flask, request, jsonify
app = Flask(__name__)
DATABASE='/data/database.sqlite3'
db = sqlite3.connect(DATABASE)
cu = db.cursor()
db.execute('SELECT name FROM sqlite_master')
if len(cu.fetchall()) == 0:
print('creating database schema...')
db.execute('CREATE TABLE IF NOT EXISTS user (id INTEGER PRIMARY KEY, name TEXT UNIQUE)')
db.execute('CREATE TABLE IF NOT EXISTS gateway (id INTEGER PRIMARY KEY, name TEXT UNIQUE, user INTEGER REFERENCES user(id))')
db.commit()
cu.close()
db.close()
@app.route('/')
def hello_world():
return 'It works!'
@app.route('/users', methods=['GET'])
def get_users():
db = sqlite3.connect(DATABASE)
cu = db.cursor()
users = []
for row in cu.execute('SELECT id, name FROM user'):
users.append({'id': row[0], 'name': row[1]})
cu.close()
db.close()
return jsonify(users)
@app.route('/users', methods=['POST'])
def post_users():
db = sqlite3.connect(DATABASE)
cu = db.cursor()
name = request.json['name']
print('creating ' + str(name))
try:
cu.execute('INSERT INTO user (name) VALUES (?)', (name,))
return jsonify({'status': 'ok'})
except sqlite3.Error as e:
return jsonify({'status': 'error', 'message': str(e)}), 409
finally:
db.commit()
cu.close()
db.close()
@app.route('/gateways', methods=['GET'])
def get_gateway():
db = sqlite3.connect(DATABASE)
cu = db.cursor()
gateways = []
for row in cu.execute('SELECT id, name, user FROM gateway'):
gateways.append({'id': row[0], 'name': row[1], 'user': row[2], 'network': row[3]})
cu.close()
db.close()
return jsonify(gateways)
if __name__ == '__main__':
app.run(host="::", port=5000, debug=True)

7
requirements.txt Normal file
View File

@ -0,0 +1,7 @@
click==7.1.2
Flask==1.1.2
itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
pkg-resources==0.0.0
Werkzeug==1.0.1