Skip to content

Commit

Permalink
add domain and hash content
Browse files Browse the repository at this point in the history
  • Loading branch information
sunwei committed Dec 7, 2024
1 parent 239ad4e commit 1e177f5
Show file tree
Hide file tree
Showing 20 changed files with 345 additions and 117 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ hugov

/dddplayer/
/public/

tmp
release-notes.md
3 changes: 3 additions & 0 deletions ADRs/notes/mvp.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,6 @@ curl -X POST "http://127.0.0.1:1314/api/deploy?type=Site&id=2" \

curl -X GET "http://127.0.0.1:1314/api/search?type=SiteDeployment&q=slug:site2" \
-H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOm51bGwsImV4cCI6IjIwMjQtMTItMDRUMDg6MTQ6NTIuNTk2MDI5KzA4OjAwIiwiaWF0IjpudWxsLCJpc3MiOm51bGwsImp0aSI6bnVsbCwibmJmIjpudWxsLCJzdWIiOm51bGwsInVzZXIiOiJtZUBzdW53ZWkueHl6In0.foManZwcdG0h52dCxeKY6jE6iTkdSZFcEbnGFanLZU0"

curl -X GET "http://127.0.0.1:1314/api/search2?type=Language" \
-H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOm51bGwsImV4cCI6IjIwMjUtMDEtMDZUMDc6MTk6MzcuNDI2ODU4KzA4OjAwIiwiaWF0IjpudWxsLCJpc3MiOm51bGwsImp0aSI6bnVsbCwibmJmIjpudWxsLCJzdWIiOm51bGwsInVzZXIiOiJtZUBzdW53ZWkueHl6In0.wVsqhQc2G1n2berEIovbitzxftYWLPdq7x6rSwgCZ5c"
13 changes: 7 additions & 6 deletions internal/application/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ type netlifyConfig struct {
LogFormat string `default:"text"`
}

