-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
1,263 additions
and
285 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,101 @@ | ||
package application | ||
|
||
import ( | ||
"github.com/gohugonet/hugoverse/internal/domain/content" | ||
"fmt" | ||
configFact "github.com/gohugonet/hugoverse/internal/domain/config/factory" | ||
"github.com/gohugonet/hugoverse/internal/domain/content/entity" | ||
"github.com/gohugonet/hugoverse/internal/domain/content/factory" | ||
"github.com/gohugonet/hugoverse/internal/domain/content/repository" | ||
contentHubFact "github.com/gohugonet/hugoverse/internal/domain/contenthub/factory" | ||
fsFact "github.com/gohugonet/hugoverse/internal/domain/fs/factory" | ||
mdFact "github.com/gohugonet/hugoverse/internal/domain/markdown/factory" | ||
moduleFact "github.com/gohugonet/hugoverse/internal/domain/module/factory" | ||
rsFact "github.com/gohugonet/hugoverse/internal/domain/resources/factory" | ||
siteFact "github.com/gohugonet/hugoverse/internal/domain/site/factory" | ||
tmplFact "github.com/gohugonet/hugoverse/internal/domain/template/factory" | ||
"github.com/gohugonet/hugoverse/internal/interfaces/api/database" | ||
"time" | ||
) | ||
|
||
type ContentServer struct { | ||
content.Content | ||
func NewContentServer(db repository.Repository) *entity.Content { | ||
return factory.NewContent(db, SearchDir()) | ||
} | ||
|
||
func NewContentServer(db repository.Repository) *ContentServer { | ||
return &ContentServer{ | ||
Content: factory.NewContent(db), | ||
func LoadHugoProject() error { | ||
c, err := configFact.LoadConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
mods, err := moduleFact.New(c) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
sfs, err := fsFact.New(c, mods) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ch, err := contentHubFact.New(&chServices{ | ||
Config: c, | ||
Fs: sfs, | ||
Module: mods, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ws := &resourcesWorkspaceProvider{ | ||
Config: c, | ||
Fs: sfs, | ||
} | ||
resources, err := rsFact.NewResources(ws) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
s := siteFact.New(&siteServices{ | ||
Config: c, | ||
Fs: sfs, | ||
ContentHub: ch, | ||
Resources: resources, | ||
}) | ||
|
||
exec, err := tmplFact.New(sfs, &templateCustomizedFunctionsProvider{ | ||
Markdown: mdFact.NewMarkdown(), | ||
ContentHub: ch, | ||
Site: s, | ||
Resources: resources, | ||
Config: c, | ||
Fs: sfs, | ||
}) | ||
|
||
resources.SetupTemplateClient(exec) // Expose template service to resources operations | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := ch.ProcessPages(exec); err != nil { | ||
return err | ||
} | ||
|
||
db := database.New(DataDir()) | ||
ct := factory.NewContentWithServices(db, SearchDir(), &siteServices{ | ||
Config: c, | ||
Fs: sfs, | ||
ContentHub: ch, | ||
}) | ||
|
||
db.Start(ct.AllContentTypeNames()) | ||
defer db.Close() | ||
|
||
err = ct.LoadHugoProject() | ||
|
||
fmt.Printf("sorting...") | ||
time.Sleep(3 * time.Second) | ||
fmt.Println(" done.") | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,16 @@ | ||
package application | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/spf13/afero" | ||
"golang.org/x/tools/txtar" | ||
"path/filepath" | ||
"github.com/gohugonet/hugoverse/pkg/testkit" | ||
) | ||
|
||
func NewDemo() (string, error) { | ||
var demoOs = &afero.OsFs{} | ||
tempDir, clean, err := CreateTempDir(demoOs, "hugoverse-temp-dir") | ||
if err != nil { | ||
clean() | ||
return "", err | ||
} | ||
|
||
var afs afero.Fs | ||
afs = afero.NewOsFs() | ||
prepareFS(tempDir, afs) | ||
|
||
return tempDir, nil | ||
} | ||
tmpDir, _, err := testkit.MkBookSite() | ||
//defer clean() | ||
|
||
// CreateTempDir creates a temp dir in the given filesystem and | ||
// returns the dirnam and a func that removes it when done. | ||
func CreateTempDir(fs afero.Fs, prefix string) (string, func(), error) { | ||
tempDir, err := afero.TempDir(fs, "", prefix) | ||
if err != nil { | ||
return "", nil, err | ||
return "", err | ||
} | ||
|
||
return tempDir, func() { fs.RemoveAll(tempDir) }, nil | ||
} | ||
|
||
func prepareFS(workingDir string, afs afero.Fs) { | ||
files := ` | ||
-- config.toml -- | ||
theme = "mytheme" | ||
contentDir = "mycontent" | ||
-- myproject.txt -- | ||
Hello project! | ||
-- themes/mytheme/mytheme.txt -- | ||
Hello theme! | ||
-- mycontent/blog/post.md -- | ||
### first blog | ||
Hello Blog | ||
-- layouts/index.html -- | ||
<p><!-- HTML comment -->abc</p> | ||
{{.Content}} | ||
-- layouts/_default/single.html -- | ||
<p>hello single page</p> | ||
{{.Content}}` | ||
data := txtar.Parse([]byte(files)) | ||
for _, f := range data.Files { | ||
filename := filepath.Join(workingDir, f.Name) | ||
data := bytes.TrimSuffix(f.Data, []byte("\n")) | ||
|
||
err := afs.MkdirAll(filepath.Dir(filename), 0777) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
err = afero.WriteFile(afs, filename, data, 0666) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
return tmpDir, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package application | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
var cachedHugoverseDir string | ||
|
||
func init() { | ||
cachedHugoverseDir = hugoverseDir() | ||
|
||
err := ensureDirExists(cachedHugoverseDir) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
} | ||
|
||
func TLSDir() string { | ||
return filepath.Join(DataDir(), "tls") | ||
} | ||
|
||
func UploadDir() string { | ||
return filepath.Join(DataDir(), "uploads") | ||
} | ||
|
||
func SearchDir() string { | ||
return filepath.Join(DataDir(), "search") | ||
} | ||
|
||
func DataDir() string { | ||
return cachedHugoverseDir | ||
} | ||
|
||
func hugoverseDir() string { | ||
homeDir, err := os.UserHomeDir() | ||
if err != nil { | ||
fmt.Println("Error getting home directory:", err, "using current directory as working directory") | ||
|
||
return getWd() | ||
} | ||
|
||
// 构建目录路径 ~/.local/share/hugoverse | ||
hugoverseDir := filepath.Join(homeDir, ".local", "share", "hugoverse") | ||
|
||
return hugoverseDir | ||
} | ||
|
||
func getWd() string { | ||
wd, err := os.Getwd() | ||
if err != nil { | ||
log.Fatalln("Couldn't find working directory", err) | ||
} | ||
return wd | ||
} | ||
|
||
func ensureDirExists(dir string) error { | ||
// 使用 os.Stat 检查目录是否存在 | ||
if _, err := os.Stat(dir); os.IsNotExist(err) { | ||
// 目录不存在,使用 os.MkdirAll 创建目录 | ||
err := os.MkdirAll(dir, 0755) | ||
if err != nil { | ||
return fmt.Errorf("failed to create directory: %w", err) | ||
} | ||
fmt.Println("Directory created:", dir) | ||
} else if err != nil { | ||
// 其他错误 | ||
return fmt.Errorf("failed to check directory: %w", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.