Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Deploy to AWS

on:
push:
branches: [ main ]

jobs:
aws-deployment:
name: Deploy
environment:
name: auth-server
url: https://github.com
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-2
- name: Create CodeDeploy Deployment
id: deploy
run: |
aws deploy create-deployment --ignore-application-stop-failures \
--application-name auth-server \
--deployment-group-name auth-server \
--deployment-config-name CodeDeployDefault.AllAtOnce \
--file-exists-behavior OVERWRITE \
--github-location repository=${{ github.repository }},commitId=${{ github.sha }}
14 changes: 14 additions & 0 deletions appspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: 0.0
os: linux
files:
- source: ./gitlangRoutes
destination: /home/ec2-user/auth-server
hooks:
ApplicationStop:
- location: stop.sh
timeout: 300
runas: root
ApplicationStart:
- location: init.sh
timeout: 300
runas: root
3 changes: 3 additions & 0 deletions client/app.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { createReadStream } from 'node:fs';

import * as dotenv from 'dotenv';
import Koa from 'koa';
import serve from 'koa-static';

dotenv.config();

const app = new Koa();
const port = 3000;
const redirect = 'docs';
Expand Down
14 changes: 7 additions & 7 deletions client/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import getSize from './helpers/size';
import languages from './requests/languages';
import repos from './requests/repos';
import repositories from './requests/repositories';

