@@ -10,53 +10,73 @@ import {
1010} from './utils/verifyPresentationRequest.js'
1111import { downloadLogs } from './utils/logger.js'
1212import dotenv from 'dotenv'
13+ import path from 'path'
14+ import { fileURLToPath } from 'url'
15+ import { policyServerApiKeyAuth } from './utils/auth.js'
16+
1317dotenv . config ( )
14- const app = express ( )
15- const authType = process . env . AUTH_TYPE || 'waltid'
16- async function handlePolicyRequest (
17- req : Request < { } , { } , PolicyRequestPayload > ,
18- res : Response
19- ) : Promise < void > {
20- const { action, ...rest } = req . body
21-
22- const handler = PolicyHandlerFactory . createPolicyHandler ( authType )
23- if ( handler == null ) {
24- res . status ( 404 ) . json ( {
25- success : false ,
26- status : 404 ,
27- message : `Handler for auth type "${ authType } " is not found.`
28- } )
18+
19+ export function createApp ( ) : express . Express {
20+ const app = express ( )
21+ const authType = process . env . AUTH_TYPE || 'waltid'
22+
23+ async function handlePolicyRequest (
24+ req : Request < { } , { } , PolicyRequestPayload > ,
25+ res : Response
26+ ) : Promise < void > {
27+ const { action, ...rest } = req . body
28+
29+ const handler = PolicyHandlerFactory . createPolicyHandler ( authType )
30+ if ( handler == null ) {
31+ res . status ( 404 ) . json ( {
32+ success : false ,
33+ httpStatus : 404 ,
34+ message : `Handler for auth type "${ authType } " is not found.`
35+ } )
36+ return
37+ }
38+
39+ const payload : PolicyRequestPayload = { action, ...rest }
40+ const response : PolicyRequestResponse = await handler . execute ( payload )
41+ res . status ( response . httpStatus ) . json ( response )
2942 }
3043
31- const payload : PolicyRequestPayload = { action, ...rest }
32- const response : PolicyRequestResponse = await handler . execute ( payload )
33- res . status ( response . httpStatus ) . json ( response )
34- }
44+ app . use ( express . json ( ) )
3545
36- app . use ( express . json ( ) )
37- app . use ( requestLogger )
38- if ( process . env . MODE_PS && process . env . MODE_PS === '1' ) {
39- app . post ( '/' , asyncHandler ( handlePolicyRequest ) )
40- }
41- if (
42- process . env . OCEAN_NODE_URL &&
43- process . env . MODE_PROXY &&
44- process . env . MODE_PROXY === '1'
45- ) {
46- app . post (
47- '/verify/:id' ,
48- express . urlencoded ( { extended : true } ) ,
49- asyncHandler ( handleVerifyPresentationRequest )
50- )
51- app . get ( '/pd/:id' , asyncHandler ( handleGetPD ) )
52- }
53- if ( process . env . ENABLE_LOGS && process . env . ENABLE_LOGS === '1' ) {
54- app . get ( '/logs' , downloadLogs )
46+ if ( process . env . MODE_PS === '1' ) {
47+ app . post (
48+ '/' ,
49+ policyServerApiKeyAuth ,
50+ requestLogger ,
51+ asyncHandler ( handlePolicyRequest )
52+ )
53+ }
54+ if ( process . env . OCEAN_NODE_URL && process . env . MODE_PROXY === '1' ) {
55+ app . post (
56+ '/verify/:id' ,
57+ express . urlencoded ( { extended : true } ) ,
58+ requestLogger ,
59+ asyncHandler ( handleVerifyPresentationRequest )
60+ )
61+ app . get ( '/pd/:id' , requestLogger , asyncHandler ( handleGetPD ) )
62+ }
63+ if ( process . env . ENABLE_LOGS === '1' ) {
64+ app . get ( '/logs' , requestLogger , downloadLogs )
65+ }
66+ app . use ( '/api-docs' , swaggerUi . serve , swaggerUi . setup ( swaggerDoc ) )
67+ app . use ( errorHandler )
68+
69+ return app
5570}
56- app . use ( '/api-docs' , swaggerUi . serve , swaggerUi . setup ( swaggerDoc ) )
57- app . use ( errorHandler )
5871
59- const PORT = process . env . PORT || 3000
60- app . listen ( PORT , ( ) => {
61- console . log ( `Server is running on port ${ PORT } ` )
62- } )
72+ export const app = createApp ( )
73+
74+ const currentModulePath = fileURLToPath ( import . meta. url )
75+ const entrypointPath = process . argv [ 1 ] ? path . resolve ( process . argv [ 1 ] ) : ''
76+
77+ if ( entrypointPath === currentModulePath ) {
78+ const PORT = process . env . PORT || 3000
79+ app . listen ( PORT , ( ) => {
80+ console . log ( `Server is running on port ${ PORT } ` )
81+ } )
82+ }
0 commit comments