Skip to content

Commit

Permalink
ปรับแต่งแสดง LOG และระบบ Search, แก้ไขเอกสาร
Browse files Browse the repository at this point in the history
  • Loading branch information
rushmi0 committed Nov 6, 2024
1 parent edc08af commit 8ae8e65
Show file tree
Hide file tree
Showing 18 changed files with 134 additions and 63 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ out/
.classpath
.factorypath
/test-results-native/test/TEST-junit-jupiter.xml
/logs/
5 changes: 4 additions & 1 deletion Dockerfile.jvm
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ COPY --chown=runner:appgroup .env /app/.env

EXPOSE 6724

ENTRYPOINT ["java", "-Xms256m", "-Xmx3g", "-XX:+UseG1GC", "-jar", "fenrir-s-1.0-all-optimized.jar"]
ENTRYPOINT sh -c "java -Xms256m -Xmx4g -XX:+UseG1GC -jar fenrir-s-1.0-all-optimized.jar; \
while [ ! -f logs/activity.log ]; do sleep 3; done; \
cd logs; tail -n 100 -f activity.log"

5 changes: 4 additions & 1 deletion Dockerfile.native
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ COPY --chown=runner:appgroup .env /app/.env

EXPOSE 6724

ENTRYPOINT ["/app/fenrir-s-v1.0"]
ENTRYPOINT sh -c "/app/fenrir-s-v1.0; \
while [ ! -f logs/activity.log ]; do sleep 3; done; \
cd logs; tail -n 100 -f activity.log"

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ git clone https://github.com/rushmi0/Fenrir-s.git
cd Fenrir-s
```

2. ปรับแต่งไฟล์ `application.toml` ตามต้องการ
2. ปรับแต่งไฟล์ `.env` ตามต้องการ

3. รัน Docker Compose:

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ micronaut {
optimizeClassLoading = true
deduceEnvironment = true
optimizeNetty = true
replaceLogbackXml = true
replaceLogbackXml = false
}
}

Expand Down
2 changes: 1 addition & 1 deletion doc/README-EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ git clone https://github.com/rushmi0/Fenrir-s.git
cd Fenrir-s
```

2. Customize the application.toml file as needed.
2. Customize the `.env` file as needed.

3. Run Docker Compose:

