mirror of
https://github.com/studiokaiji/nostr-webhost.git
synced 2025-12-17 06:44:28 +01:00
通常イベントをneventでアクセスできるようにした
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/nbd-wtf/go-nostr"
|
"github.com/nbd-wtf/go-nostr"
|
||||||
|
"github.com/nbd-wtf/go-nostr/nip19"
|
||||||
"github.com/studiokaiji/nostr-webhost/hostr/cmd/consts"
|
"github.com/studiokaiji/nostr-webhost/hostr/cmd/consts"
|
||||||
"github.com/studiokaiji/nostr-webhost/hostr/cmd/keystore"
|
"github.com/studiokaiji/nostr-webhost/hostr/cmd/keystore"
|
||||||
"github.com/studiokaiji/nostr-webhost/hostr/cmd/relays"
|
"github.com/studiokaiji/nostr-webhost/hostr/cmd/relays"
|
||||||
@@ -61,17 +62,13 @@ func addNostrEventQueue(event *nostr.Event) {
|
|||||||
nostrEventsQueue = append(nostrEventsQueue, event)
|
nostrEventsQueue = append(nostrEventsQueue, event)
|
||||||
}
|
}
|
||||||
|
|
||||||
func publishEventsFromQueue() (string, error) {
|
var allRelays []string
|
||||||
|
|
||||||
|
func publishEventsFromQueue(replaceable bool) (string, string) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
fmt.Println("Publishing...")
|
fmt.Println("Publishing...")
|
||||||
|
|
||||||
// リレーを取得
|
|
||||||
allRelays, err := relays.GetAllRelays()
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 各リレーに接続
|
// 各リレーに接続
|
||||||
var relays []*nostr.Relay
|
var relays []*nostr.Relay
|
||||||
|
|
||||||
@@ -122,9 +119,18 @@ func publishEventsFromQueue() (string, error) {
|
|||||||
fmt.Println("Failed to deploy", allEventsCount-uploadedFilesCount, "files.")
|
fmt.Println("Failed to deploy", allEventsCount-uploadedFilesCount, "files.")
|
||||||
}
|
}
|
||||||
|
|
||||||
indexEventId := nostrEventsQueue[len(nostrEventsQueue)-1].ID
|
indexEvent := nostrEventsQueue[len(nostrEventsQueue)-1]
|
||||||
|
|
||||||
return indexEventId, err
|
encoded := ""
|
||||||
|
if !replaceable {
|
||||||
|
if enc, err := nip19.EncodeEvent(indexEvent.ID, allRelays, indexEvent.PubKey); err == nil {
|
||||||
|
encoded = enc
|
||||||
|
} else {
|
||||||
|
fmt.Println("❌ Failed to covert nevent:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return indexEvent.ID, encoded
|
||||||
}
|
}
|
||||||
|
|
||||||
func isExternalURL(urlStr string) bool {
|
func isExternalURL(urlStr string) bool {
|
||||||
@@ -136,7 +142,7 @@ func isValidFileType(str string) bool {
|
|||||||
return strings.HasSuffix(str, ".html") || strings.HasSuffix(str, ".css") || strings.HasSuffix(str, ".js")
|
return strings.HasSuffix(str, ".html") || strings.HasSuffix(str, ".css") || strings.HasSuffix(str, ".js")
|
||||||
}
|
}
|
||||||
|
|
||||||
func Deploy(basePath string, replaceable bool, htmlIdentifier string) (string, error) {
|
func Deploy(basePath string, replaceable bool, htmlIdentifier string) (string, string, error) {
|
||||||
// 引数からデプロイしたいサイトのパスを受け取る。
|
// 引数からデプロイしたいサイトのパスを受け取る。
|
||||||
filePath := filepath.Join(basePath, "index.html")
|
filePath := filepath.Join(basePath, "index.html")
|
||||||
|
|
||||||
@@ -144,26 +150,26 @@ func Deploy(basePath string, replaceable bool, htmlIdentifier string) (string, e
|
|||||||
content, err := os.ReadFile(filePath)
|
content, err := os.ReadFile(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("❌ Failed to read index.html:", err)
|
fmt.Println("❌ Failed to read index.html:", err)
|
||||||
return "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTMLの解析
|
// HTMLの解析
|
||||||
doc, err := html.Parse(bytes.NewReader(content))
|
doc, err := html.Parse(bytes.NewReader(content))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("❌ Failed to parse index.html:", err)
|
fmt.Println("❌ Failed to parse index.html:", err)
|
||||||
return "", nil
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Eventの取得に必要になるキーペアを取得
|
// Eventの取得に必要になるキーペアを取得
|
||||||
priKey, err := keystore.GetSecret()
|
priKey, err := keystore.GetSecret()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("❌ Failed to get private key:", err)
|
fmt.Println("❌ Failed to get private key:", err)
|
||||||
return "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
pubKey, err := nostr.GetPublicKey(priKey)
|
pubKey, err := nostr.GetPublicKey(priKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("❌ Failed to get public key:", err)
|
fmt.Println("❌ Failed to get public key:", err)
|
||||||
return "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
// htmlIdentifierの存在チェック
|
// htmlIdentifierの存在チェック
|
||||||
@@ -179,6 +185,12 @@ func Deploy(basePath string, replaceable bool, htmlIdentifier string) (string, e
|
|||||||
fmt.Printf("Identifier: %s\n", htmlIdentifier)
|
fmt.Printf("Identifier: %s\n", htmlIdentifier)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// リレーを取得
|
||||||
|
allRelays, err = relays.GetAllRelays()
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
|
||||||
// リンクの解析と変換
|
// リンクの解析と変換
|
||||||
convertLinks(priKey, pubKey, basePath, replaceable, htmlIdentifier, doc)
|
convertLinks(priKey, pubKey, basePath, replaceable, htmlIdentifier, doc)
|
||||||
|
|
||||||
@@ -204,12 +216,14 @@ func Deploy(basePath string, replaceable bool, htmlIdentifier string) (string, e
|
|||||||
event, err := getEvent(priKey, pubKey, strHtml, indexHtmlKind, tags)
|
event, err := getEvent(priKey, pubKey, strHtml, indexHtmlKind, tags)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("❌ Failed to get public key:", err)
|
fmt.Println("❌ Failed to get public key:", err)
|
||||||
return "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
addNostrEventQueue(event)
|
addNostrEventQueue(event)
|
||||||
fmt.Println("Added", filePath, "event to publish queue")
|
fmt.Println("Added", filePath, "event to publish queue")
|
||||||
|
|
||||||
return publishEventsFromQueue()
|
eventId, encoded := publishEventsFromQueue(replaceable)
|
||||||
|
|
||||||
|
return eventId, encoded, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func convertLinks(priKey, pubKey, basePath string, replaceable bool, indexHtmlIdentifier string, n *html.Node) {
|
func convertLinks(priKey, pubKey, basePath string, replaceable bool, indexHtmlIdentifier string, n *html.Node) {
|
||||||
@@ -253,9 +267,15 @@ func convertLinks(priKey, pubKey, basePath string, replaceable bool, indexHtmlId
|
|||||||
addNostrEventQueue(event)
|
addNostrEventQueue(event)
|
||||||
fmt.Println("Added", filePath, "event to publish queue")
|
fmt.Println("Added", filePath, "event to publish queue")
|
||||||
|
|
||||||
|
// 置き換え可能なイベントでない場合
|
||||||
if !replaceable {
|
if !replaceable {
|
||||||
// 元のパスをEvent[.]IDに変更
|
// neventを指定
|
||||||
n.Attr[i].Val = event.ID
|
nevent, err := nip19.EncodeEvent(event.ID, allRelays, pubKey)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("❌ Failed to encode event", filePath, ":", err)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
n.Attr[i].Val = nevent
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,14 +24,32 @@ func Start(port string) {
|
|||||||
|
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
r.GET("/e/:idHex", func(ctx *gin.Context) {
|
r.GET("/e/:hex_or_nevent", func(ctx *gin.Context) {
|
||||||
// IDを取得
|
hexOrNevent := ctx.Param("hex_or_nevent")
|
||||||
id := ctx.Param("idHex")
|
|
||||||
|
ids := []string{}
|
||||||
|
|
||||||
|
// neventからIDを取得
|
||||||
|
if hexOrNevent[0:6] == "nevent" {
|
||||||
|
_, res, err := nip19.Decode(hexOrNevent)
|
||||||
|
if err != nil {
|
||||||
|
ctx.String(http.StatusBadRequest, "Invalid nevent")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data, ok := res.(nostr.EventPointer)
|
||||||
|
if !ok {
|
||||||
|
ctx.String(http.StatusBadRequest, "Failed to decode nevent")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ids = append(ids, data.ID)
|
||||||
|
}
|
||||||
|
|
||||||
// Poolからデータを取得する
|
// Poolからデータを取得する
|
||||||
ev := pool.QuerySingle(ctx, allRelays, nostr.Filter{
|
ev := pool.QuerySingle(ctx, allRelays, nostr.Filter{
|
||||||
Kinds: []int{consts.KindWebhostHTML, consts.KindWebhostCSS, consts.KindWebhostJS, consts.KindWebhostPicture},
|
Kinds: []int{consts.KindWebhostHTML, consts.KindWebhostCSS, consts.KindWebhostJS, consts.KindWebhostPicture},
|
||||||
IDs: []string{id},
|
IDs: ids,
|
||||||
})
|
})
|
||||||
if ev != nil {
|
if ev != nil {
|
||||||
switch ev.Kind {
|
switch ev.Kind {
|
||||||
|
|||||||
@@ -52,10 +52,19 @@ func main() {
|
|||||||
replaceable := ctx.Bool("replaceable")
|
replaceable := ctx.Bool("replaceable")
|
||||||
dTag := ctx.String("identifier")
|
dTag := ctx.String("identifier")
|
||||||
|
|
||||||
indexEventId, err := deploy.Deploy(path, replaceable, dTag)
|
id, encoded, err := deploy.Deploy(path, replaceable, dTag)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
fmt.Println("🌐 Deploy Complete!")
|
fmt.Println("🌐 Deploy Complete!")
|
||||||
fmt.Println("🌐 index.html Event ID:", indexEventId)
|
fmt.Println("index.html:")
|
||||||
|
fmt.Println(" - event.id:", id)
|
||||||
|
|
||||||
|
label := " - "
|
||||||
|
if replaceable {
|
||||||
|
label += "naddr"
|
||||||
|
} else {
|
||||||
|
label += "nevent"
|
||||||
|
}
|
||||||
|
fmt.Printf("%s: %s\n", label, encoded)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user