Skip to content

Commit

Permalink
refactor(idp): health endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bouassaba committed Dec 13, 2024
1 parent 3864852 commit 76a2de4
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 35 deletions.
6 changes: 0 additions & 6 deletions api/package-lock.json

This file was deleted.

11 changes: 7 additions & 4 deletions idp/src/account/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import {
import { newHashId, newHyphenlessUuid } from '@/infra/id.ts'
import { sendTemplateMail } from '@/infra/mail.ts'
import { hashPassword } from '@/infra/password.ts'
import search, { USER_SEARCH_INDEX } from '@/infra/search.ts'
import {
client as meilisearch,
USER_SEARCH_INDEX,
} from '../infra/meilisearch.ts'
import { User } from '@/user/model.ts'
import userRepo from '@/user/repo.ts'
import { getUserCount, mapEntity, UserDTO } from '@/user/service.ts'
Expand Down Expand Up @@ -73,7 +76,7 @@ export async function createUser(
createTime: newDateTime(),
isAdmin: options.isAdmin,
})
await search.index(USER_SEARCH_INDEX).addDocuments([
await meilisearch.index(USER_SEARCH_INDEX).addDocuments([
{
id: user.id,
username: user.username,
Expand All @@ -91,7 +94,7 @@ export async function createUser(
return mapEntity(user)
} catch (error) {
await userRepo.delete(id)
await search.index(USER_SEARCH_INDEX).deleteDocuments([id])
await meilisearch.index(USER_SEARCH_INDEX).deleteDocuments([id])
throw newInternalServerError(error)
}
}
Expand All @@ -111,7 +114,7 @@ export async function confirmEmail(options: AccountConfirmEmailOptions) {
isEmailConfirmed: true,
emailConfirmationToken: null,
})
await search.index(USER_SEARCH_INDEX).updateDocuments([
await meilisearch.index(USER_SEARCH_INDEX).updateDocuments([
{
id: user.id,
username: user.username,
Expand Down
22 changes: 9 additions & 13 deletions idp/src/health/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,21 @@
// by the GNU Affero General Public License v3.0 only, included in the file
// AGPL-3.0-only in the root of this repository.
import { Request, Response, Router } from 'express'
import { Client as PgClient } from 'https://deno.land/x/postgres@v0.19.3/mod.ts'
import { getConfig } from '@/config/config.ts'
import { client as postgres } from '@/infra/postgres.ts'
import { client as meilisearch } from '@/infra/meilisearch.ts'

const router = Router()

router.get('', async (_: Request, res: Response) => {
let pg: PgClient | undefined
try {
pg = new PgClient(getConfig().databaseURL)
await pg.connect()
await pg.queryObject('SELECT 1')
res.send('OK')
} catch {
if (!postgres.connected) {
res.sendStatus(503)
} finally {
if (pg) {
await pg.end()
}
return
}
if (!(await meilisearch.isHealthy())) {
res.sendStatus(503)
return
}
res.send('OK')
})

export default router
4 changes: 1 addition & 3 deletions idp/src/infra/search.ts → idp/src/infra/meilisearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@ import { getConfig } from '@/config/config.ts'

export const USER_SEARCH_INDEX = 'user'

const client = new MeiliSearch({ host: getConfig().search.url })
export const client = new MeiliSearch({ host: getConfig().search.url })
client.createIndex(USER_SEARCH_INDEX, { primaryKey: 'id' }).then()

export default client
21 changes: 12 additions & 9 deletions idp/src/user/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ import { ErrorCode, newError } from '@/infra/error/core.ts'
import { newHyphenlessUuid } from '@/infra/id.ts'
import { sendTemplateMail } from '@/infra/mail.ts'
import { hashPassword, verifyPassword } from '@/infra/password.ts'
import search, { USER_SEARCH_INDEX } from '@/infra/search.ts'
import {
client as meilisearch,
USER_SEARCH_INDEX,
} from '../infra/meilisearch.ts'
import { User } from '@/user/model.ts'
import userRepo from '@/user/repo.ts'
import { Buffer } from 'node:buffer'
Expand Down Expand Up @@ -149,7 +152,7 @@ export async function list({
page,
}: UserListOptions): Promise<UserAdminList> {
if (query && query.length >= 3) {
const users = await search
const users = await meilisearch
.index(USER_SEARCH_INDEX)
.search(query, { page: page, hitsPerPage: size })
.then((value) => {
Expand Down Expand Up @@ -194,7 +197,7 @@ export async function updateFullName(
): Promise<UserDTO> {
let user = await userRepo.findById(id)
user = await userRepo.update({ id: user.id, fullName: options.fullName })
await search.index(USER_SEARCH_INDEX).updateDocuments([
await meilisearch.index(USER_SEARCH_INDEX).updateDocuments([
{
id: user.id,
username: user.username,
Expand Down Expand Up @@ -266,7 +269,7 @@ export async function updateEmailConfirmation(
emailUpdateToken: null,
emailUpdateValue: null,
})
await search.index(USER_SEARCH_INDEX).updateDocuments([
await meilisearch.index(USER_SEARCH_INDEX).updateDocuments([
{
id: user.id,
username: user.username,
Expand Down Expand Up @@ -308,7 +311,7 @@ export async function updatePicture(
id: userId,
picture: `data:${contentType};base64,${picture}`,
})
await search.index(USER_SEARCH_INDEX).updateDocuments([
await meilisearch.index(USER_SEARCH_INDEX).updateDocuments([
{
id: user.id,
username: user.username,
Expand All @@ -326,7 +329,7 @@ export async function updatePicture(
export async function deletePicture(id: string): Promise<UserDTO> {
let user = await userRepo.findById(id)
user = await userRepo.update({ id: user.id, picture: null })
await search.index(USER_SEARCH_INDEX).updateDocuments([
await meilisearch.index(USER_SEARCH_INDEX).updateDocuments([
{
id: user.id,
username: user.username,
Expand All @@ -345,7 +348,7 @@ export async function deleteUser(id: string, options: UserDeleteOptions) {
const user = await userRepo.findById(id)
if (verifyPassword(options.password, user.passwordHash)) {
await userRepo.delete(user.id)
await search.index(USER_SEARCH_INDEX).deleteDocuments([user.id])
await meilisearch.index(USER_SEARCH_INDEX).deleteDocuments([user.id])
} else {
throw newInvalidPasswordError()
}
Expand All @@ -362,7 +365,7 @@ export async function suspendUser(id: string, options: UserSuspendOptions) {
}
if (user) {
await userRepo.suspend(user.id, options.suspend)
await search.index(USER_SEARCH_INDEX).updateDocuments([
await meilisearch.index(USER_SEARCH_INDEX).updateDocuments([
{
id: user.id,
username: user.username,
Expand Down Expand Up @@ -390,7 +393,7 @@ export async function makeAdminUser(id: string, options: UserMakeAdminOptions) {
}
if (user) {
await userRepo.makeAdmin(user.id, options.makeAdmin)
await search.index(USER_SEARCH_INDEX).updateDocuments([
await meilisearch.index(USER_SEARCH_INDEX).updateDocuments([
{
id: user.id,
username: user.username,
Expand Down

0 comments on commit 76a2de4

Please sign in to comment.