File tree Expand file tree Collapse file tree 5 files changed +236
-76
lines changed
Expand file tree Collapse file tree 5 files changed +236
-76
lines changed Original file line number Diff line number Diff line change 77 "cf-typegen" : " wrangler types --env-interface CloudflareBindings"
88 },
99 "dependencies" : {
10- "hono" : " ^4.9.10"
10+ "@supabase/supabase-js" : " ^2.58.0" ,
11+ "@t3-oss/env-core" : " ^0.13.8" ,
12+ "hono" : " ^4.9.10" ,
13+ "zod" : " ^4.1.12"
1114 },
1215 "devDependencies" : {
1316 "wrangler" : " ^4.42.0"
Original file line number Diff line number Diff line change 1+ import { createEnv } from "@t3-oss/env-core" ;
2+ import type { Context } from "hono" ;
3+ import { env } from "hono/adapter" ;
4+ import { z } from "zod" ;
5+
6+ export const getEnv = ( c : Context ) =>
7+ createEnv ( {
8+ server : {
9+ PORT : z . coerce . number ( ) . default ( 3000 ) ,
10+ OPENAI_DEFAULT_MODEL : z . string ( ) . min ( 1 ) ,
11+ OPENAI_BASE_URL : z . string ( ) . min ( 1 ) ,
12+ OPENAI_API_KEY : z . string ( ) . min ( 1 ) ,
13+ SUPABASE_URL : z . string ( ) . min ( 1 ) ,
14+ SUPABASE_SERVICE_ROLE_KEY : z . string ( ) . min ( 1 ) ,
15+ } ,
16+ runtimeEnv : env ( c ) ,
17+ emptyStringAsUndefined : true ,
18+ } ) ;
Original file line number Diff line number Diff line change 11import { Hono } from "hono" ;
2+ import { proxy } from "hono/proxy" ;
3+
4+ import { getEnv } from "./env" ;
5+ import { supabaseMiddleware } from "./middleware/supabase" ;
26
37const app = new Hono ( ) ;
8+ app . use ( "/v1" , supabaseMiddleware ( ) ) ;
9+
10+ app . get ( "/health" , ( c ) => c . text ( "OK" ) ) ;
11+
12+ app . post ( "/v1/write" , async ( c ) => {
13+ return c . json ( { message : "OK" } ) ;
14+ } ) ;
15+
16+ app . post ( "/v1/chat/completions" , async ( c ) => {
17+ const { OPENAI_BASE_URL , OPENAI_DEFAULT_MODEL , OPENAI_API_KEY } = getEnv ( c ) ;
18+
19+ const data = await c . req . json ( ) ;
20+
21+ const res = await proxy (
22+ `${ OPENAI_BASE_URL } /chat/completions` ,
23+ {
24+ method : "POST" ,
25+ body : JSON . stringify ( {
26+ ...data ,
27+ model : OPENAI_DEFAULT_MODEL ,
28+ } ) ,
29+ headers : {
30+ Authorization : `Bearer ${ OPENAI_API_KEY } ` ,
31+ } ,
32+ } ,
33+ ) ;
434
5- app . get ( "/" , ( c ) => {
6- return c . text ( "Hello Hono!" ) ;
35+ return res ;
736} ) ;
837
938export default app ;
Original file line number Diff line number Diff line change 1+ import { createClient } from "@supabase/supabase-js" ;
2+ import { createMiddleware } from "hono/factory" ;
3+
4+ import { getEnv } from "../env" ;
5+
6+ export const supabaseMiddleware = ( ) => {
7+ return createMiddleware ( async ( c , next ) => {
8+ const authHeader = c . req . header ( "Authorization" ) ;
9+ const token = authHeader ?. replace ( "Bearer " , "" ) ;
10+
11+ if ( ! token ) {
12+ return c . json ( { error : "missing_authorization_header" } , 401 ) ;
13+ }
14+
15+ const { SUPABASE_URL , SUPABASE_SERVICE_ROLE_KEY } = getEnv ( c ) ;
16+ const supabase = createClient ( SUPABASE_URL , SUPABASE_SERVICE_ROLE_KEY ) ;
17+ const { data, error } = await supabase . auth . getUser ( token ) ;
18+ if ( error || ! data ?. user ) {
19+ return c . json ( { error : "invalid_authorization_header" } , 401 ) ;
20+ }
21+
22+ c . set ( "supabase" , supabase ) ;
23+ c . set ( "user" , data . user ) ;
24+ await next ( ) ;
25+ } ) ;
26+ } ;
You can’t perform that action at this time.
0 commit comments