Skip to content

Commit

Permalink
customize deploy domains
Browse files Browse the repository at this point in the history
  • Loading branch information
sunwei committed Dec 9, 2024
1 parent 5438d7a commit a6b8b7a
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 22 deletions.
6 changes: 0 additions & 6 deletions internal/domain/content/entity/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,6 @@ 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
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package content
package form

import (
"fmt"
"net/http"
"strings"
)

const (
MaxMemory = 4 << 20
)

type Content struct {
}

Expand All @@ -24,7 +28,7 @@ func (c *Content) Handle(next http.HandlerFunc) http.HandlerFunc {
}
} else if strings.HasPrefix(contentType, "multipart/form-data") {
// 支持 multipart/form-data
if err := req.ParseMultipartForm(4 << 20); err != nil { // 限制上传大小为 4 MB
if err := req.ParseMultipartForm(MaxMemory); err != nil {
fmt.Println("[content] Error parsing multipart form data:", err)
http.Error(res, "Failed to parse multipart form data", http.StatusBadRequest)
return
Expand Down
3 changes: 2 additions & 1 deletion internal/interfaces/api/handler/handlecontent.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"github.com/gohugonet/hugoverse/internal/domain/content"
apiFrom "github.com/gohugonet/hugoverse/internal/interfaces/api/form"
"github.com/gohugonet/hugoverse/internal/interfaces/api/query"
"github.com/gohugonet/hugoverse/pkg/db"
"github.com/gohugonet/hugoverse/pkg/form"
Expand Down Expand Up @@ -194,7 +195,7 @@ func (s *Handler) getContent(res http.ResponseWriter, req *http.Request) {
}

func (s *Handler) postContent(res http.ResponseWriter, req *http.Request) {
err := req.ParseMultipartForm(1024 * 1024 * 4) // maxMemory 4MB
err := req.ParseMultipartForm(apiFrom.MaxMemory) // maxMemory 4MB
if err != nil {
s.log.Errorf("Error parsing multipart form: %v", err)
res.WriteHeader(http.StatusInternalServerError)
Expand Down
12 changes: 7 additions & 5 deletions internal/interfaces/api/handler/handledeploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"github.com/gohugonet/hugoverse/internal/application"
"github.com/gohugonet/hugoverse/internal/domain/content"
"github.com/gohugonet/hugoverse/internal/interfaces/api/form"
"log"
"net/http"
)
Expand All @@ -19,16 +20,17 @@ func (s *Handler) DeployContentHandler(res http.ResponseWriter, req *http.Reques
return
}

err := req.ParseForm()
err := req.ParseMultipartForm(form.MaxMemory)
if err != nil {
s.log.Errorf("Error parsing deploy form: %v", err)
res.WriteHeader(http.StatusInternalServerError)
return
}

hostName := req.PostForm.Get("host_name")
hostToken := req.PostForm.Get("host_token")
root := req.PostForm.Get("domain")
hostName := req.FormValue("host_name")
hostToken := req.FormValue("host_token")
root := req.FormValue("domain")

if hostToken == "" || root == "" {
hostName = "Netlify"
hostToken = s.adminApp.Netlify.Token()
Expand Down Expand Up @@ -99,7 +101,7 @@ func (s *Handler) DeployContentHandler(res http.ResponseWriter, req *http.Reques
return
}

jsonBytes, err := json.Marshal(d.FullDomain())
jsonBytes, err := json.Marshal("https://" + d.FullDomain())
if err != nil {
s.log.Errorf("Error marshalling token: %v", err)
return
Expand Down
86 changes: 86 additions & 0 deletions internal/interfaces/api/handler/handlepreview.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import (
"fmt"
"github.com/gohugonet/hugoverse/internal/application"
"github.com/gohugonet/hugoverse/internal/domain/content"
"github.com/gohugonet/hugoverse/internal/domain/content/valueobject"
"github.com/gohugonet/hugoverse/internal/interfaces/api/form"
"github.com/gohugonet/hugoverse/pkg/fs/static"
"github.com/gohugonet/hugoverse/pkg/rand"
"github.com/spf13/afero"
"log"
"net/http"
"path"
)
Expand All @@ -22,6 +26,88 @@ func (s *Handler) PreviewContentHandler(res http.ResponseWriter, req *http.Reque
return
}

err := req.ParseMultipartForm(form.MaxMemory)
if err != nil {
s.log.Errorf("Error parsing deploy form: %v", err)
res.WriteHeader(http.StatusInternalServerError)
return
}

pt, ok := s.contentApp.GetContentCreator(t)
if !ok {
res.WriteHeader(http.StatusNotFound)
return
}

p := pt()
_, ok = p.(content.Deployable)
if !ok {
log.Println("[Response] error: Type", t, "does not implement item.Deployable or embed item.Item.")
res.WriteHeader(http.StatusBadRequest)
return
}

t, err = s.contentApp.BuildTarget(t, id, status)
if err != nil {
s.log.Errorf("Error building: %v", err)
res.WriteHeader(http.StatusInternalServerError)
return
}

err = application.GenerateStaticSiteWithTarget(t)
if err != nil {
s.log.Errorf("Error building site %s for deployment with error : %v", id, err)
res.WriteHeader(http.StatusInternalServerError)
return
}

d := &valueobject.Domain{
Root: "app.mdfriday.com",
Sub: fmt.Sprintf("%s-%s", "mdf", rand.ShortString(6)),
Owner: "MDFriday",
}

sd := &valueobject.Deployment{
SiteName: d.Sub,
HostName: "Netlify",
Status: "pending",
}

err = application.DeployToNetlify(t, sd, d, s.adminApp.Netlify.Token())
if err != nil {
s.log.Errorf("Error building: %v", err)
res.WriteHeader(http.StatusInternalServerError)
return
}

jsonBytes, err := json.Marshal("https://" + d.FullDomain())
if err != nil {
s.log.Errorf("Error marshalling token: %v", err)
return
}

j, err := s.res.FmtJSON(jsonBytes)
if err != nil {
s.log.Errorf("Error formatting JSON: %v", err)
res.WriteHeader(http.StatusInternalServerError)
return
}

res.WriteHeader(http.StatusOK)
s.res.Json(res, j)
}

func (s *Handler) PreviewContentHandlerLocal(res http.ResponseWriter, req *http.Request) {
q := req.URL.Query()
id := q.Get("id")
t := q.Get("type")
status := q.Get("status")

if t == "" || id == "" {
res.WriteHeader(http.StatusBadRequest)
return
}

pt, ok := s.contentApp.GetContentCreator(t)
if !ok {
res.WriteHeader(http.StatusNotFound)
Expand Down
6 changes: 3 additions & 3 deletions internal/interfaces/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"github.com/gohugonet/hugoverse/internal/interfaces/api/auth"
"github.com/gohugonet/hugoverse/internal/interfaces/api/cache"
"github.com/gohugonet/hugoverse/internal/interfaces/api/compression"
"github.com/gohugonet/hugoverse/internal/interfaces/api/content"
"github.com/gohugonet/hugoverse/internal/interfaces/api/cors"
"github.com/gohugonet/hugoverse/internal/interfaces/api/database"
"github.com/gohugonet/hugoverse/internal/interfaces/api/form"
"github.com/gohugonet/hugoverse/internal/interfaces/api/handler"
"github.com/gohugonet/hugoverse/internal/interfaces/api/record"
"github.com/gohugonet/hugoverse/internal/interfaces/api/tls"
Expand Down Expand Up @@ -50,7 +50,7 @@ type Server struct {
tls *tls.Tls

record *record.Record
content *content.Content
content *form.Content
comp *compression.Compression
cache *cache.Cache
cors *cors.Cors
Expand All @@ -74,7 +74,7 @@ func NewServer(options ...func(s *Server) error) (*Server, error) {

db: db,
record: record.New(application.DataDir()),
content: &content.Content{},
content: &form.Content{},
auth: &auth.Auth{},
}
for _, o := range options {
Expand Down
8 changes: 4 additions & 4 deletions internal/interfaces/cli/vercurr.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cli

var CurrentVersion = Version{
Major: 0,
Minor: 0,
PatchLevel: 14,
Suffix: "",
Major: 0,
Minor: 1,
PatchLevel: 0,
Suffix: "",
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.0.14",
"version": "0.1.0",
"name": "Hugoverse",
"description": "Headless CMS for Hugo",
"author": "sunwei",
Expand Down
20 changes: 20 additions & 0 deletions pkg/rand/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package rand

import (
"golang.org/x/exp/rand"
"time"
)

const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

func ShortString(length int) string {
// 使用时间作为种子,确保随机性
rand.Seed(uint64(time.Now().UnixNano()))

// 生成随机字符串
b := make([]byte, length)
for i := range b {
b[i] = charset[rand.Intn(len(charset))]
}
return string(b)
}

0 comments on commit a6b8b7a

Please sign in to comment.