# Ejecucion y despliegue

## Ejecucion local

Desde la raiz del proyecto:

```powershell
node server.js
```

Salida esperada:

```text
World Cup 2026 API running at http://127.0.0.1:3000
Landing: http://127.0.0.1:3000/
API:     http://127.0.0.1:3000/api
```

Abrir:

```text
http://127.0.0.1:3000/
```

La produccion actual esta publicada en:

```text
https://worldcupfixtureapi.com
```

## Configuracion

Variables de entorno:

```text
HOST    Host de escucha. Default: 127.0.0.1
PORT    Puerto de escucha. Default: 3000
```

Ejemplo:

```powershell
$env:HOST="0.0.0.0"
$env:PORT="8080"
node server.js
```

Para publicar en un hosting, normalmente conviene usar:

```text
HOST=0.0.0.0
PORT=<puerto asignado por el proveedor>
```

## Verificacion manual

Landing:

```powershell
Invoke-WebRequest http://127.0.0.1:3000/ -UseBasicParsing
```

Metadata:

```powershell
Invoke-RestMethod http://127.0.0.1:3000/api
```

Partidos:

```powershell
Invoke-RestMethod "http://127.0.0.1:3000/api/matches?team=MEX&timezone=America/Argentina/Buenos_Aires"
```

ICS:

```powershell
Invoke-WebRequest "http://127.0.0.1:3000/api/calendar.ics?team=MEX" -UseBasicParsing
```

## Verificacion en produccion

Web app:

```powershell
Invoke-WebRequest https://worldcupfixtureapi.com/ -UseBasicParsing
```

Metadata:

```powershell
Invoke-RestMethod https://worldcupfixtureapi.com/api
```

Partidos:

```powershell
Invoke-RestMethod "https://worldcupfixtureapi.com/api/matches?team=MEX&timezone=America/Argentina/Buenos_Aires"
```

ICS:

```powershell
Invoke-WebRequest "https://worldcupfixtureapi.com/api/calendar.ics?team=ARG" -UseBasicParsing
```

## Validacion de sintaxis

Si `npm` esta disponible:

```powershell
npm run check
```

Si solo esta disponible `node`:

```powershell
node --check server.js
node --check app.js
node --check netlify\functions\api.js
```

## Despliegue en Netlify

El despliegue de produccion usa Netlify como web app con servicio serverless:

```text
Landing y visor: archivos estaticos publicados desde la raiz del proyecto
API: Netlify Function en netlify/functions/api.js
Rutas: /api y /api/* redirigidas a /.netlify/functions/api
Datos: output/worldcup_2026_fixture.json incluido en el bundle de la funcion
```

Archivos clave:

```text
netlify.toml
netlify/functions/api.js
server.js
output/worldcup_2026_fixture.json
index.html
fixture.html
app.js
landing.js
styles.css
fixture-data.js
match-metadata.js
```

El archivo `netlify.toml` contiene:

```toml
[build]
  publish = "."
  functions = "netlify/functions"

[functions]
  included_files = ["output/worldcup_2026_fixture.json"]

[[redirects]]
  from = "/api"
  to = "/.netlify/functions/api"
  status = 200

[[redirects]]
  from = "/api/*"
  to = "/.netlify/functions/api/:splat"
  status = 200
```

Publicar a produccion:

```powershell
netlify deploy --prod
```

Si el CLI no esta autenticado:

```powershell
netlify login
```

Notas:

- No usar `--allow-anonymous` para produccion: Netlify Drop puede crear sitios protegidos por password.
- Si `/api` responde `502`, revisar logs de funciones. El fallo mas probable es que `output/worldcup_2026_fixture.json` no haya quedado incluido en el bundle.
- La funcion reutiliza el handler de `server.js`; por eso local y produccion comparten la misma logica de endpoints.

## Despliegue alternativo en Node hosting

El proyecto no necesita build.

Archivos requeridos:

```text
server.js
package.json
index.html
fixture.html
app.js
styles.css
fixture-data.js
output/worldcup_2026_fixture.json
output/worldcup_2026_fixture.csv
netlify.toml
netlify/functions/api.js
```

Comando de inicio:

```text
node server.js
```

Si el proveedor usa `npm start`, `package.json` ya incluye:

```json
{
  "scripts": {
    "start": "node server.js"
  }
}
```

## Despliegue alternativo como sitio estatico

Si solo queres publicar la landing y el visor sin API dinamica, se pueden servir como archivos estaticos:

```text
index.html
fixture.html
app.js
styles.css
fixture-data.js
match-metadata.js
```

En ese modo no funcionan los endpoints `/api/*`, pero si funciona el visor `fixture.html`.

## Seguridad

- No publicar `.football-data-token`.
- No subir secretos al repositorio.
- Revisar terminos de uso de football-data.org antes de exponer datos publicamente.
- Si se agrega escritura, autenticacion o panel admin, separar esos endpoints del servidor publico.

## Operacion durante el torneo

Sugerencia de rutina:

```text
1. Descargar datos actualizados con fetch_worldcup_fixture.py.
2. Regenerar fixture-data.js.
3. Validar sintaxis con npm run check.
4. Publicar el nuevo deploy en Netlify.
5. Verificar /api, /api/matches y /api/calendar.ics en produccion.
```

Durante dias con partidos en vivo, conviene automatizar la actualizacion con mayor frecuencia y conservar snapshots para poder explicar cambios de horario, estado o resultado.
