Compare commits
4 Commits
fix/cloudf
...
feat/issue
| Author | SHA1 | Date | |
|---|---|---|---|
| 856e20c19c | |||
| 6ca0256bf7 | |||
| 70759d6c1c | |||
| 5ab329adbf |
16
.env.example
Normal file
16
.env.example
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
PUBLIC_CONTACT_FORM_ACTION_URL=/api/contact
|
||||||
|
PUBLIC_CONTACT_FORM_SUCCESS_REDIRECT=http://localhost:4321/#contatti
|
||||||
|
PUBLIC_CONTACT_FORM_MAILTO=
|
||||||
|
PUBLIC_CONTACT_TURNSTILE_SITE_KEY=
|
||||||
|
|
||||||
|
CONTACT_FORM_MODE=dev
|
||||||
|
CONTACT_FORM_RESEND_API_KEY=
|
||||||
|
CONTACT_FORM_FROM_EMAIL=no-reply@nexstudio.com
|
||||||
|
CONTACT_FORM_TO_EMAIL=info@nexstudio.com
|
||||||
|
CONTACT_FORM_TO_EMAIL_SUPPORT=support@nexstudio.com
|
||||||
|
CONTACT_FORM_TO_EMAIL_PRIVACY=
|
||||||
|
CONTACT_FORM_TO_EMAIL_SECURITY=
|
||||||
|
CONTACT_FORM_TO_EMAIL_ETHICS=
|
||||||
|
CONTACT_FORM_TO_EMAIL_LEGAL=
|
||||||
|
CONTACT_FORM_TO_EMAIL_HR=
|
||||||
|
CONTACT_TURNSTILE_SECRET_KEY=
|
||||||
52
README.md
52
README.md
@ -50,3 +50,55 @@ If you deploy with Wrangler in CI/Cloudflare, build first and deploy using Astro
|
|||||||
- Deploy command: `npm run deploy:cf`
|
- Deploy command: `npm run deploy:cf`
|
||||||
|
|
||||||
This avoids entrypoint errors like `@astrojs/cloudflare/entrypoints/server` not found.
|
This avoids entrypoint errors like `@astrojs/cloudflare/entrypoints/server` not found.
|
||||||
|
|
||||||
|
## Contact form configuration
|
||||||
|
|
||||||
|
Set these variables for the website contact form.
|
||||||
|
|
||||||
|
Public (frontend):
|
||||||
|
|
||||||
|
- `PUBLIC_CONTACT_FORM_ACTION_URL` (default `/api/contact`)
|
||||||
|
- `PUBLIC_CONTACT_FORM_SUCCESS_REDIRECT` (absolute URL for `_next`)
|
||||||
|
- `PUBLIC_CONTACT_FORM_MAILTO` (fallback only if `ACTION_URL` is empty)
|
||||||
|
- `PUBLIC_CONTACT_TURNSTILE_SITE_KEY`
|
||||||
|
|
||||||
|
Server-side (Cloudflare runtime / local `.env`):
|
||||||
|
|
||||||
|
- `CONTACT_FORM_MODE` (`dev` or `live`)
|
||||||
|
- `CONTACT_FORM_RESEND_API_KEY`
|
||||||
|
- `CONTACT_FORM_FROM_EMAIL` (verified sender in Resend, recommended `no-reply@nexstudio.com`)
|
||||||
|
- `CONTACT_FORM_TO_EMAIL` (default recipient inbox or alias, recommended `info@nexstudio.com`)
|
||||||
|
- `CONTACT_FORM_TO_EMAIL_SUPPORT`
|
||||||
|
- `CONTACT_FORM_TO_EMAIL_PRIVACY`
|
||||||
|
- `CONTACT_FORM_TO_EMAIL_SECURITY`
|
||||||
|
- `CONTACT_FORM_TO_EMAIL_ETHICS`
|
||||||
|
- `CONTACT_FORM_TO_EMAIL_LEGAL`
|
||||||
|
- `CONTACT_FORM_TO_EMAIL_HR`
|
||||||
|
- `CONTACT_TURNSTILE_SECRET_KEY`
|
||||||
|
|
||||||
|
Mode behavior:
|
||||||
|
|
||||||
|
- `dev`: accepts form and logs payload server-side (no real email send)
|
||||||
|
- `live`: verifies Turnstile (if secret configured) and sends via Resend API
|
||||||
|
|
||||||
|
Department routing:
|
||||||
|
|
||||||
|
- the contact form includes a department selector
|
||||||
|
- if a department-specific env is set, the email is routed to that recipient
|
||||||
|
- otherwise it falls back to `CONTACT_FORM_TO_EMAIL`
|
||||||
|
|
||||||
|
Suggested operational use:
|
||||||
|
|
||||||
|
- `info@...` for prospects and generic clarifications (non-clients)
|
||||||
|
- `support@...` for existing customers
|
||||||
|
- `no-reply@...` as sender for newsletters and automated communications
|
||||||
|
|
||||||
|
Copy `.env.example` to `.env` for local development and configure the same keys in Cloudflare Pages/Workers for production.
|
||||||
|
|
||||||
|
## Production domain
|
||||||
|
|
||||||
|
- Official domain: `https://nexstudio.ai`
|
||||||
|
- Keep Turnstile widget hostnames aligned with:
|
||||||
|
- `nexstudio.ai`
|
||||||
|
- `www.nexstudio.ai` (if used)
|
||||||
|
- `localhost` (for local testing)
|
||||||
|
|||||||
@ -34,7 +34,7 @@ function viteSsrOptimizeIgnoreOutdated() {
|
|||||||
// https://astro.build/config
|
// https://astro.build/config
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
/** Usato da Astro per URL assoluti (es. sitemap). Imposta il dominio di produzione. */
|
/** Usato da Astro per URL assoluti (es. sitemap). Imposta il dominio di produzione. */
|
||||||
// site: 'https://www.tuodominio.com',
|
site: 'https://nexstudio.ai',
|
||||||
|
|
||||||
output: 'server', // per il deployment su Cloudflare Pages
|
output: 'server', // per il deployment su Cloudflare Pages
|
||||||
|
|
||||||
|
|||||||
@ -11,6 +11,7 @@ const model = {
|
|||||||
fieldName: cf.fieldName,
|
fieldName: cf.fieldName,
|
||||||
fieldCompany: cf.fieldCompany,
|
fieldCompany: cf.fieldCompany,
|
||||||
fieldCountry: cf.fieldCountry,
|
fieldCountry: cf.fieldCountry,
|
||||||
|
fieldDepartment: cf.fieldDepartment,
|
||||||
fieldEmail: cf.fieldEmail,
|
fieldEmail: cf.fieldEmail,
|
||||||
fieldMessage: cf.fieldMessage,
|
fieldMessage: cf.fieldMessage,
|
||||||
};
|
};
|
||||||
@ -161,6 +162,20 @@ const fieldClass =
|
|||||||
class={fieldClass}
|
class={fieldClass}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
<div>
|
||||||
|
<label class="block text-xs font-medium text-nx-muted" for="nx-cf-department">
|
||||||
|
{cf.departmentLabel}
|
||||||
|
</label>
|
||||||
|
<select
|
||||||
|
id="nx-cf-department"
|
||||||
|
name={cf.fieldDepartment}
|
||||||
|
class={fieldClass}
|
||||||
|
>
|
||||||
|
{cf.departmentOptions.map((opt) => (
|
||||||
|
<option value={opt.value}>{opt.label}</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label class="block text-xs font-medium text-nx-muted" for="nx-cf-msg">
|
<label class="block text-xs font-medium text-nx-muted" for="nx-cf-msg">
|
||||||
{cf.messageLabel}
|
{cf.messageLabel}
|
||||||
|
|||||||
@ -1,4 +1,10 @@
|
|||||||
/** Call-to-action (blocco contatti / chiusura pagina) */
|
/** Call-to-action (blocco contatti / chiusura pagina) */
|
||||||
|
const env = import.meta.env;
|
||||||
|
const contactActionUrl = (env.PUBLIC_CONTACT_FORM_ACTION_URL ?? '/api/contact').trim();
|
||||||
|
const contactSuccessRedirect = (env.PUBLIC_CONTACT_FORM_SUCCESS_REDIRECT ?? '').trim();
|
||||||
|
const contactMailto = (env.PUBLIC_CONTACT_FORM_MAILTO ?? '').trim();
|
||||||
|
const contactTurnstileSiteKey = (env.PUBLIC_CONTACT_TURNSTILE_SITE_KEY ?? '').trim();
|
||||||
|
|
||||||
export const cta = {
|
export const cta = {
|
||||||
/** Riga sezione (stesso ruolo di FAQ, Stack, …) */
|
/** Riga sezione (stesso ruolo di FAQ, Stack, …) */
|
||||||
eyebrow: 'Contatti',
|
eyebrow: 'Contatti',
|
||||||
@ -24,26 +30,37 @@ export const cta = {
|
|||||||
nameLabel: 'Nome',
|
nameLabel: 'Nome',
|
||||||
companyLabel: 'Azienda (opzionale)',
|
companyLabel: 'Azienda (opzionale)',
|
||||||
countryLabel: 'Paese',
|
countryLabel: 'Paese',
|
||||||
|
departmentLabel: 'Reparto',
|
||||||
emailLabel: 'Email',
|
emailLabel: 'Email',
|
||||||
messageLabel: 'Scrivi qui il tuo messaggio',
|
messageLabel: 'Scrivi qui il tuo messaggio',
|
||||||
submitLabel: 'Invia messaggio',
|
submitLabel: 'Invia messaggio',
|
||||||
/** Formspree: `https://formspree.io/f/xxxx` — vuoto = prova `mailto` */
|
/** Formspree: `https://formspree.io/f/xxxx` — valorizzato via `PUBLIC_CONTACT_FORM_ACTION_URL` */
|
||||||
actionUrl: '',
|
actionUrl: contactActionUrl,
|
||||||
/** Formspree: redirect dopo invio (`_next`) — URL **assoluta** in produzione */
|
/** Formspree `_next` — URL assoluta in produzione (`PUBLIC_CONTACT_FORM_SUCCESS_REDIRECT`) */
|
||||||
successRedirect: '',
|
successRedirect: contactSuccessRedirect,
|
||||||
/**
|
/**
|
||||||
* Cloudflare Turnstile (anti-spam): inserisci qui la site key pubblica.
|
* Cloudflare Turnstile (anti-spam): inserisci qui la site key pubblica.
|
||||||
* Se vuota, il CAPTCHA non viene mostrato.
|
* Se vuota, il CAPTCHA non viene mostrato.
|
||||||
*/
|
*/
|
||||||
turnstileSiteKey: '',
|
turnstileSiteKey: contactTurnstileSiteKey,
|
||||||
/** Formspree `_subject` e oggetto mailto */
|
/** Formspree `_subject` e oggetto mailto */
|
||||||
notifySubject: 'Richiesta contatto — sito NexStudio',
|
notifySubject: 'Richiesta contatto — sito NexStudio',
|
||||||
/** Se `actionUrl` è vuoto: email per `mailto:` (es. `info@tuodominio.com`) */
|
/** Fallback se `actionUrl` e' vuoto (`PUBLIC_CONTACT_FORM_MAILTO`) */
|
||||||
mailto: '',
|
mailto: contactMailto,
|
||||||
fieldName: 'name',
|
fieldName: 'name',
|
||||||
fieldCompany: 'company',
|
fieldCompany: 'company',
|
||||||
fieldCountry: 'country',
|
fieldCountry: 'country',
|
||||||
|
fieldDepartment: 'department',
|
||||||
fieldEmail: 'email',
|
fieldEmail: 'email',
|
||||||
fieldMessage: 'message',
|
fieldMessage: 'message',
|
||||||
|
departmentOptions: [
|
||||||
|
{ value: 'general', label: 'Generale' },
|
||||||
|
{ value: 'support', label: 'Support' },
|
||||||
|
{ value: 'privacy', label: 'Privacy / DPO' },
|
||||||
|
{ value: 'security', label: 'Security / Incident Response' },
|
||||||
|
{ value: 'ethics', label: 'Segnalazioni confidenziali (Ethics)' },
|
||||||
|
{ value: 'legal', label: 'Legal' },
|
||||||
|
{ value: 'hr', label: 'HR' },
|
||||||
|
],
|
||||||
},
|
},
|
||||||
} as const;
|
} as const;
|
||||||
|
|||||||
166
src/pages/api/contact.ts
Normal file
166
src/pages/api/contact.ts
Normal file
@ -0,0 +1,166 @@
|
|||||||
|
import type { APIRoute } from 'astro';
|
||||||
|
|
||||||
|
export const prerender = false;
|
||||||
|
|
||||||
|
type RuntimeEnv = Record<string, unknown>;
|
||||||
|
|
||||||
|
function getRuntimeEnv(locals: unknown): RuntimeEnv {
|
||||||
|
if (
|
||||||
|
locals &&
|
||||||
|
typeof locals === 'object' &&
|
||||||
|
'runtime' in locals &&
|
||||||
|
locals.runtime &&
|
||||||
|
typeof locals.runtime === 'object' &&
|
||||||
|
'env' in locals.runtime &&
|
||||||
|
locals.runtime.env &&
|
||||||
|
typeof locals.runtime.env === 'object'
|
||||||
|
) {
|
||||||
|
return locals.runtime.env as RuntimeEnv;
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
function readEnv(name: string, runtimeEnv: RuntimeEnv): string {
|
||||||
|
const runtimeValue = runtimeEnv[name];
|
||||||
|
if (typeof runtimeValue === 'string' && runtimeValue.trim().length > 0) {
|
||||||
|
return runtimeValue.trim();
|
||||||
|
}
|
||||||
|
const staticValue = import.meta.env[name];
|
||||||
|
return typeof staticValue === 'string' ? staticValue.trim() : '';
|
||||||
|
}
|
||||||
|
|
||||||
|
function sanitizeRedirect(candidate: string, requestUrl: URL): string {
|
||||||
|
if (!candidate) return '';
|
||||||
|
try {
|
||||||
|
const u = new URL(candidate, requestUrl);
|
||||||
|
return u.toString();
|
||||||
|
} catch {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function required(value: FormDataEntryValue | null): string {
|
||||||
|
return typeof value === 'string' ? value.trim() : '';
|
||||||
|
}
|
||||||
|
|
||||||
|
async function verifyTurnstile(token: string, secret: string, ip?: string | null): Promise<boolean> {
|
||||||
|
const body = new URLSearchParams();
|
||||||
|
body.set('secret', secret);
|
||||||
|
body.set('response', token);
|
||||||
|
if (ip) body.set('remoteip', ip);
|
||||||
|
|
||||||
|
const response = await fetch('https://challenges.cloudflare.com/turnstile/v0/siteverify', {
|
||||||
|
method: 'POST',
|
||||||
|
body,
|
||||||
|
});
|
||||||
|
if (!response.ok) return false;
|
||||||
|
const payload = (await response.json()) as { success?: boolean };
|
||||||
|
return payload.success === true;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function sendWithResend(apiKey: string, fromEmail: string, toEmail: string, subject: string, text: string) {
|
||||||
|
const response = await fetch('https://api.resend.com/emails', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${apiKey}`,
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
from: fromEmail,
|
||||||
|
to: [toEmail],
|
||||||
|
subject,
|
||||||
|
text,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const detail = await response.text();
|
||||||
|
throw new Error(`Resend error ${response.status}: ${detail}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolveDepartmentRecipient(runtimeEnv: RuntimeEnv, defaultRecipient: string, department: string): string {
|
||||||
|
const normalized = department.trim().toLowerCase();
|
||||||
|
const map: Record<string, string> = {
|
||||||
|
support: readEnv('CONTACT_FORM_TO_EMAIL_SUPPORT', runtimeEnv),
|
||||||
|
privacy: readEnv('CONTACT_FORM_TO_EMAIL_PRIVACY', runtimeEnv),
|
||||||
|
security: readEnv('CONTACT_FORM_TO_EMAIL_SECURITY', runtimeEnv),
|
||||||
|
ethics: readEnv('CONTACT_FORM_TO_EMAIL_ETHICS', runtimeEnv),
|
||||||
|
legal: readEnv('CONTACT_FORM_TO_EMAIL_LEGAL', runtimeEnv),
|
||||||
|
hr: readEnv('CONTACT_FORM_TO_EMAIL_HR', runtimeEnv),
|
||||||
|
};
|
||||||
|
return map[normalized] || defaultRecipient;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const POST: APIRoute = async ({ request, locals, url }) => {
|
||||||
|
const runtimeEnv = getRuntimeEnv(locals);
|
||||||
|
const mode = readEnv('CONTACT_FORM_MODE', runtimeEnv).toLowerCase() || 'dev';
|
||||||
|
const resendApiKey = readEnv('CONTACT_FORM_RESEND_API_KEY', runtimeEnv);
|
||||||
|
const fromEmail = readEnv('CONTACT_FORM_FROM_EMAIL', runtimeEnv);
|
||||||
|
const toEmail = readEnv('CONTACT_FORM_TO_EMAIL', runtimeEnv);
|
||||||
|
const turnstileSecret = readEnv('CONTACT_TURNSTILE_SECRET_KEY', runtimeEnv);
|
||||||
|
|
||||||
|
const form = await request.formData();
|
||||||
|
const redirectTarget =
|
||||||
|
sanitizeRedirect(required(form.get('_next')), url) ||
|
||||||
|
sanitizeRedirect(readEnv('PUBLIC_CONTACT_FORM_SUCCESS_REDIRECT', runtimeEnv), url) ||
|
||||||
|
sanitizeRedirect(request.headers.get('referer') ?? '/', url) ||
|
||||||
|
'/';
|
||||||
|
|
||||||
|
const name = required(form.get('name'));
|
||||||
|
const company = required(form.get('company'));
|
||||||
|
const email = required(form.get('email'));
|
||||||
|
const country = required(form.get('country'));
|
||||||
|
const department = required(form.get('department')) || 'general';
|
||||||
|
const message = required(form.get('message'));
|
||||||
|
|
||||||
|
if (!name || !email || !country || !message) {
|
||||||
|
return new Response('Missing required contact fields.', { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const turnstileToken = required(form.get('cf-turnstile-response'));
|
||||||
|
if (turnstileSecret) {
|
||||||
|
const isHuman = await verifyTurnstile(
|
||||||
|
turnstileToken,
|
||||||
|
turnstileSecret,
|
||||||
|
request.headers.get('CF-Connecting-IP'),
|
||||||
|
);
|
||||||
|
if (!isHuman) {
|
||||||
|
return new Response('Turnstile verification failed.', { status: 400 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const subject = required(form.get('_subject')) || 'Richiesta contatto - sito NexStudio';
|
||||||
|
const text = [
|
||||||
|
`Nome: ${name}`,
|
||||||
|
company ? `Azienda: ${company}` : '',
|
||||||
|
`Email: ${email}`,
|
||||||
|
`Paese: ${country}`,
|
||||||
|
`Reparto: ${department}`,
|
||||||
|
'',
|
||||||
|
message,
|
||||||
|
]
|
||||||
|
.filter(Boolean)
|
||||||
|
.join('\n');
|
||||||
|
|
||||||
|
if (mode === 'live') {
|
||||||
|
if (!resendApiKey || !fromEmail || !toEmail) {
|
||||||
|
return new Response('Live mode requires CONTACT_FORM_RESEND_API_KEY, CONTACT_FORM_FROM_EMAIL and CONTACT_FORM_TO_EMAIL.', {
|
||||||
|
status: 500,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const destination = resolveDepartmentRecipient(runtimeEnv, toEmail, department);
|
||||||
|
await sendWithResend(resendApiKey, fromEmail, destination, subject, text);
|
||||||
|
} else {
|
||||||
|
console.info('[contact:dev] Contact payload received', {
|
||||||
|
name,
|
||||||
|
company,
|
||||||
|
email,
|
||||||
|
country,
|
||||||
|
department,
|
||||||
|
subject,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return Response.redirect(redirectTarget, 303);
|
||||||
|
};
|
||||||
@ -8,6 +8,7 @@ interface ContactFormCfg {
|
|||||||
fieldName: string;
|
fieldName: string;
|
||||||
fieldCompany: string;
|
fieldCompany: string;
|
||||||
fieldCountry: string;
|
fieldCountry: string;
|
||||||
|
fieldDepartment: string;
|
||||||
fieldEmail: string;
|
fieldEmail: string;
|
||||||
fieldMessage: string;
|
fieldMessage: string;
|
||||||
}
|
}
|
||||||
@ -25,6 +26,7 @@ function readCfg(): ContactFormCfg {
|
|||||||
fieldName: 'name',
|
fieldName: 'name',
|
||||||
fieldCompany: 'company',
|
fieldCompany: 'company',
|
||||||
fieldCountry: 'country',
|
fieldCountry: 'country',
|
||||||
|
fieldDepartment: 'department',
|
||||||
fieldEmail: 'email',
|
fieldEmail: 'email',
|
||||||
fieldMessage: 'message',
|
fieldMessage: 'message',
|
||||||
};
|
};
|
||||||
@ -67,12 +69,13 @@ function bind(): void {
|
|||||||
const company = String(fd.get(cfg.fieldCompany) ?? '').trim();
|
const company = String(fd.get(cfg.fieldCompany) ?? '').trim();
|
||||||
const email = String(fd.get(cfg.fieldEmail) ?? '').trim();
|
const email = String(fd.get(cfg.fieldEmail) ?? '').trim();
|
||||||
const country = String(fd.get(cfg.fieldCountry) ?? '').trim();
|
const country = String(fd.get(cfg.fieldCountry) ?? '').trim();
|
||||||
|
const department = String(fd.get(cfg.fieldDepartment) ?? '').trim();
|
||||||
const message = String(fd.get(cfg.fieldMessage) ?? '').trim();
|
const message = String(fd.get(cfg.fieldMessage) ?? '').trim();
|
||||||
if (cfg.mailto) {
|
if (cfg.mailto) {
|
||||||
const subject = encodeURIComponent(cfg.notifySubject || 'Contatto sito');
|
const subject = encodeURIComponent(cfg.notifySubject || 'Contatto sito');
|
||||||
const companyLine = company ? `\nAzienda: ${company}` : '';
|
const companyLine = company ? `\nAzienda: ${company}` : '';
|
||||||
const body = encodeURIComponent(
|
const body = encodeURIComponent(
|
||||||
`Nome: ${name}${companyLine}\nEmail: ${email}\nPaese: ${country}\n\n${message}`,
|
`Nome: ${name}${companyLine}\nEmail: ${email}\nPaese: ${country}\nReparto: ${department || 'general'}\n\n${message}`,
|
||||||
);
|
);
|
||||||
window.location.href = `mailto:${cfg.mailto}?subject=${subject}&body=${body}`;
|
window.location.href = `mailto:${cfg.mailto}?subject=${subject}&body=${body}`;
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user