-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
35 lines (27 loc) · 893 Bytes
/
server.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
import json
from dataclasses import dataclass
from flask import Flask, jsonify
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.sql import func
app = Flask(__name__)
app.config['DEBUG'] = True
# Connect to mariadb database
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:[email protected]:3306/flask_recipe'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
# Difine Recipe model table
@dataclass
class Recipe(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(255), nullable=False)
content = db.Column(db.Text, nullable=False)
created_at = db.Column(db.DateTime(timezone=True),
server_default=func.now())
def __repr__(self):
return f'<Recipe> {self.title}'
# Routes
@app.route('/api/recipes')
def index():
recipes = Recipe.query.all()
return jsonify(recipes)
app.run()