Spaces:
Runtime error
Runtime error
File size: 9,435 Bytes
83508c1 0396645 83508c1 9ce74cb 0396645 83508c1 0396645 83508c1 0396645 83508c1 0396645 6b807d8 0396645 83508c1 ad8f1ad db9c797 ad8f1ad 83508c1 0396645 83508c1 c220170 7b236ed c220170 4c632a6 c220170 7b236ed db9c797 7b236ed db9c797 7b236ed db9c797 7b236ed db9c797 0396645 83508c1 0396645 db9c797 | 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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | const express = require('express');
const { exec } = require('child_process');
const fs = require('fs');
const path = require('path');
const { v4: uuidv4 } = require('uuid');
const app = express();
const PORT = 7860;
// CORS middleware
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === 'OPTIONS') {
return res.sendStatus(200);
}
next();
});
// JSON body parser
app.use(express.json());
// Job storage
const jobs = {};
// Web arayüzü
app.get('/', (req, res) => {
res.send(`
<html>
<head>
<title>APK Build Service</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
button { padding: 10px 20px; font-size: 16px; cursor: pointer; }
#status { margin-top: 20px; padding: 10px; border: 1px solid #ccc; }
</style>
</head>
<body>
<h1>APK Build Service API</h1>
<p>POST /build - Build başlat (appName, packageId, repoUrl)</p>
<p>GET /status/:jobId - Durum sorgula</p>
<p>GET /download/:jobId - APK indir</p>
</body>
</html>
`);
});
// Build endpoint
app.post('/build', (req, res) => {
const { appName, packageId, repoUrl } = req.body;
if (!appName || !packageId || !repoUrl) {
return res.status(400).json({ success: false, message: 'appName, packageId ve repoUrl gerekli' });
}
const jobId = uuidv4();
jobs[jobId] = {
status: 'pending',
appName,
packageId,
repoUrl,
createdAt: new Date(),
apkPath: null
};
// Build işlemini arka planda başlat
buildAPK(jobId);
res.json({ success: true, jobId, message: 'Build başlatıldı' });
});
// Durum sorgulama
app.get('/status/:jobId', (req, res) => {
const { jobId } = req.params;
const job = jobs[jobId];
if (!job) {
return res.status(404).json({ success: false, message: 'Job bulunamadı' });
}
res.json({
success: true,
jobId,
status: job.status,
appName: job.appName,
createdAt: job.createdAt
});
});
// APK indirme
app.get('/download/:jobId', (req, res) => {
const { jobId } = req.params;
const job = jobs[jobId];
if (!job) {
return res.status(404).json({ success: false, message: 'Job bulunamadı' });
}
if (job.status !== 'completed') {
return res.status(400).json({ success: false, message: 'APK henüz hazır değil' });
}
if (job.apkPath && fs.existsSync(job.apkPath)) {
res.download(job.apkPath, `${job.appName}-debug.apk`);
} else {
res.status(404).json({ success: false, message: 'APK dosyası bulunamadı' });
}
});
// Tüm job'ları listele
app.get('/jobs', (req, res) => {
const jobList = Object.entries(jobs).map(([jobId, job]) => ({
jobId,
status: job.status,
appName: job.appName,
packageId: job.packageId,
repoUrl: job.repoUrl,
createdAt: job.createdAt,
error: job.error
}));
// Tarihe göre sırala (en yeni önce)
jobList.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
res.json({ success: true, jobs: jobList });
});
// Build fonksiyonu
function buildAPK(jobId) {
const job = jobs[jobId];
job.status = 'building';
const buildDir = `/tmp/build-${jobId}`;
// Temizle
if (fs.existsSync(buildDir)) {
fs.rmSync(buildDir, { recursive: true });
}
// Klonla (with token if available)
const token = process.env.GITHUB_TOKEN;
let cloneUrl = job.repoUrl;
if (token && job.repoUrl.startsWith('https://github.com/')) {
cloneUrl = job.repoUrl.replace('https://github.com/', `https://git:${token}@github.com/`);
}
exec(`git clone ${cloneUrl} ${buildDir}`, (error, stdout, stderr) => {
if (error) {
console.error('Klonlama hatası:', error);
job.status = 'failed';
job.error = error.message;
return;
}
// Build işlemi (step by step) - WebView Android App
const steps = [
'npm install',
// Android projesi oluştur
`mkdir -p android-project/app/src/main/java/com/example/webview`,
`mkdir -p android-project/app/src/main/res/values`,
`mkdir -p android-project/app/src/main/res/layout`,
// AndroidManifest.xml oluştur
`cat > android-project/app/src/main/AndroidManifest.xml << 'EOF'
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.webview">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="${job.appName}"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
EOF`,
// MainActivity.java oluştur
`cat > android-project/app/src/main/java/com/example/webview/MainActivity.java << 'EOF'
package com.example.webview;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.loadUrl("file:///android_asset/index.html");
}
}
EOF`,
// activity_main.xml oluştur
`cat > android-project/app/src/main/res/layout/activity_main.xml << 'EOF'
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
EOF`,
// strings.xml oluştur
`cat > android-project/app/src/main/res/values/strings.xml << 'EOF'
<resources>
<string name="app_name">${job.appName}</string>
</resources>
EOF`,
// build.gradle oluştur
`cat > android-project/app/build.gradle << 'EOF'
plugins {
id 'com.android.application'
}
android {
namespace 'com.example.webview'
compileSdk 34
defaultConfig {
applicationId "com.example.webview"
minSdk 21
targetSdk 34
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
}
EOF`,
// settings.gradle oluştur
`cat > android-project/settings.gradle << 'EOF'
pluginManagement {
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "${job.appName}"
include ':app'
EOF`,
// Web dosyalarını assets klasörüne kopyala
`mkdir -p android-project/app/src/main/assets`,
`cp -r index.html css js android-project/app/src/main/assets/ 2>/dev/null || cp -r * android-project/app/src/main/assets/`,
// Gradle wrapper oluştur
`cd android-project && gradle wrapper`,
// APK derle
`cd android-project && ./gradlew assembleDebug`
];
let currentStep = 0;
const runStep = () => {
if (currentStep >= steps.length) {
// Tüm adımlar başarılı
const apkPath = path.join(buildDir, 'android/app/build/outputs/apk/debug/app-debug.apk');
const destPath = `/tmp/${job.appName}-${jobId}-debug.apk`;
if (fs.existsSync(apkPath)) {
fs.copyFileSync(apkPath, destPath);
console.log('APK oluşturuldu:', destPath);
job.status = 'completed';
job.apkPath = destPath;
} else {
job.status = 'failed';
job.error = 'APK dosyası bulunamadı';
}
return;
}
const step = steps[currentStep];
console.log(`Step ${currentStep + 1}: ${step}`);
exec(step, { cwd: buildDir, timeout: 600000 }, (err, stdout, stderr) => {
if (err) {
console.error(`Step ${currentStep + 1} failed:`, err.message);
job.status = 'failed';
job.error = `Step ${currentStep + 1} failed: ${err.message}`;
return;
}
if (stdout) console.log(stdout);
if (stderr) console.log(stderr);
currentStep++;
runStep();
});
};
runStep();
});
}
app.listen(PORT, () => {
console.log(`APK Build Service ${PORT} portunda çalışıyor`);
});
|