File size: 2,273 Bytes
2166cf5
 
 
 
465c4c7
 
 
 
 
 
2166cf5
 
 
465c4c7
2166cf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
465c4c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2166cf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
387c610
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
from flask import Flask
from flask_login import LoginManager
from flask_ckeditor import CKEditor
from models import db, User
import json
import os
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

def create_app():
    app = Flask(__name__)
    app.config['SECRET_KEY'] = os.getenv('SECRET_KEY', 'your-secret-key-here')
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///gangs.db'
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    
    # Initialize extensions
    db.init_app(app)
    ckeditor = CKEditor(app)
    
    # Initialize LoginManager
    login_manager = LoginManager()
    login_manager.init_app(app)
    login_manager.login_view = 'auth.login'
    
    @login_manager.user_loader
    def load_user(user_id):
        return User.query.get(int(user_id))
    
    # Create necessary data files and admin user on startup
    def create_data_files():
        # Create data directory if it doesn't exist
        os.makedirs('data', exist_ok=True)

        # Initialize users.json with admin user from environment variables
        if not os.path.exists('data/users.json'):
            admin_data = {
                'users': [{
                    'id': 1,
                    'username': os.getenv('ADMIN_USERNAME', 'admin'),
                    'password': os.getenv('ADMIN_PASSWORD', 'admin')
                }]
            }
            with open('data/users.json', 'w') as f:
                json.dump(admin_data, f, indent=4)

        # Initialize gangs.json if it doesn't exist
        if not os.path.exists('data/gangs.json'):
            with open('data/gangs.json', 'w') as f:
                json.dump({'gangs': []}, f, indent=4)

        # Initialize blogs.json if it doesn't exist
        if not os.path.exists('data/blogs.json'):
            with open('data/blogs.json', 'w') as f:
                json.dump({'blogs': []}, f, indent=4)

    create_data_files()

    # Register blueprints
    from routes import main
    app.register_blueprint(main)
    
    from admin import admin
    app.register_blueprint(admin)
    
    # Create database tables
    with app.app_context():
        db.create_all()
    
    return app

if __name__ == '__main__':
    app = create_app()
    app.run(host='0.0.0.0', port=7860)