Skip to content

Commit

Permalink
add hugo project load to api
Browse files Browse the repository at this point in the history
  • Loading branch information
sunwei committed Sep 28, 2024
1 parent 385e847 commit b652e11
Show file tree
Hide file tree
Showing 33 changed files with 1,263 additions and 285 deletions.
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ Hugo headless CMS server

- Install Dart Sass

## TODO for PoC of book theme
## PoC

- [x] DDD Ponzu to expose API
- [x] DDD Hugo to get content info
- [x] DDD Hugo to build site
- [x] Load Hugo project from file system
- [ ] Manage Post through API
- [ ] Deploy Site through API

## MVP

- [ ] CLI JWT token
- [ ] Test Coverage

- [x] Template
- [x] .Page.Pages
- [x] .Site.Pages
- [x] Multiple Language
- [x] Git info

## Next

Expand Down
8 changes: 8 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ func New() error {
if err := staticCmd.Run(); err != nil {
return err
}
case "load":
loadCmd, err := cli.NewLoadCmd(topLevel)
if err != nil {
return err
}
if err := loadCmd.Run(); err != nil {
return err
}

default:
topLevel.Usage()
Expand Down
96 changes: 90 additions & 6 deletions internal/application/content.go
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
}
63 changes: 5 additions & 58 deletions internal/application/demo.go
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
}
73 changes: 73 additions & 0 deletions internal/application/dir.go
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
}
10 changes: 10 additions & 0 deletions internal/domain/config/entity/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,13 @@ func (l *Language) GetLanguageName(lang string) string {

return ""
}

func (l *Language) GetLanguageFolder(lang string) string {
for c, v := range l.RootConfigs {
if c == lang {
return v.ContentDir
}
}

return ""
}
Loading

0 comments on commit b652e11

Please sign in to comment.