Expand Down
2 changes: 1 addition & 1 deletion doc/README-JP.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ git clone https://github.com/rushmi0/Fenrir-s.git
cd Fenrir-s
```

2. 適当に`application.toml`ファイルをカスタマイズする。
2. 適当に`.env`ファイルをカスタマイズする。

3. Docker Composeを実行する:

Expand Down
25 changes: 19 additions & 6 deletions script/http/client_req.http
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ accept: application/nostr+json

###

GET https://relay.nostr.band
GET https://relay.rushmi0.win/
accept: application/nostr+json

###

GET http://localhost:6725
GET http://localhost:6724
accept: application/nostr+json


Expand All @@ -29,7 +29,7 @@ Content-Type: application/json

###

WEBSOCKET ws://localhost:6724
WEBSOCKET ws://localhost:6725
Content-Type: application/json

["COUNT","hsZEOtaDsENYkP5H-JIWp",{"kinds": [1],"limit": 2}, {"kinds": [4],"limit": 150}]
Expand All @@ -43,7 +43,7 @@ Content-Type: application/json

###

WEBSOCKET ws://localhost:6724
WEBSOCKET ws://localhost:6725
Content-Type: application/json

["COUNT","hsZEOtaDsENYkP5H-JIWp",{"kinds": [0],"authors":["e4b2c64f0e4e54abb34d5624cd040e05ecc77f0c467cc46e2cc4d5be98abe3e3"]}]
Expand Down Expand Up @@ -230,13 +230,26 @@ Content-Type: application/json
["REQ","6Y92Rj5mPPvkyh9_pYBXD",{"#p":["e4b2c64f0e4e54abb34d5624cd040e05ecc77f0c467cc46e2cc4d5be98abe3e3"],"kinds":[1,6,16,7,9735,2004,30023],"limit":150}]

###
WEBSOCKET ws://localhost:6724
WEBSOCKET ws://localhost:6725
Content-Type: application/json

[
"REQ",
"Rj5mPPvkyh9",
{
"search" : "after"
"search" : "d8914525efb7030f8d1c86283b8a6313e4e6dfe573f2c1b5b130eaa5767d530e"
}
]

###

WEBSOCKET ws://localhost:6724
Content-Type: application/json

[
"COUNT",
"Rj5mPPvkyh9",
{
"search" : "dd653b86c26bdc8991cc9401b7ff8c7914c04b24348272a85871bebfd503491a"
}
]
21 changes: 10 additions & 11 deletions src/main/kotlin/org/fenrirs/relay/Gateway.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@ import io.micronaut.websocket.annotation.ServerWebSocket

import jakarta.inject.Inject

import org.fenrirs.relay.core.nip01.command.CLOSE
import org.fenrirs.relay.core.nip01.command.EVENT
import org.fenrirs.relay.core.nip01.command.CLOSE
import org.fenrirs.relay.core.nip01.command.COUNT
import org.fenrirs.relay.core.nip01.command.REQ
import org.fenrirs.relay.core.nip01.command.CommandFactory.parse
import org.fenrirs.relay.core.nip01.response.RelayResponse
import org.fenrirs.relay.core.nip01.ProtocolFlowFactory
import org.fenrirs.relay.core.nip01.command.COUNT
import org.fenrirs.relay.core.nip11.RelayInformation
import org.fenrirs.storage.Subscription.clearSession

import org.fenrirs.utils.Color.GREEN
import org.fenrirs.utils.Color.PURPLE
import org.fenrirs.utils.Color.RED
import org.fenrirs.utils.Color.RESET
import org.fenrirs.utils.Color.YELLOW

import org.slf4j.Logger
import org.slf4j.LoggerFactory
Expand All @@ -48,22 +49,20 @@ class Gateway @Inject constructor(

// ดึงข้อมูลของไคลเอนต์
val clientIp = request.headers["X-Forwarded-For"] ?: request.remoteAddress.address.hostAddress
val userAgent = request.headers["User-Agent"]
val userAgent = request.headers["User-Agent"] ?: "N/A"
val sessionId = session?.id ?: "N/A"

// Log ข้อมูลไคลเอนต์ที่เชื่อมต่อ
LOG.info("Client IP: $clientIp, Agent: $userAgent")
LOG.info("${YELLOW}Client IP: $clientIp, Session ID: $sessionId$RESET")
LOG.info("User Agent: $userAgent")

session?.let {
LOG.info("${GREEN}open$RESET ${session.id}")
LOG.info("${GREEN}* open$RESET ${session.id}")
return@let HttpResponse.ok("Session opened")
.header(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "*")
}

//LOG.info("${YELLOW}accept: $RESET$accept ${BLUE}session: $RESET${session?.id}")
val contentType = when {
accept == "application/nostr+json" -> MediaType.APPLICATION_JSON
else -> MediaType.TEXT_HTML
}
val contentType = if (accept == "application/nostr+json") MediaType.APPLICATION_JSON else MediaType.TEXT_HTML

return HttpResponse.ok(nip11.loadRelayInfo(contentType))
.contentType(contentType)
Expand Down Expand Up @@ -98,7 +97,7 @@ class Gateway @Inject constructor(

@OnClose
fun onClose(session: WebSocketSession) {
LOG.info("${PURPLE}close: ${RESET}$session")
LOG.info("${PURPLE}# close: ${RESET}$session")
clearSession(session)
}

Expand Down
30 changes: 9 additions & 21 deletions src/main/kotlin/org/fenrirs/relay/core/nip01/BasicProtocolFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import org.fenrirs.storage.Subscription.isSubscriptionActive
import org.fenrirs.storage.Subscription.saveSubscription
import org.fenrirs.storage.statement.StoredServiceImpl

import org.fenrirs.utils.Color.YELLOW
import org.fenrirs.utils.Color.GREEN
import org.fenrirs.utils.Color.PURPLE
import org.fenrirs.utils.Color.RED
Expand Down Expand Up @@ -56,7 +55,7 @@ class BasicProtocolFlow @Inject constructor(
RelayResponse.OK(event.id!!, false, warning).toClient(session)
}

// ดึงข้อมูล public key ของ relay owner และรายการ passList จาก src/main/resources/application.toml
// ดึงข้อมูล public key ของ relay owner และรายการ passList จาก .env
val relayOwner = env.RELAY_OWNER
val passList: List<String> = getPassList(relayOwner)
val followsPass: Boolean = env.FOLLOWS_PASS
Expand Down Expand Up @@ -167,7 +166,7 @@ class BasicProtocolFlow @Inject constructor(
val (success, message) = action.invoke()

if (success) {
LOG.info("Event kind: ${PURPLE}[${event.kind}] ${RESET}handled ${GREEN}saved")
LOG.info("Session ${session.id} handled ${GREEN}saved, ${RESET}Event ID: ${PURPLE}[${event.id}]${RESET}")
RelayResponse.OK(event.id!!, true, message).toClient(session)
} else {
LOG.warn("${RED}Failed ${RESET}to handle event: ${event.id}")
Expand Down Expand Up @@ -282,6 +281,9 @@ class BasicProtocolFlow @Inject constructor(
filtersX: List<FiltersX>,
session: WebSocketSession
) {
//LOG.info("${GREEN}filters ${YELLOW}[${filtersX.size}] ${RESET}req subscription ID: ${CYAN}$subscriptionId ${RESET}")
//LOG.info("FiltersX: $filtersX")

filtersX.forEach { filter ->
sqlExec.filterList(filter)?.forEach { event ->
RelayResponse.EVENT(subscriptionId, event).toClient(session)
Expand Down Expand Up @@ -351,20 +353,6 @@ class BasicProtocolFlow @Inject constructor(
}


/**
* ฟังก์ชัน handleResponse ใช้ในการส่งการตอบกลับไปยังไคลเอนต์ตามประเภทที่กำหนด
*
* @param subscriptionId ไอดีที่ใช้ในการติดตามหรืออ้างอิงการร้องขอนั้นๆ จากไคลเอนต์
* @param data ข้อมูลที่ต้องการส่งกลับ
* @param session เซสชัน WebSocket ที่ใช้ในการตอบกลับ
*/
private inline fun <reified T> handleResponse(subscriptionId: String, data: Any, session: WebSocketSession) {
when (T::class) {
COUNT::class -> RelayResponse.COUNT(subscriptionId, data).toClient(session)
REQ::class -> RelayResponse.EVENT(subscriptionId, data as Event).toClient(session)
}
}


/**
* ฟังก์ชัน startRealTimeUpdates ใช้ในการเริ่มต้นการอัปเดตข้อมูลแบบเรียลไทม์
Expand Down Expand Up @@ -394,11 +382,11 @@ class BasicProtocolFlow @Inject constructor(
when (T::class) {
COUNT::class -> {
val count = events.size
val response = if (count >= 93_412_452) ApproximateCountREQ(93_412_452, true) else CountREQ(count)
handleResponse<T>(subscriptionId, response, session)
val data = if (count >= 93_412_452) ApproximateCountREQ(93_412_452, true) else CountREQ(count)
RelayResponse.COUNT(subscriptionId, data).toClient(session)
}
REQ::class -> events.forEach { event ->
handleResponse<T>(subscriptionId, event, session)
RelayResponse.EVENT(subscriptionId, event).toClient(session)
}
}
}
Expand All @@ -420,7 +408,7 @@ class BasicProtocolFlow @Inject constructor(
* @param session เซสชัน WebSocket ที่ใช้ในการตอบกลับ
*/
fun onClose(subscriptionId: String, session: WebSocketSession) {
LOG.info("${YELLOW}cancel ${RESET}subscription ID: $subscriptionId")
//LOG.info("${YELLOW}cancel ${RESET}subscription ID: $subscriptionId")
RelayResponse.CANCEL(subscriptionId).toClient(session)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ object CommandFactory {

// ตรวจสอบจำนวน filters ว่าไม่เกินค่าที่กำหนด
if (filtersJson.size > env.MAX_FILTERS) {
throw IllegalArgumentException("rate-limited: max filters ${env.MAX_FILTERS} values in each sub ID allowed")
throw IllegalArgumentException("rate-limited: max filter ${env.MAX_FILTERS} values each sub ID allowed")
}

val data: Map<String, JsonElement> = filtersJson.flatMap { it.entries }.associate { it.key to it.value }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ sealed class RelayResponse<out T> {
clearSubscription(session, subscriptionId)
}
} catch (e: WebSocketSessionException) {
LOG.warn("$session is closed, cannot send message")
LOG.info("$session is closed, cannot send message")
}
}

else -> LOG.warn("$session is closed")
else -> LOG.info("$session is closed")
}
}

Expand Down
35 changes: 33 additions & 2 deletions src/main/kotlin/org/fenrirs/relay/core/nip50/SearchEngine.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package org.fenrirs.relay.core.nip50

import jakarta.inject.Singleton
import io.micronaut.context.annotation.Bean
import org.fenrirs.storage.table.EVENT.CONTENT
import org.fenrirs.storage.table.EVENT.EVENT_ID
import org.fenrirs.storage.table.EVENT.PUBKEY
import org.fenrirs.utils.Bech32
import org.fenrirs.utils.ShiftTo.toHex
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq


@Singleton
@Bean
class SearchEngine {


Expand Down Expand Up @@ -46,6 +52,7 @@ class SearchEngine {
*/
class MatchOp(expr1: Expression<*>, expr2: Expression<*>) : ComparisonOp(expr1, expr2, "@@")


/**
* ฟังก์ชันสำหรับสร้างการเปรียบเทียบ `tsvector` กับ `tsquery`
* - ใช้ตัวดำเนินการ `@@` สำหรับการค้นหาข้อความ
Expand All @@ -72,4 +79,28 @@ class SearchEngine {
}


/**
* ฟังก์ชันสำหรับตรวจสอบประเภทของ search query และเลือกคอลัมน์ที่เหมาะสมสำหรับการค้นหา
*
* - หาก search เริ่มต้นด้วย "npub" จะทำการถอดรหัสเป็นค่า hex เพื่อค้นหาในคอลัมน์ PUBKEY
* - หาก search เป็น hex string ความยาว 64 ตัวอักษร จะค้นหาในคอลัมน์ EVENT_ID หรือ PUBKEY
* - กรณีอื่นๆ จะค้นหาในคอลัมน์ CONTENT โดยใช้ tsQuery
*
* @param search ข้อความที่ต้องการค้นหา
* @return Op<Boolean> เงื่อนไขการค้นหาสำหรับ query
*/
fun searchQuery(search: String): Op<Boolean> {
return when {
search.startsWith("npub") -> {
val decodedHex = Bech32.decode(search).data.toHex()
PUBKEY eq decodedHex
}
search.length == 64 && search.all { it in '0'..'9' || it in 'a'..'f' } -> {
(EVENT_ID eq search) or (PUBKEY eq search)
}
else -> tsQuery(CONTENT, search)
}
}


}
4 changes: 2 additions & 2 deletions src/main/kotlin/org/fenrirs/storage/DatabaseFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ object DatabaseFactory {
username = ENV.DATABASE_USERNAME
password = ENV.DATABASE_PASSWORD

minimumIdle = 2
maximumPoolSize = 10
minimumIdle = 10
maximumPoolSize = 64

isAutoCommit = false

Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/org/fenrirs/storage/Subscription.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package org.fenrirs.storage

import io.github.reactivecircus.cache4k.Cache
import io.micronaut.context.annotation.Bean
import io.micronaut.context.annotation.Factory
import io.micronaut.websocket.WebSocketSession

import org.fenrirs.relay.core.nip01.SubscriptionData
import org.fenrirs.relay.policy.FiltersX
import org.slf4j.LoggerFactory

//import org.slf4j.LoggerFactory

@Bean
@Factory
object Subscription {

private val config: Cache<String, SubscriptionData> = Cache.Builder<String, SubscriptionData>()
Expand Down Expand Up @@ -84,7 +84,7 @@ object Subscription {
if (!session.isOpen) {
// ถ้า session ถูกปิดไปแล้ว ให้ล้างข้อมูล session นั้นออก
clearSession(session)
LOG.info("Clear Session: $session")
//LOG.info("${PURPLE}clear: $RESET$session")
return false
}
//LOG.info("session: $session")
Expand Down Expand Up @@ -114,6 +114,6 @@ object Subscription {
fun clearSession(session: WebSocketSession) = remove(session.id)


private val LOG = LoggerFactory.getLogger(Subscription::class.java)
//private val LOG = LoggerFactory.getLogger(Subscription::class.java)

}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class StoredServiceImpl @Inject constructor(
// ถ้ามีการระบุ search ใน filters ให้เพิ่มเงื่อนไขการค้นหา CONTENT ที่ตรงกับ search ที่กำหนดโดยใช้ full-text search
filters.search?.let {
query.andWhere {
ts.tsQuery(CONTENT, filters.search)
ts.searchQuery(filters.search)
}
}

Expand Down
Loading

0 comments on commit 8ae8e65

Please sign in to comment.