This commit is contained in:
pika 2025-03-30 23:42:24 +02:00
parent 3b2f1db4ce
commit 5c16964b76
47 changed files with 2080 additions and 1053 deletions

145
run.py
View file

@ -15,92 +15,100 @@ import json
current_dir = os.path.abspath(os.path.dirname(__file__))
sys.path.insert(0, current_dir)
def create_basic_app():
"""Create a Flask app without database dependencies"""
app = Flask(__name__,
template_folder=os.path.join(current_dir, 'app', 'templates'),
static_folder=os.path.join(current_dir, 'app', 'static'))
app = Flask(
__name__,
template_folder=os.path.join(current_dir, "app", "templates"),
static_folder=os.path.join(current_dir, "app", "static"),
)
# Basic configuration
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'dev-key-placeholder')
app.config['DEBUG'] = True
app.config["SECRET_KEY"] = os.environ.get("SECRET_KEY", "dev-key-placeholder")
app.config["DEBUG"] = True
# Register basic routes
register_routes(app)
# Add a fallback index route if no routes match
@app.route('/')
@app.route("/")
def index():
return "Your Network Management Flask Application is running! Navigate to /dashboard to see content."
return app
def register_routes(app):
"""Register blueprints without database dependencies"""
routes_dir = os.path.join(current_dir, 'app', 'routes')
routes_dir = os.path.join(current_dir, "app", "routes")
# Check if routes directory exists
if not os.path.isdir(routes_dir):
print(f"Warning: Routes directory {routes_dir} not found")
return
# Try to register API blueprint which is simplest
try:
from app.routes.api import bp as api_bp
app.register_blueprint(api_bp)
print("Registered API blueprint")
except Exception as e:
print(f"Could not register API blueprint: {e}")
# Try to register other blueprints with basic error handling
try:
from app.routes.dashboard import bp as dashboard_bp
app.register_blueprint(dashboard_bp)
print("Registered dashboard blueprint")
except ImportError as e:
print(f"Could not import dashboard blueprint: {e}")
try:
from app.routes.ipam import bp as ipam_bp
app.register_blueprint(ipam_bp)
print("Registered IPAM blueprint")
except ImportError as e:
print(f"Could not import IPAM blueprint: {e}")
# Create a development application instance
print("Starting Flask app with SQLite database...")
app = create_app('development')
app = create_app("development")
@app.shell_context_processor
def make_shell_context():
return {
'db': db,
'User': User,
'Server': Server,
'Subnet': Subnet,
'App': App,
'Port': Port
"db": db,
"User": User,
"Server": Server,
"Subnet": Subnet,
"App": App,
"Port": Port,
}
def init_db():
"""Initialize database tables"""
with app.app_context():
db.create_all()
def create_admin_user():
"""Create an admin user if no users exist"""
with app.app_context():
if User.query.count() == 0:
admin = User(
username='admin',
email='admin@example.com',
is_admin=True
)
admin.set_password('admin')
admin = User(username="admin", email="admin@example.com", is_admin=True)
admin.set_password("admin")
db.session.add(admin)
db.session.commit()
print("Created admin user: admin@example.com (password: admin)")
# Update seed_data to use consistent structures
def seed_data():
"""Add some sample data to the database"""
@ -108,42 +116,75 @@ def seed_data():
# Only seed if the database is empty
if Subnet.query.count() == 0:
# Create sample subnets
subnet1 = Subnet(cidr='192.168.1.0/24', location='Office', active_hosts=json.dumps([]))
subnet2 = Subnet(cidr='10.0.0.0/24', location='Datacenter', active_hosts=json.dumps([]))
subnet1 = Subnet(
cidr="192.168.1.0/24", location="Office", active_hosts=json.dumps([])
)
subnet2 = Subnet(
cidr="10.0.0.0/24", location="Datacenter", active_hosts=json.dumps([])
)
db.session.add_all([subnet1, subnet2])
db.session.commit()
# Create sample servers
server1 = Server(hostname='web-server', ip_address='192.168.1.10', subnet=subnet1)
server2 = Server(hostname='db-server', ip_address='192.168.1.11', subnet=subnet1)
server3 = Server(hostname='app-server', ip_address='10.0.0.5', subnet=subnet2)
server1 = Server(
hostname="web-server", ip_address="192.168.1.10", subnet=subnet1
)
server2 = Server(
hostname="db-server", ip_address="192.168.1.11", subnet=subnet1
)
server3 = Server(
hostname="app-server", ip_address="10.0.0.5", subnet=subnet2
)
db.session.add_all([server1, server2, server3])
db.session.commit()
# Create sample apps
app1 = App(name='Website', server=server1, documentation='# Company Website\nRunning on Nginx/PHP')
app2 = App(name='PostgreSQL', server=server2, documentation='# Database Server\nPostgreSQL 15')
app3 = App(name='API Service', server=server3, documentation='# REST API\nNode.js service')
app1 = App(
name="Website",
server=server1,
documentation="# Company Website\nRunning on Nginx/PHP",
)
app2 = App(
name="PostgreSQL",
server=server2,
documentation="# Database Server\nPostgreSQL 15",
)
app3 = App(
name="API Service",
server=server3,
documentation="# REST API\nNode.js service",
)
db.session.add_all([app1, app2, app3])
db.session.commit()
# Create sample ports
port1 = Port(app=app1, port_number=80, protocol='TCP', description='HTTP')
port2 = Port(app=app1, port_number=443, protocol='TCP', description='HTTPS')
port3 = Port(app=app2, port_number=5432, protocol='TCP', description='PostgreSQL')
port4 = Port(app=app3, port_number=3000, protocol='TCP', description='Node.js API')
port1 = Port(app=app1, port_number=80, protocol="TCP", description="HTTP")
port2 = Port(app=app1, port_number=443, protocol="TCP", description="HTTPS")
port3 = Port(
app=app2, port_number=5432, protocol="TCP", description="PostgreSQL"
)
port4 = Port(
app=app3, port_number=3000, protocol="TCP", description="Node.js API"
)
db.session.add_all([port1, port2, port3, port4])
db.session.commit()
print("Sample data has been added to the database")
if __name__ == '__main__':
if __name__ == "__main__":
# Create the app first
app = create_app()
# Initialize database if needed
if not os.path.exists('app.db') and 'sqlite' in app.config['SQLALCHEMY_DATABASE_URI']:
if (
not os.path.exists("app.db")
and "sqlite" in app.config["SQLALCHEMY_DATABASE_URI"]
):
print("Database not found, initializing...")
try:
init_db()
@ -153,10 +194,10 @@ if __name__ == '__main__':
except Exception as e:
print(f"Error initializing database: {e}")
sys.exit(1)
# Run the application
try:
app.run(debug=True, port=5000)
except Exception as e:
print(f"Error starting Flask app: {e}")
sys.exit(1)
sys.exit(1)