Skip to content

Commit

Permalink
fix: runtime redis configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
qin-guan committed Jul 16, 2023
1 parent b1d4fb9 commit b4bfda8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
29 changes: 8 additions & 21 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,6 @@ export default defineNuxtConfig({
},

image: {},
nitro: {
storage: {
redis: (() => (process.env.NUXT_ENABLE_REDIS
? {
driver: 'redis',
port: process.env.NUXT_REDIS_PORT,
host: process.env.NUXT_REDIS_HOST,
username: process.env.NUXT_REDIS_USERNAME,
password: process.env.NUXT_REDIS_PASSWORD,
}
: {
driver: 'memory',
}
))(),
},
devStorage: {
redis: {
driver: 'memory',
},
},
},

runtimeConfig: {
public: {
Expand All @@ -78,6 +57,14 @@ export default defineNuxtConfig({
sessionSecret: '',
sessionName: 'h3',

redis: {
enabled: false,
host: '',
port: 0,
username: '',
password: '',
},

resend: {
apiKey: '',
fromAddress: '',
Expand Down
11 changes: 11 additions & 0 deletions plugins/env.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ const resendSchema = z.object({
}),
})

const redisSchema = z.object({
redis: z.object({
enabled: z.boolean(),
host: z.string(),
port: z.number().positive(),
username: z.string(),
password: z.string(),
}),
})

/**
* Specify your server-side environment variables schema here. This way you can ensure the app isn't
* built with invalid env vars.
Expand All @@ -37,6 +47,7 @@ const server = z
})
// Add on schemas as needed that requires conditional validation.
.merge(resendSchema)
.merge(redisSchema)
.merge(client)
.refine(val => !(val.resend.apiKey && !val.resend.fromAddress), {
message: 'resend.fromAddress is required when resend.apiKey is set',
Expand Down
18 changes: 18 additions & 0 deletions server/plugins/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import redisDriver from 'unstorage/drivers/redis'
import memoryDriver from 'unstorage/drivers/memory'

export default defineNitroPlugin(() => {
const storage = useStorage()

const driver = useRuntimeConfig().redis.enabled
? redisDriver({
base: 'redis',
host: useRuntimeConfig().redis.host,
port: useRuntimeConfig().redis.port,
username: useRuntimeConfig().redis.username,
password: useRuntimeConfig().redis.password,
})
: memoryDriver()

storage.mount('redis', driver)
})

0 comments on commit b4bfda8

Please sign in to comment.