-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-elixir.sh
More file actions
executable file
ยท217 lines (183 loc) ยท 5.34 KB
/
build-elixir.sh
File metadata and controls
executable file
ยท217 lines (183 loc) ยท 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/bin/bash
# Build script for Elixir/Phoenix components for Vercel deployment
set -e
echo "๐ฎ Building Elixir/Phoenix components for Vercel..."
# Check if we're in the right directory
if [ ! -f "server/mix.exs" ]; then
echo "โ Error: server/mix.exs not found. Please run from core directory."
exit 1
fi
# Check Elixir installation
if ! command -v elixir &> /dev/null; then
echo "โ Error: Elixir not found. Please install Elixir."
exit 1
fi
# Create api/elixir directory structure
mkdir -p api/elixir
# Copy Phoenix application files
echo "๐ Copying Phoenix application..."
cp -r server/lib api/elixir/
cp -r server/config api/elixir/
cp -r server/priv api/elixir/
cp server/mix.exs api/elixir/
cp server/mix.lock api/elixir/
# Create production configuration
echo "โ๏ธ Creating production configuration..."
cat > api/elixir/config/prod.exs << 'EOF'
import Config
# Database configuration
config :katalyst, Katalyst.Repo,
url: System.get_env("DATABASE_URL"),
pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"),
ssl: true
# Endpoint configuration
config :katalyst, KatalystWeb.Endpoint,
url: [host: System.get_env("HOST"), port: 80],
cache_static_manifest: "priv/static/cache_manifest.json",
server: false,
secret_key_base: System.get_env("SECRET_KEY_BASE")
# Runtime configuration
config :katalyst, KatalystWeb.Endpoint,
server: true
# Logger configuration
config :logger, level: :info
# Phoenix configuration
config :phoenix, :json_library, Jason
EOF
# Create release configuration
echo "๐ฆ Creating release configuration..."
cat > api/elixir/config/runtime.exs << 'EOF'
import Config
# Runtime configuration
if config_env() == :prod do
database_url =
System.get_env("DATABASE_URL") ||
raise """
environment variable DATABASE_URL is missing.
For example: ecto://USER:PASS@HOST/DATABASE
"""
config :katalyst, Katalyst.Repo,
# ssl: false,
url: database_url,
pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10")
secret_key_base =
System.get_env("SECRET_KEY_BASE") ||
raise """
environment variable SECRET_KEY_BASE is missing.
You can generate one by calling: mix phx.gen.secret
"""
config :katalyst, KatalystWeb.Endpoint,
url: [host: System.get_env("HOST"), port: 80],
cache_static_manifest: "priv/static/cache_manifest.json",
server: true,
secret_key_base: secret_key_base
config :katalyst, :dns_cluster_query, System.get_env("DNS_CLUSTER_QUERY")
end
EOF
# Create Vercel serverless function handler
echo "๐ Creating Vercel serverless function handler..."
cat > api/elixir/index.js << 'EOF'
const { spawn } = require('child_process');
const path = require('path');
module.exports = async (req, res) => {
// Start Elixir application
const elixirProcess = spawn('mix', ['phx.server'], {
cwd: __dirname,
stdio: ['pipe', 'pipe', 'pipe'],
env: {
...process.env,
MIX_ENV: 'prod',
PORT: '8080',
HOST: 'localhost'
}
});
// Wait for server to start
await new Promise((resolve) => {
elixirProcess.stdout.on('data', (data) => {
if (data.toString().includes('Running KatalystWeb.Endpoint')) {
resolve();
}
});
});
// Proxy request to Elixir application
const http = require('http');
const options = {
hostname: 'localhost',
port: 8080,
path: req.url,
method: req.method,
headers: req.headers
};
const proxyReq = http.request(options, (proxyRes) => {
res.writeHead(proxyRes.statusCode, proxyRes.headers);
proxyRes.pipe(res);
});
proxyReq.on('error', (error) => {
res.status(500).json({ error: error.message });
});
req.pipe(proxyReq);
};
EOF
# Create build script for Elixir
echo "๐จ Creating Elixir build script..."
cat > api/elixir/build.sh << 'EOF'
#!/bin/bash
set -e
echo "๐ฆ Installing Elixir dependencies..."
mix deps.get --only prod
echo "๐จ Compiling application..."
MIX_ENV=prod mix compile
echo "๐ฆ Creating release..."
MIX_ENV=prod mix release
echo "โ
Elixir build complete!"
EOF
chmod +x api/elixir/build.sh
# Create package.json for build scripts
echo "๐ Creating package.json..."
cat > api/elixir/package.json << 'EOF'
{
"name": "@katalyst/elixir-api",
"version": "0.1.0",
"description": "Elixir Phoenix API for Katalyst on Vercel",
"scripts": {
"build": "./build.sh",
"start": "mix phx.server"
},
"engines": {
"elixir": ">=1.15.0",
"otp": ">=26.0"
}
}
EOF
# Create Vercel configuration for Elixir
echo "โ๏ธ Creating Vercel configuration for Elixir..."
cat > api/elixir/vercel.json << 'EOF'
{
"runtime": "elixir",
"buildCommand": "./build.sh",
"outputDirectory": "_build/prod/rel/katalyst",
"installCommand": "mix deps.get --only prod",
"devCommand": "mix phx.server",
"maxDuration": 30,
"memory": 1024,
"includeFiles": ["lib/**", "config/**", "priv/**", "mix.*"],
"excludeFiles": ["_build/**", "deps/**", ".elixir_ls/**"],
"environment": {
"MIX_ENV": "prod",
"HOST": "localhost",
"PORT": "8080"
}
}
EOF
echo "โ
Elixir build setup complete!"
echo "๐ Built files are in api/elixir/"
echo ""
echo "๐ To use in Vercel:"
echo " Phoenix API will be available at /api/elixir/"
echo " Build with: ./build.sh"
echo ""
echo "๐ Required environment variables:"
echo " - DATABASE_URL"
echo " - SECRET_KEY_BASE"
echo " - HOST"
echo " - POOL_SIZE"