Skip to content

Commit

Permalink
Now the Square Keep Alive saves the application log
Browse files Browse the repository at this point in the history
  • Loading branch information
josejooj committed Oct 21, 2023
1 parent 94a394b commit 88d32fa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
14 changes: 11 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
.env
package-lock.json

# folders

node_modules
dist
*.zip
logs

# files

.env
package-lock.json
*.zip
*.log
12 changes: 9 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import { Status } from "./typing";
import { config as ConfigEnv } from 'dotenv';
import axios from "axios";
import fs from 'fs';

ConfigEnv()

const log = (tag: string, msg: string) => console.log(`[${tag}] - ${msg}`)
const square = axios.create({ baseURL: "https://api.squarecloud.app/v2", headers: { Authorization: process.env.SQUARE_API_KEY } });

square.interceptors.response.use(res => res, error => error.response);

(async () => {

const { data, status } = await square.get(`/user`).catch(e => e.response);
const user = data.response?.user as { tag: string, plan: { name: string } };

if (status === 401) return log('ERROR', "Please, verify your API Key!")
if (user.plan.name === 'free') log('ALERT', `Don't host me on Square Cloud with free plan.`) // maybe you are hosting on another machine
if (!fs.existsSync("./logs")) fs.mkdirSync("./logs")

log('INFO', `Started successfully! Hello ${user.tag}!`)
log('INFO', `I will check all applications each 15 seconds`);
Expand All @@ -23,7 +27,7 @@ const square = axios.create({ baseURL: "https://api.squarecloud.app/v2", headers

try {

const { data, status } = await square.get("/apps/all/status").catch(e => e.response);
const { data, status } = await square.get("/apps/all/status");
const response = data.response as Status[];

if (status === 401) return log('ERROR', `Please, re-configure your api key.`)
Expand All @@ -32,8 +36,10 @@ const square = axios.create({ baseURL: "https://api.squarecloud.app/v2", headers

if (app.running) continue;

const { status } = await square.post(`/apps/${app.id}/start`).catch(e => e.response);

const { data } = await square.get(`/apps/${app.id}/logs`);
const { status } = await square.post(`/apps/${app.id}/start`);

if (data.status === 'success') fs.writeFileSync(`./logs/[${new Date().getTime()}] - ${app.id}.log`, data.response.logs)
if (status === 200) log('INFO', `[CHECK ${i + 1}] - The application "${app.id}" was offline, and i restarted it successfully!`);
else log('WARNING', `[CHECK ${i + 1}] - I received the HTTP ERROR ${status} when i tried to restart the application "${app.id}" (ID ${app.id})`)

Expand Down
7 changes: 1 addition & 6 deletions src/typing.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
type Status = {
id: string,
cpu: string,
ram: string,
running: boolean
}


export {
Status
Expand Down

0 comments on commit 88d32fa

Please sign in to comment.