-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve env validation and definition #19
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,30 @@ | |
}}} | ||
import env from '#start/env' | ||
|
||
const kafkaSecurityProtocol = env.get('KAFKA_SECURITY_PROTOCOL', 'PLAIN') | ||
const useSSL = kafkaSecurityProtocol === 'SSL' | ||
const useSasl = env.get('KAFKA_SASL_MECHANISM') !== undefined | ||
|
||
const saslOptions = useSasl | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure we want this in the default stub. I’d opt for getting folks going fast in a dev env. But we could leave this commented out so folks know they have this path for a live env There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mmm, maybe. Idk. |
||
? { | ||
mechanism: env.get('KAFKA_SASL_MECHANISM')?.toLowerCase(), | ||
username: env.get('KAFKA_SASL_USERNAME'), | ||
password: env.get('KAFKA_SASL_PASSWORD'), | ||
} | ||
: undefined | ||
|
||
const kafkaConfig = { | ||
brokers: env.get('KAFKA_BROKERS', 'localhost:9092'), | ||
clientId: env.get('KAFKA_CLIENT_ID'), | ||
ssl: useSSL, | ||
sasl: saslOptions, | ||
clientId: env.get('KAFKA_CLIENT_ID', 'local'), | ||
timeouts: { | ||
connection: env.get('KAFKA_CONNECTION_TIMEOUT'), | ||
authentication: env.get('KAFKA_AUTHENTICATION_TIMEOUT'), | ||
reauthentication: env.get('KAFKA_REAUTHENTICATION_TIMEOUT'), | ||
request: env.get('KAFKA_REQUEST_TIMEOUT'), | ||
}, | ||
logLevel: env.get('KAFKA_LOG_LEVEL', env.get('LOG_LEVEL')), | ||
} | ||
|
||
export default kafkaConfig | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { test } from '@japa/runner' | ||
import { KafkaEnv } from '../src/env/index.ts' | ||
|
||
interface BrokersAssertion { | ||
input: string | ||
expected: string[] | ||
error: boolean | ||
} | ||
|
||
test.group('KafkaEnv', () => { | ||
const cases: BrokersAssertion[] = [ | ||
{ input: 'localhost:9092', error: false, expected: ['localhost:9092'] }, | ||
{ | ||
input: | ||
'b-1.example.foobar.c19.kafka.us-east-1.amazonaws.com:9096,b-2.example.foobar.c19.kafka.us-east-1.amazonaws.com:9096', | ||
error: false, | ||
expected: [ | ||
'b-1.example.foobar.c19.kafka.us-east-1.amazonaws.com:9096', | ||
'b-2.example.foobar.c19.kafka.us-east-1.amazonaws.com:9096', | ||
], | ||
}, | ||
{ input: '0.0.0.0:9092', error: false, expected: ['0.0.0.0:9092'] }, | ||
{ | ||
input: '172.17.0.2:9092,172.17.0.3:9092', | ||
error: false, | ||
expected: ['172.17.0.2:9092', '172.17.0.3:9092'], | ||
}, | ||
{ | ||
input: 'localhost:9092,localhost:9093', | ||
error: false, | ||
expected: ['localhost:9092', 'localhost:9093'], | ||
}, | ||
// missing port: | ||
{ input: 'localhost', error: true, expected: [] }, | ||
{ input: '0.0.0.0', error: true, expected: [] }, | ||
// missing host | ||
{ input: ':9092', error: true, expected: [] }, | ||
// missing port | ||
{ input: 'localhost:', error: true, expected: [] }, | ||
// invalid port: | ||
{ input: 'localhost:aaa', error: true, expected: [] }, | ||
// empty element: | ||
{ input: 'localhost,,', error: true, expected: [] }, | ||
] | ||
|
||
for (const testcase of cases) { | ||
test(`schema.brokers with "${testcase.input}" should be ${testcase.error ? 'invalid' : 'valid'}`, async ({ | ||
assert, | ||
}) => { | ||
const key = 'KAFKA_BROKERS' | ||
const validator = KafkaEnv.schema.brokers() | ||
if (testcase.error) { | ||
assert.throws(() => { | ||
validator(key, testcase.input) | ||
}) | ||
} else { | ||
assert.doesNotThrow(() => { | ||
try { | ||
validator(key, testcase.input) | ||
} catch (err) { | ||
console.log(err) | ||
throw err | ||
} | ||
}) | ||
|
||
const result = validator(key, testcase.input) | ||
|
||
assert.sameMembers(result, testcase.expected) | ||
} | ||
}) | ||
} | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
may want: