-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Adam McKee
committed
Oct 16, 2024
1 parent
fce76a4
commit e39a3c4
Showing
3 changed files
with
45 additions
and
696 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,42 @@ | ||
import cookieParser from 'cookie-parser' | ||
import express from 'express' | ||
import {serve} from '@hono/node-server' | ||
import {Hono} from 'hono' | ||
import {logger} from 'hono/logger' | ||
import {handleGraphQuery} from './data.js' | ||
|
||
const HTTP_PORT = 7411 | ||
|
||
const app = express() | ||
app.use(cookieParser()) | ||
app.use(express.json()) | ||
app.use((req, res, next) => { | ||
const start = Date.now() | ||
res.on('finish', () => { | ||
const uri = decodeURI(req.url) | ||
const end = `${Date.now() - start}ms` | ||
if (res.statusCode === 301) { | ||
console.log(req.method, uri, res.statusCode, res.statusMessage, 'to', res.getHeaders().location, end) | ||
} else { | ||
console.log(req.method, uri, res.statusCode, res.statusMessage, end) | ||
} | ||
}) | ||
next() | ||
}) | ||
const app = new Hono() | ||
app.use(logger()) | ||
|
||
app.post('/offline/github/graph', (req, res) => { | ||
res.json({data: handleGraphQuery(req.body.query)}) | ||
app.post('/offline/github/graph', async (c) => { | ||
const json = await c.req.json() | ||
return c.json({data: handleGraphQuery(json.query)}) | ||
}) | ||
|
||
app.get('/offline/github/oauth', async (req, res) => { | ||
app.get('/offline/github/oauth', (c) => { | ||
const accessToken = '1234' | ||
// todo does Safari work with Secure and SameSite=Strict when the hostname isn't localhost? | ||
if (req.header('user-agent')?.includes('Safari')) { | ||
res.setHeader('Set-Cookie', `ght=${accessToken}; Path=/`) | ||
if (c.req.header('User-Agent')?.includes('Safari')) { | ||
c.res.headers.append('Set-Cookie', `ght=${accessToken}; Path=/`) | ||
} else { | ||
res.setHeader('Set-Cookie', `ght=${accessToken}; Secure; SameSite=Strict; Path=/`) | ||
c.res.headers.append('Set-Cookie', `ght=${accessToken}; Secure; SameSite=Strict; Path=/`) | ||
} | ||
res.redirect('http://localhost:5711') | ||
return c.redirect('http://localhost:5711', 302) | ||
}) | ||
|
||
const generatedScripts: Array<any> = [] | ||
|
||
app.get('/offline/api/generated-scripts', async (_req, res) => { | ||
res.json(generatedScripts) | ||
}) | ||
app.get('/offline/api/generated-scripts', (c) => c.json(generatedScripts)) | ||
|
||
app.post('/offline/api/generated-scripts', async (req, res) => { | ||
generatedScripts.push(req.body) | ||
console.log(generatedScripts) | ||
res.end() | ||
app.post('/offline/api/generated-scripts', async (c) => { | ||
const generatedScript = await c.req.json() | ||
generatedScripts.push(generatedScript) | ||
console.log(JSON.stringify(generatedScript, null, 4)) | ||
return c.body('') | ||
}) | ||
|
||
app.listen(HTTP_PORT, () => { | ||
console.log('install.sh http listening on', 7411) | ||
serve({ | ||
fetch: app.fetch, | ||
port: HTTP_PORT, | ||
}) | ||
|
Oops, something went wrong.