Skip to content

Commit

Permalink
feat: add docker
Browse files Browse the repository at this point in the history
Signed-off-by: Marin Petrunic <[email protected]>
  • Loading branch information
mpetrunic committed May 25, 2024
1 parent 80e9149 commit 5fa746a
Show file tree
Hide file tree
Showing 13 changed files with 6,988 additions and 5,328 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
**/node_modules
.env
**/.env
15 changes: 15 additions & 0 deletions Dockerfile.server
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Stage 1: Build the TypeScript code
FROM node:20-alpine AS build

WORKDIR /app

RUN corepack enable

COPY package.json yarn.lock .yarn .yarnrc.yml ./
COPY packages/zu-git-proofs-server/package.json packages/zu-git-proofs-server/

RUN yarn workspaces focus zu-git-proofs-server

COPY . .

CMD ["yarn", "workspace", "zu-git-proofs-server", "run", "start"]
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: '3.8'

services:
server:
image: ghcr.io/mpetrunic/zu-git-proofs-server:v0.1
container_name: zu-git-proof-server
env_file: zu-git-pass.env
ports:
- "9000:3000"
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"name": "zu-git-proofs",
"packageManager": "yarn@3.3.1",
"packageManager": "yarn@4.2.2",
"private": true,
"workspaces": [
"packages/*"
]
],
"devDependencies": {
"tsx": "4.11.0"
}
}
3 changes: 2 additions & 1 deletion packages/zu-git-pass-client/src/GitHubCallback.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useEffect } from 'react';

export default function GitHubCallback() {

useEffect(() => {
const urlParams = new URLSearchParams(window.location.search);
const code = urlParams.get('code');

console.log("Running effect", code)
if (code) {
fetch(`${process.env.REACT_APP_ZU_GIT_SERVER_API}/auth/github`, {
method: 'POST',
Expand Down
2 changes: 1 addition & 1 deletion packages/zu-git-pass-client/src/GithubLogin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default function GithubLogin() {
const loginWithGitHub = React.useCallback(() => {
const clientID = process.env.REACT_APP_GITHUB_CLIENT_ID;
const redirectURI = `${window.location.href}auth/github/callback`;
window.location.href = `https://github.com/login/oauth/authorize?client_id=${clientID}&redirect_uri=${redirectURI}`;
window.location.href = `https://github.com/login/oauth/authorize?client_id=${clientID}&redirect_uri=${redirectURI}&scope=read:user,user:email`;
}, []);

return (
Expand Down
4 changes: 1 addition & 3 deletions packages/zu-git-pass-client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import reportWebVitals from './reportWebVitals';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
<App />
);

// If you want to start measuring performance in your app, pass a function
Expand Down
4 changes: 1 addition & 3 deletions packages/zu-git-proofs-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
"@fastify/cors": "^9.0.1",
"@fastify/env": "^4.3.0",
"@octokit/auth-oauth-app": "^8.1.1",
"@octokit/oauth-app": "^7.1.2",
"@pcd/eddsa-pcd": "^0.6.0",
"@pcd/email-pcd": "^0.6.0",
"@pcd/message-pcd": "^0.1.0",
"@pcd/passport-interface": "^0.11.0",
"@pcd/pcd-collection": "^0.11.0",
"@pcd/pcd-types": "0.11.0",
"fastify": "^4.27.0",
"fastify-sqlite-typed": "^0.1.1",
"octokit": "^4.0.2",
Expand All @@ -20,7 +19,6 @@
"devDependencies": {
"@tsconfig/node20": "^20.1.4",
"@types/node": "^20.12.12",
"ts-node": "^10.9.2",
"tsx": "^4.11.0",
"typescript": "^5.4.5"
},
Expand Down
19 changes: 11 additions & 8 deletions packages/zu-git-proofs-server/src/badges.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import jsonData from './artifacts/badge-members.json'
// @ts-expect-error
import jsonData from './artifacts/badge-members.json' assert {type: 'json'};


export async function getGithubBadhes(githubUsername: string): Promise<string|undefined> {
const badges: string[] = []
export async function getGithubBadhes(githubEmails: string[]): Promise<string|undefined> {
const badges = new Set()
Object.entries(jsonData).forEach(([badge, badgeMembers]) => {
if((badgeMembers as string[]).includes(githubUsername.toLowerCase())) {
badges.push(badge)
}
githubEmails.forEach((email) => {
if((badgeMembers as string[]).includes(email.toLowerCase())) {
badges.add(badge)
}
})
})
if(badges.length == 0) {
if(badges.size == 0) {
return undefined
}
return badges.join(",")
return Array.from(badges.values()).join(",")
}
5 changes: 5 additions & 0 deletions packages/zu-git-proofs-server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const schema: FastifyEnvOptions["schema"] = {
type: 'number',
default: 3000
},
HOST: {
type: "string",
default: "localhost"
},
SERVER_PRIVATE_KEY: {
type: 'string'
},
Expand Down Expand Up @@ -48,6 +52,7 @@ declare module 'fastify' {
interface FastifyInstance {
config: {
PORT: number,
HOST: string,
GITHUB_CLIENT_ID: string,
GITHUB_CLIENT_SECRET: string,
SERVER_PRIVATE_KEY: string,
Expand Down
2 changes: 1 addition & 1 deletion packages/zu-git-proofs-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async function init() {


init().then(() => {
server.listen({ port: server.config.PORT }, (err, address) => {
server.listen({ port: server.config.PORT, host: server.config.HOST }, (err, address) => {
if (err) {
console.error(err)
process.exit(1)
Expand Down
11 changes: 5 additions & 6 deletions packages/zu-git-proofs-server/src/routes/auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import { Octokit } from "octokit";
import { storeZupassBadge } from "../db.js";
import { getGithubBadhes } from "../badges.js";
import { getGithubBadhes as getGithubBadges } from "../badges.js";


export function authRoute(server: FastifyInstance): void {
Expand All @@ -16,7 +16,7 @@ export function authRoute(server: FastifyInstance): void {
) => {
try {
const { code, zupassEmail } = req.body;

console.log("Received callback", code, zupassEmail)
const tokenAuthentication = await server.oauth({
type: "oauth-user",
code: code,
Expand All @@ -25,10 +25,9 @@ export function authRoute(server: FastifyInstance): void {
const octokit = new Octokit({
auth: tokenAuthentication.token,
});
const { data: { login } } = await octokit.rest.users.getAuthenticated();
console.log("Authenticated user", {github: login, zupassEmail})
const badge = await getGithubBadhes(login)

const { data: emails } = await octokit.rest.users.listEmailsForAuthenticatedUser();
console.log("Authenticated user", {github: emails, zupassEmail})
const badge = await getGithubBadges(emails.map((email) => email.email))
console.log("User badges", {zupassEmail, badges: badge})
if(badge) {
await storeZupassBadge(server.db, zupassEmail, badge)
Expand Down
Loading

0 comments on commit 5fa746a

Please sign in to comment.