func DeployToNetlify(target string, deployment *valueobject.SiteDeployment, token string) error {
func DeployToNetlify(target string, deployment *valueobject.Deployment, domain *valueobject.Domain, token string) error {
c := &netlifyConfig{
AuthToken: token,
SiteID: deployment.NetlifySiteID,
SiteID: deployment.SiteID,
Directory: path.Join(target, "public"),
Draft: false,
DeployMessage: "Deployed by MDFriday",
Expand All @@ -55,16 +55,16 @@ func DeployToNetlify(target string, deployment *valueobject.SiteDeployment, toke
client := setupNetlifyClient()
ctx := setupContext(c, logger)

fmt.Println("Deploying to Netlify...", deployment.Netlify, deployment.Domain)
fmt.Println("Deploying to Netlify...", deployment.SiteName, domain.FullDomain())

// 检查 SiteID 是否为空
if c.SiteID == "" {
// 创建新 Netlify 站点
newSite, err := client.CreateSite(ctx, &models.SiteSetup{
Site: models.Site{
//AccountSlug: "admin-zbpioce",
Name: deployment.Netlify,
CustomDomain: fmt.Sprintf("%s.app.mdfriday.com", deployment.Domain),
Name: deployment.SiteName,
CustomDomain: domain.FullDomain(),
Ssl: true,
},
SiteSetupAllOf1: models.SiteSetupAllOf1{},
Expand All @@ -76,7 +76,8 @@ func DeployToNetlify(target string, deployment *valueobject.SiteDeployment, toke

// 更新 SiteID
c.SiteID = newSite.ID
deployment.NetlifySiteID = newSite.ID
deployment.SiteID = newSite.ID
deployment.Status = "deploying"
logger.Println("Created new site with ID: " + c.SiteID)
}

Expand Down
7 changes: 6 additions & 1 deletion internal/domain/content/entity/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ func (c *Content) UpdateContentObject(ci any) error {
}
status := cis.ItemStatus()

cih, ok := ci.(content.Hashable)
if ok {
cih.SetHash()
}

cii, ok := ci.(content.Identifiable)
if ok {
go func() {
Expand Down Expand Up @@ -226,5 +231,5 @@ func (c *Content) Unmarshal(data []byte, content any) error {
}

func (c *Content) NormalizeString(s string) (string, error) {
return stringToSlug(s)
return valueobject.StringToSlug(s)
}
8 changes: 7 additions & 1 deletion internal/domain/content/entity/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (c *Content) newContent(contentType string, ci any) (string, error) {
return "", errors.New("content type does not implement Identifiable")
}

slug, err := Slug(cii)
slug, err := valueobject.Slug(cii)
if err != nil {
return "", err
}
Expand All @@ -80,6 +80,12 @@ func (c *Content) newContent(contentType string, ci any) (string, error) {
return "", errors.New("content type does not implement Statusable")
}

cih, ok := ci.(content.Hashable)
if ok {
cih.SetHash()
fmt.Println("--==--", cih.ItemHash())
}

b, err := c.Marshal(ci)
if err != nil {
return "", err
Expand Down
51 changes: 20 additions & 31 deletions internal/domain/content/entity/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,48 @@ package entity

import (
"encoding/json"
"errors"
"fmt"
"github.com/gohugonet/hugoverse/internal/domain/content/valueobject"
"time"
)

func (c *Content) GetDeployment(siteId string, domain *valueobject.Domain) (*valueobject.SiteDeployment, error) {
content, err := c.getContent("Site", siteId)
func (c *Content) GetDeployment(domain *valueobject.Domain, hostName string) (*valueobject.Deployment, error) {
sd, err := c.searchDeployment(domain.QueryString(), hostName)
if err != nil {
return nil, err
}

if site, ok := content.(*valueobject.Site); ok {
sd, err := c.searchDeployment(domain.Root, domain.Sub, domain.Owner)
if sd == nil {
item, err := valueobject.NewItemWithNamespace("Deployment")
if err != nil {
return nil, err
}

if sd == nil {
item, err := valueobject.NewItemWithNamespace("SiteDeployment")
if err != nil {
return nil, err
}

sd = &valueobject.SiteDeployment{
Item: *item,
Site: site.QueryString(),
Netlify: fmt.Sprintf("mdf-%d-%s", time.Now().UnixMilli(), site.ItemSlug()),
Domain: site.ItemSlug(),
Status: "Not Started",
}

_, err = c.newContent("SiteDeployment", sd)
if err != nil {
return nil, err
}
sd = &valueobject.Deployment{
Item: *item,
Domain: domain.QueryString(),
SiteName: fmt.Sprintf("mdf-%d", time.Now().UnixMilli()),
HostName: hostName,
Status: "pending",
}

return sd, nil
_, err = c.newContent("Deployment", sd)
if err != nil {
return nil, err
}
}

return nil, errors.New("only site could be deployed")
return sd, nil
}

func (c *Content) searchDeployment(root string, sub string, owner string) (*valueobject.SiteDeployment, error) {
func (c *Content) searchDeployment(domainQueryStr string, hostName string) (*valueobject.Deployment, error) {
conditions := map[string]string{
"root": root,
"domain": sub,
"owner": owner,
"domain": domainQueryStr,
"host_name": hostName,
}

// 查询域名信息
deploys, err := c.termSearch("SiteDeployment", conditions)
deploys, err := c.termSearch("Deployment", conditions)
if err != nil {
return nil, err
}
Expand All @@ -66,7 +55,7 @@ func (c *Content) searchDeployment(root string, sub string, owner string) (*valu

// 遍历查询结果并解析
for _, data := range deploys {
var deployment valueobject.SiteDeployment
var deployment valueobject.Deployment
if err := json.Unmarshal(data, &deployment); err != nil {
return nil, err
}
Expand Down
36 changes: 22 additions & 14 deletions internal/domain/content/entity/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,38 @@ package entity
import (
"encoding/json"
"errors"
"fmt"
"github.com/gohugonet/hugoverse/internal/domain/content/valueobject"
)

func (c *Content) ApplyDomain(siteId string, domain string) (*valueobject.Domain, error) {
func (c *Content) ApplyDomain(siteId string, domain string) (*valueobject.Domain, bool, error) {
site, err := c.getContent("Site", siteId)
if err != nil {
return nil, err
return nil, false, err
}

if site, ok := site.(*valueobject.Site); ok {
slug, err := Slug(site)
slug, err := valueobject.Slug(site)
fmt.Println("-------- slug", slug, domain)
if err != nil {
return nil, err
return nil, false, err
}

sd, err := c.searchDomain(domain, slug, site.Owner)
sd, err := c.searchDomain(domain, slug)
if err != nil {
return nil, err
return nil, false, err
}

fmt.Println("-------- sd", sd)

if sd != nil && sd.Owner != site.Owner {
return nil, true, errors.New(fmt.Sprintf("domain %s already exists", sd.String()))
}

if sd == nil {
item, err := valueobject.NewItemWithNamespace("Domain")
if err != nil {
return nil, err
return nil, false, err
}

sd = &valueobject.Domain{
Expand All @@ -38,22 +46,20 @@ func (c *Content) ApplyDomain(siteId string, domain string) (*valueobject.Domain

_, err = c.newContent("Domain", sd)
if err != nil {
return nil, err
return nil, false, err
}
}

return sd, nil
return sd, false, nil
}

return nil, errors.New("only site could be deployed with domain")
return nil, false, errors.New("only site could be deployed with domain")
}

func (c *Content) searchDomain(root string, sub string, owner string) (*valueobject.Domain, error) {
func (c *Content) searchDomain(root string, sub string) (*valueobject.Domain, error) {
// 构建精确匹配的查询条件
conditions := map[string]string{
"root": root,
"sub": sub,
"owner": owner,
"hash": valueobject.Hash([]string{sub, root}),
}

// 查询域名信息
Expand All @@ -67,6 +73,8 @@ func (c *Content) searchDomain(root string, sub string, owner string) (*valueobj
return nil, nil
}

fmt.Println("-------- domains", len(domains))

// 遍历查询结果并解析
for _, data := range domains {
var domain valueobject.Domain
Expand Down
16 changes: 13 additions & 3 deletions internal/domain/content/entity/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"github.com/blevesearch/bleve"
"github.com/blevesearch/bleve/search/query"
"github.com/gohugonet/hugoverse/internal/domain/content"
"github.com/gohugonet/hugoverse/internal/domain/content/repository"
"github.com/gohugonet/hugoverse/internal/domain/content/valueobject"
Expand Down Expand Up @@ -192,20 +193,23 @@ func (s *Search) TermQuery(typeName string, keyValues map[string]string, count,
return nil, content.ErrNoIndex
}

s.Log.Debugln("KeyValueQuery KeyValues: ", keyValues)
fmt.Println("KeyValueQuery KeyValues: ", keyValues)

// 创建每个 Key-Value 的查询
var termQueries []bleve.Query
var termQueries []query.Query
for key, value := range keyValues {
tq := bleve.NewTermQuery(value)
tq.SetField(key)

fmt.Printf("TermQuery 22 : %+v", tq)

termQueries = append(termQueries, tq)
}

// 将查询组合成一个 ConjunctionQuery
finalQuery := bleve.NewConjunctionQuery(termQueries...)

s.Log.Debugf("TermQuery: %+v", finalQuery)

// 创建搜索请求
req := bleve.NewSearchRequestOptions(finalQuery, count, offset, false)

Expand Down Expand Up @@ -246,6 +250,12 @@ func (s *Search) UpdateIndex(ns, id string, data []byte) error {

// add data to search index
i := valueobject.NewIndex(ns, id)

l, ok := p.(*valueobject.Language)
if ok {
fmt.Println("indexing ppp...:", l, l.Name, l.Code)
}

err = idx.Index(i.String(), p)

return err
Expand Down
2 changes: 1 addition & 1 deletion internal/domain/content/factory/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func prepareUserTypes(c *entity.Content) {
c.UserTypes["SiteLanguage"] = func() interface{} { return new(valueobject.SiteLanguage) }
c.UserTypes["SitePost"] = func() interface{} { return new(valueobject.SitePost) }
c.UserTypes["SiteResource"] = func() interface{} { return new(valueobject.SiteResource) }
c.UserTypes["SiteDeployment"] = func() interface{} { return new(valueobject.SiteDeployment) }
c.UserTypes["Deployment"] = func() interface{} { return new(valueobject.Deployment) }
}

func prepareAdminTypes(c *entity.Content) {
Expand Down
5 changes: 5 additions & 0 deletions internal/domain/content/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ type Sluggable interface {
ItemSlug() string
}

type Hashable interface {
SetHash()
ItemHash() string
}

// Searchable ...
type Searchable interface {
SearchMapping() (*mapping.IndexMappingImpl, error)
Expand Down
Loading

0 comments on commit 1e177f5

Please sign in to comment.