const langs = async (inputOwner: string) => {
const langs = async (username: string) => {
try {
window.history.pushState('', '', `/${inputOwner}`);
let owner = inputOwner;
window.history.pushState('', '', `/${username}`);
let owner = username;
let allNames: string[];
if (inputOwner.includes('/')) {
const [splitOwner, splitRepo] = inputOwner.split('/');
if (username.includes('/')) {
const [splitOwner, splitRepo] = username.split('/');
owner = splitOwner;
allNames = [splitRepo];
} else {
allNames = await repos(inputOwner);
allNames = await repositories(username);
}
const allLanguages = await languages(owner, allNames);
const space = getSize(allLanguages.flat());
Expand Down
26 changes: 24 additions & 2 deletions client/routes/requests/languages.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
import axios from 'axios';

const languages = async (owner: string, repos: string[]) => {
import router from '../../../local';

const localToken = process.env.GH_PAT;

const localApi = async (owner: string, repos: string[]) => {
try {
const allLanguages = JSON.parse(
await router.langs(owner, repos, localToken),
);

if (allLanguages && allLanguages.length > 0) {
return allLanguages;
}
return [];
} catch (error) {
console.error('Error getting token from auth api', error);
return [];
}
};

const serverApi = async (owner: string, repos: string[]) => {
try {
const allLanguages: { data: { [key: string]: number }[] } = await axios.get(
'https://api.5105015032.com/auth/gitlang/langs',
'https://api.5105015032.com/gitlang/github/langs',
{
params: {
owner,
Expand All @@ -20,4 +40,6 @@ const languages = async (owner: string, repos: string[]) => {
}
};

const languages = localToken ? localApi : serverApi;

export default languages;
21 changes: 0 additions & 21 deletions client/routes/requests/repos.ts

This file was deleted.

40 changes: 40 additions & 0 deletions client/routes/requests/repositories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import axios from 'axios';

import router from '../../../local';

const localToken = process.env.GH_PAT;

const localApi = async (username: string) => {
try {
const allRepos = JSON.parse(await router.repos(username, localToken));
if (allRepos && allRepos.length > 0) {
return allRepos;
}
return [];
} catch (error) {
console.error('Error getting token from auth api', error);
return [];
}
};

const serverApi = async (username: string) => {
try {
const allRepos: { data: [] } = await axios.get(
'https://api.5105015032.com/gitlang/github/repos',
{
params: { username },
},
);
if (allRepos && allRepos.data && allRepos.data.length > 0) {
return allRepos.data;
}
return [];
} catch (error) {
console.error('Error getting token from auth api', error);
return [];
}
};

const repositories = localToken ? localApi : serverApi;

export default repositories;
9 changes: 9 additions & 0 deletions gitlangRoutes/gitlang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Router from '@koa/router';

import gitlangRouter from './requests/gitlangRouter';

const router = new Router({ prefix: '/gitlang' });

router.use(gitlangRouter.routes(), gitlangRouter.allowedMethods());

export default router;
36 changes: 36 additions & 0 deletions gitlangRoutes/helpers/github/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
SecretsManagerClient,
GetSecretValueCommand,
} from '@aws-sdk/client-secrets-manager';

const region = 'us-east-2';
const secretName = 'gitlang-auth';

const auth: () => Promise<string> = async () => {
try {
let secret = '';

const client = new SecretsManagerClient({
region,
});

const data = await client.send(
new GetSecretValueCommand({ SecretId: secretName }),
);

if (data && data.SecretString) {
secret = data.SecretString;
}

const parser: { parse: (argument: string) => { [secretName]: string } } =
JSON;

const parseSecret = parser.parse(secret);
return parseSecret[secretName];
} catch (error) {
console.error(error);
return '';
}
};

export default auth;
31 changes: 31 additions & 0 deletions gitlangRoutes/helpers/github/languages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Octokit } from '@octokit/rest';

let octokit: Octokit;

const fetchLanguage = async (owner: string, repo: string, token?: string) => {
try {
if (!octokit) {
if (!token) {
console.error('No token');
return [];
}
octokit = new Octokit({ auth: token });
}
return await octokit.paginate(octokit.rest.repos.listLanguages, {
owner,
repo,
});
} catch {
return [];
}
};

const languages = async (owner: string, names: string[], token?: string) => {
const langs = [];
for (const repo of names) {
langs.push(await fetchLanguage(owner, repo, token));
}
return langs;
};

export default languages;
30 changes: 30 additions & 0 deletions gitlangRoutes/helpers/github/repositories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Octokit } from '@octokit/rest';

let octokit: Octokit;

const repositories = async (username: string, token?: string) => {
try {
if (!octokit) {
if (!token) {
console.error('No token');
return [];
}
octokit = new Octokit({ auth: token });
}
return await octokit.paginate(
octokit.rest.repos.listForUser,
{
username,
type: 'owner',
},
(response) =>
response.data
.filter((repo: { fork: boolean }) => repo.fork === false)
.map((repo: { name: string }) => repo.name),
);
} catch {
return [];
}
};

export default repositories;
30 changes: 30 additions & 0 deletions gitlangRoutes/requests/gitlangLocal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import languages from '../helpers/github/languages';
import repositories from '../helpers/github/repositories';

const gitlangLocal = {
langs: async (owner: string, repos: string[], token?: string) => {
try {
const response = await languages(owner, repos, token);
if (response && response.length > 0) {
return JSON.stringify(response);
}
return JSON.stringify([]);
} catch {
return JSON.stringify([]);
}
},

repos: async (username: string, token?: string) => {
try {
const response = await repositories(username, token);
if (response && response.length > 0) {
return JSON.stringify(response);
}
return JSON.stringify([]);
} catch {
return JSON.stringify([]);
}
},
};

export default gitlangLocal;
59 changes: 59 additions & 0 deletions gitlangRoutes/requests/gitlangRouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import Router from '@koa/router';

import auth from '../helpers/github/auth';
import languages from '../helpers/github/languages';
import repositories from '../helpers/github/repositories';

const router = new Router({ prefix: '/github' });

router.get(
'/langs',
async (context: {
request: { query: { owner: string; repos: string } };
response: { status: number; body: string };
}) => {
try {
const token = await auth();
const { owner } = context.request.query;
const { repos } = context.request.query;
const repoList = JSON.parse(repos) as string[];
const response = await languages(owner, repoList, token);
if (response && response.length > 0) {
context.response.status = 200;
context.response.body = JSON.stringify(response);
} else {
context.response.status = 404;
context.response.body = JSON.stringify([]);
}
} catch {
context.response.status = 404;
context.response.body = JSON.stringify([]);
}
},
);

router.get(
'/repos',
async (context: {
request: { query: { username: string } };
response: { status: number; body: string };
}) => {
try {
const token = await auth();
const { username } = context.request.query;
const response = await repositories(username, token);
if (response && response.length > 0) {
context.response.status = 200;
context.response.body = JSON.stringify(response);
} else {
context.response.status = 404;
context.response.body = JSON.stringify([]);
}
} catch {
context.response.status = 404;
context.response.body = JSON.stringify([]);
}
},
);

export default router;
6 changes: 6 additions & 0 deletions init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

yum update -y && yum upgrade -y
cd /home/ec2-user/auth-server
npm ci
screen -Sdm node npm start
5 changes: 5 additions & 0 deletions local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import gitlangLocal from './routes/requests/gitlangLocal';

const router = gitlangLocal;

export default router;
Loading