Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
add ecs button
Browse files Browse the repository at this point in the history
  • Loading branch information
gkorland committed Sep 12, 2023
1 parent e7db018 commit 71e1837
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 149 deletions.
23 changes: 23 additions & 0 deletions app/api/auth/[...nextauth]/options.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { AuthOptions } from "next-auth"
import GoogleProvider from "next-auth/providers/google";
import GithubProvider from "next-auth/providers/github"
import { TypeORMAdapter } from "@auth/typeorm-adapter"
import { Adapter } from "next-auth/adapters";
import dataSourceOptions from "@/app/api/db/options";

const authOptions: AuthOptions = {
adapter: TypeORMAdapter(dataSourceOptions) as Adapter,
providers: [
GithubProvider({
clientId: process.env.GITHUB_ID as string,
clientSecret: process.env.GITHUB_SECRET as string,
}),
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
}),
],
}


export default authOptions;
36 changes: 1 addition & 35 deletions app/api/auth/[...nextauth]/route.tsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,6 @@

import NextAuth from "next-auth"
import type { AuthOptions } from "next-auth"
import GoogleProvider from "next-auth/providers/google";
import GithubProvider from "next-auth/providers/github"
import { TypeORMAdapter } from "@auth/typeorm-adapter"
import { DataSourceOptions } from "typeorm"
import { Adapter } from "next-auth/adapters";

const env = process.env.NODE_ENV;
const connection: DataSourceOptions = {
type: "postgres",
host: (process.env.POSTGRES_HOST || "localhost") as string,
port: Number(process.env.POSTGRES_PORT || 5432),
username: process.env.POSTGRES_USER as string,
password: process.env.POSTGRES_PASSWORD as string,
database: (process.env.POSTGRES_DATABASE || "falkordb") as string,
synchronize: (env == "development" ? true : false),
ssl: (env == "development" ? undefined : {
rejectUnauthorized: false,
requestCert: true,
}),
}

const authOptions: AuthOptions = {
adapter: TypeORMAdapter(connection) as Adapter,
providers: [
GithubProvider({
clientId: process.env.GITHUB_ID as string,
clientSecret: process.env.GITHUB_SECRET as string,
}),
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
}),
],
}
import authOptions from "./options"

const handler = NextAuth(authOptions)

Expand Down
18 changes: 18 additions & 0 deletions app/api/db/options.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { DataSourceOptions } from "typeorm"

const env = process.env.NODE_ENV;
const dataSourceOptions: DataSourceOptions = {
type: "postgres",
host: (process.env.POSTGRES_HOST || "localhost") as string,
port: Number(process.env.POSTGRES_PORT || 5432),
username: process.env.POSTGRES_USER as string,
password: process.env.POSTGRES_PASSWORD as string,
database: (process.env.POSTGRES_DATABASE || "falkordb") as string,
synchronize: (env == "development" ? true : false),
ssl: (env == "development" ? undefined : {
rejectUnauthorized: false,
requestCert: true,
}),
}

export default dataSourceOptions;
29 changes: 17 additions & 12 deletions app/api/db/route.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { NextResponse } from 'next/server'
import { ECSClient, RunTaskCommand, StartTaskCommand } from "@aws-sdk/client-ecs";
import authOptions from '@/app/api/auth/[...nextauth]/options';
import { getServerSession } from "next-auth/next"
import { NextResponse } from "next/server"

const SUBNETS = process.env.SUBNETS?.split(":");
const SECURITY_GROUPS = process.env.SECURITY_GROUPS?.split(":");

export async function POST(request: Request) {
export async function POST() {

const session = await getServerSession(authOptions)

if (!session) {
return NextResponse.json({ message: "You must be logged in." }, { status: 401 })
}

// Create ECS service client object.
const client = new ECSClient({ region:process.env.REGION });
Expand All @@ -31,16 +39,13 @@ export async function POST(request: Request) {
}
};

const run = async () => {
try {
const data = await client.send(new RunTaskCommand(params));
console.log("Success, task started!", data);
} catch (err) {
console.log("Error", err);
}
try {
const data = await client.send(new RunTaskCommand(params));
console.debug("Success, task started!", data);
} catch (err) {
console.log("Error", err);
return NextResponse.json({ message: "Failed to start Task" }, { status: 500 })
}

run();

return NextResponse.json({ message: "Task Started" })
return NextResponse.json({ message: "Task Started" })
}
2 changes: 1 addition & 1 deletion app/components/navigation/navbar/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default function Button() {

if (status === "unauthenticated") {
return (
<button onClick={() => signIn(undefined, { callbackUrl: '/dashboard' })} className="h-12 rounded-lg font-bold px-5">Sign in</button>
<button onClick={() => signIn(undefined, { callbackUrl: '/sandbox' })} className="h-12 rounded-lg font-bold px-5">Sign in</button>
);
}
return <button onClick={() => signOut({ callbackUrl: '/' })} className="h-12 rounded-lg font-bold px-5">Log Out</button>
Expand Down
2 changes: 1 addition & 1 deletion app/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default function Page() {
}

if (status === "unauthenticated") {
signIn(undefined, { callbackUrl: '/dashboard' })
signIn(undefined, { callbackUrl: '/sandbox' })
}

return (
Expand Down
2 changes: 1 addition & 1 deletion app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default function Page() {
<main className="flex flex-col items-center justify-center flex-1 px-20 text-center">
<h1 className="text-6xl font-bold">
Welcome to{' '}
<a className="text-blue-600" href="/dashboard">
<a className="text-blue-600" href="/sandbox">
FalkorDB Cloud
</a>
</h1>
Expand Down
38 changes: 38 additions & 0 deletions app/sandbox/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use client';

import { useSession, getSession, signIn } from "next-auth/react"
import { redirect } from 'next/navigation';

export default function Page() {
const { data: session, status } = useSession()

if (status === "loading") {
return <p className="text-blue-600 text-3xl">Loading...</p>
}

if (status === "unauthenticated") {
signIn(undefined, { callbackUrl: '/sandbox' })
}

function createSandbox(){
fetch('/api/db', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({session: session})
})
.then(response => {
console.log(response.json())
redirect('/dashboard')
})
}

return (
<div className="flex flex-col items-center justify-center min-h-screen py-2">
<main className="flex flex-col items-center justify-center flex-1 px-20 text-center">
<button className="rounded-full bg-blue-600 text-6xl font-bold p-5" onClick={createSandbox}>Create Sandbox</button>
</main>
</div>
)
}
Loading

0 comments on commit 71e1837

Please sign in to comment.