@@ -8,10 +8,10 @@ import { checkWebsites } from './services/http.js';
88
99const __dirname = dirname ( fileURLToPath ( import . meta. url ) ) ;
1010
11- const PORT = process . env . PORT || 3000 ;
12- const BASE_URL = process . env . BASE_URL || `http://localhost:${ PORT } ` ;
13- const AUTH_USER = process . env . AUTH_USER || 'admin' ;
14- const AUTH_PASS = process . env . AUTH_PASS || 'admin' ;
11+ const PORT = process . env . PORT || 3000 ;
12+ const BASE_URL = process . env . BASE_URL || `http://localhost:${ PORT } ` ;
13+ const AUTH_USER = process . env . AUTH_USER || 'admin' ;
14+ const AUTH_PASS = process . env . AUTH_PASS || 'admin' ;
1515const SESSION_SECRET = process . env . SESSION_SECRET || 'change-me' ;
1616
1717const app = express ( ) ;
@@ -24,7 +24,14 @@ app.use(session({
2424 cookie : { httpOnly : true , maxAge : 8 * 60 * 60 * 1000 } ,
2525} ) ) ;
2626
27- // --- Auth routes (publiques) ---
27+ // --- Routes publiques ---
28+
29+ app . get ( '/' , ( req , res ) => {
30+ if ( req . session . authenticated ) {
31+ return res . sendFile ( join ( __dirname , '../public/index.html' ) ) ;
32+ }
33+ res . sendFile ( join ( __dirname , '../public/home.html' ) ) ;
34+ } ) ;
2835
2936app . post ( '/auth/login' , ( req , res ) => {
3037 const { username, password } = req . body ;
@@ -40,28 +47,20 @@ app.post('/auth/logout', (req, res) => {
4047 res . json ( { ok : true } ) ;
4148} ) ;
4249
43- // --- Middleware auth ---
50+ // Fichiers statiques (style.css, app.js, login.html, home.html…)
51+ // index.html n'est pas accessible directement — servi uniquement via GET /
52+ app . use ( express . static ( join ( __dirname , '../public' ) ) ) ;
53+
54+ // --- Middleware auth pour l'API ---
4455
4556function requireAuth ( req , res , next ) {
4657 if ( req . session . authenticated ) return next ( ) ;
47- if ( req . path . startsWith ( '/api/' ) ) return res . status ( 401 ) . json ( { error : 'Non authentifié' } ) ;
48- res . redirect ( '/login.html' ) ;
58+ res . status ( 401 ) . json ( { error : 'Non authentifié' } ) ;
4959}
5060
51- // Fichiers statiques publics (login.html, style.css)
52- app . use ( express . static ( join ( __dirname , '../public' ) ) ) ;
53-
54- // Toutes les routes suivantes sont protégées
55- app . use ( requireAuth ) ;
56-
57- // Redirige / vers index.html (déjà servi en statique, mais protégé via middleware)
58- app . get ( '/' , ( _req , res ) => {
59- res . sendFile ( join ( __dirname , '../public/index.html' ) ) ;
60- } ) ;
61-
62- // --- API ---
61+ // --- API (protégée) ---
6362
64- app . get ( '/api/status' , async ( _req , res ) => {
63+ app . get ( '/api/status' , requireAuth , async ( _req , res ) => {
6564 try {
6665 const [ containers , websites ] = await Promise . all ( [
6766 getContainers ( ) ,
@@ -81,7 +80,7 @@ app.get('/api/status', async (_req, res) => {
8180 }
8281} ) ;
8382
84- app . post ( '/api/container/restart' , async ( req , res ) => {
83+ app . post ( '/api/container/restart' , requireAuth , async ( req , res ) => {
8584 const { name } = req . body ;
8685 if ( ! name ) return res . status ( 400 ) . json ( { error : 'name requis' } ) ;
8786 try {
@@ -92,7 +91,7 @@ app.post('/api/container/restart', async (req, res) => {
9291 }
9392} ) ;
9493
95- app . post ( '/api/container/stop' , async ( req , res ) => {
94+ app . post ( '/api/container/stop' , requireAuth , async ( req , res ) => {
9695 const { name } = req . body ;
9796 if ( ! name ) return res . status ( 400 ) . json ( { error : 'name requis' } ) ;
9897 try {
@@ -103,7 +102,7 @@ app.post('/api/container/stop', async (req, res) => {
103102 }
104103} ) ;
105104
106- app . post ( '/api/container/start' , async ( req , res ) => {
105+ app . post ( '/api/container/start' , requireAuth , async ( req , res ) => {
107106 const { name } = req . body ;
108107 if ( ! name ) return res . status ( 400 ) . json ( { error : 'name requis' } ) ;
109108 try {
0 commit comments