All files server.js

94.11% Statements 48/51
73.33% Branches 11/15
100% Functions 2/2
94.11% Lines 48/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 761x 1x 1x 1x   1x 1x     1x 1x 1x 1x 1x   1x 1x 5x   5x 1x 1x 1x 1x 1x 1x   1x 1x   1x   1x 5x   5x 1x 1x   5x 1x 1x     3x 3x 3x 3x 5x 5x 5x                   5x 2x 5x 1x 1x   3x 3x     1x       1x  
import express from 'express'
import cors from 'cors'
import logger from './logger.js'
import {startServer} from './starter.js'
 
const app = express()
app.use(express.json())
 
// 例: 環境変数は空白 or カンマ区切りどちらでもOKに
const parseOrigins = (v) => v.split(/[,\s]+/).map(s => s.trim()).filter(Boolean)
const allowed = parseOrigins(
    process.env.CORS_ORIGINS ||
    'http://www.resume.local http://agent.resume.local http://corporate.resume.local'
)
 
const corsOptions = {
    origin(origin, cb) {
        if (!origin) return cb(null, true)
        return cb(null, allowed.includes(origin))
    },
    methods: ['POST', 'OPTIONS'],
    allowedHeaders: ['Content-Type', 'X-Request-ID', 'Authorization'],
    credentials: true,
    maxAge: 86400,
    optionsSuccessStatus: 204,
}
 
app.use(cors(corsOptions))
app.options('*', cors(corsOptions))
 
app.get('/health', (_req, res) => res.status(200).send('health check ok.'))
 
app.post('/', (req, res) => {
    const { level = 'info', schema, message, payload, timestamp } = req.body
 
    if (!schema) {
        return res.status(400).send('schema is required')
    }
 
    if (!message) {
        return res.status(400).send('message is required')
    }
 
    // const logEntry = { message }
    const logEntry = {
        level,
        schema,
        message,
        ...(payload && { payload }),
        ...(timestamp && { timestamp })
    }
 
    // if (payload && Object.keys(payload).length > 0) {
    //     logEntry.payload = payload
    // }
    //
    // if (timestamp) {
    //     logEntry.timestamp = timestamp
    // }
 
    if (typeof logger[level] === 'function') {
        logger[level](logEntry)
    } else {
        logger.info(logEntry)
    }
 
    res.status(200).send('ok')
})
 
// テスト時はlistenしない
if (process.env.NODE_ENV !== 'test') {
    startServer(app)
}
 
export default app