EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
ensure_demo_a_record.js
Go to the documentation of this file.
1// Script to ensure demo.everydaytech.au has an A record pointing to your droplet IP
2require('dotenv').config();
3const axios = require('axios');
4
5const CLOUDFLARE_BASE_URL = process.env.CLOUDFLARE_BASE_URL || 'https://api.cloudflare.com/client/v4';
6const CLOUDFLARE_ZONE_ID = process.env.CLOUDFLARE_ZONE_ID;
7const CLOUDFLARE_TOKEN = process.env.CLOUDFLARE_TOKEN;
8const ZONE_NAME = process.env.ZONE_NAME || 'everydaytech.au';
9const DROPLET_IP = '209.38.80.86';
10
11/**
12 *
13 */
14async function ensureARecord() {
15 if (!CLOUDFLARE_ZONE_ID || !CLOUDFLARE_TOKEN) {
16 throw new Error('Cloudflare credentials not configured');
17 }
18
19 // Check for existing A record
20 const dnsName = `demo.${ZONE_NAME}`;
21 const listUrl = `${CLOUDFLARE_BASE_URL}/zones/${CLOUDFLARE_ZONE_ID}/dns_records?type=A&name=${dnsName}`;
22 const headers = {
23 Authorization: `Bearer ${CLOUDFLARE_TOKEN}`,
24 'Content-Type': 'application/json'
25 };
26
27 const res = await axios.get(listUrl, { headers });
28 const existing = res.data.result && res.data.result.length > 0;
29
30 if (existing) {
31 console.log(`A record for ${dnsName} already exists.`);
32 return;
33 }
34
35 // Create A record
36 const createUrl = `${CLOUDFLARE_BASE_URL}/zones/${CLOUDFLARE_ZONE_ID}/dns_records`;
37 const payload = {
38 type: 'A',
39 name: dnsName,
40 content: DROPLET_IP,
41 ttl: 120,
42 proxied: true
43 };
44
45 const createRes = await axios.post(createUrl, payload, { headers });
46 if (createRes.data.success) {
47 console.log(`✅ Created A record for ${dnsName} → ${DROPLET_IP}`);
48 } else {
49 console.error('❌ Failed to create A record:', createRes.data.errors);
50 }
51}
52
53ensureARecord().catch(err => {
54 console.error('Error:', err.message);
55 process.exit(1);
56});