From a6b8b7a5360ef59abd6d8d06817a06f3dfa5e299 Mon Sep 17 00:00:00 2001 From: sunwei Date: Mon, 9 Dec 2024 19:06:41 +0800 Subject: [PATCH] customize deploy domains --- internal/domain/content/entity/search.go | 6 -- .../api/{content => form}/content.go | 8 +- .../interfaces/api/handler/handlecontent.go | 3 +- .../interfaces/api/handler/handledeploy.go | 12 +-- .../interfaces/api/handler/handlepreview.go | 86 +++++++++++++++++++ internal/interfaces/api/server.go | 6 +- internal/interfaces/cli/vercurr.go | 8 +- manifest.json | 2 +- pkg/rand/string.go | 20 +++++ 9 files changed, 129 insertions(+), 22 deletions(-) rename internal/interfaces/api/{content => form}/content.go (91%) create mode 100644 pkg/rand/string.go diff --git a/internal/domain/content/entity/search.go b/internal/domain/content/entity/search.go index bfe6ed9c..dcf17ea5 100644 --- a/internal/domain/content/entity/search.go +++ b/internal/domain/content/entity/search.go @@ -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 diff --git a/internal/interfaces/api/content/content.go b/internal/interfaces/api/form/content.go similarity index 91% rename from internal/interfaces/api/content/content.go rename to internal/interfaces/api/form/content.go index ce6dab05..13edccf9 100644 --- a/internal/interfaces/api/content/content.go +++ b/internal/interfaces/api/form/content.go @@ -1,4 +1,4 @@ -package content +package form import ( "fmt" @@ -6,6 +6,10 @@ import ( "strings" ) +const ( + MaxMemory = 4 << 20 +) + type Content struct { } @@ -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 diff --git a/internal/interfaces/api/handler/handlecontent.go b/internal/interfaces/api/handler/handlecontent.go index 314b82fe..05ca9291 100644 --- a/internal/interfaces/api/handler/handlecontent.go +++ b/internal/interfaces/api/handler/handlecontent.go @@ -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" @@ -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) diff --git a/internal/interfaces/api/handler/handledeploy.go b/internal/interfaces/api/handler/handledeploy.go index 4b70f3fd..c4e7dd64 100644 --- a/internal/interfaces/api/handler/handledeploy.go +++ b/internal/interfaces/api/handler/handledeploy.go @@ -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" ) @@ -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() @@ -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 diff --git a/internal/interfaces/api/handler/handlepreview.go b/internal/interfaces/api/handler/handlepreview.go index fcd42485..da93612e 100644 --- a/internal/interfaces/api/handler/handlepreview.go +++ b/internal/interfaces/api/handler/handlepreview.go @@ -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" ) @@ -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) diff --git a/internal/interfaces/api/server.go b/internal/interfaces/api/server.go index 4c8adbae..4d031e2b 100644 --- a/internal/interfaces/api/server.go +++ b/internal/interfaces/api/server.go @@ -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" @@ -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 @@ -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 { diff --git a/internal/interfaces/cli/vercurr.go b/internal/interfaces/cli/vercurr.go index c7ed72c5..ed6c5da7 100644 --- a/internal/interfaces/cli/vercurr.go +++ b/internal/interfaces/cli/vercurr.go @@ -1,8 +1,8 @@ package cli var CurrentVersion = Version{ - Major: 0, - Minor: 0, - PatchLevel: 14, - Suffix: "", + Major: 0, + Minor: 1, + PatchLevel: 0, + Suffix: "", } diff --git a/manifest.json b/manifest.json index a08d11e4..3a1ba328 100644 --- a/manifest.json +++ b/manifest.json @@ -1,5 +1,5 @@ { - "version": "0.0.14", + "version": "0.1.0", "name": "Hugoverse", "description": "Headless CMS for Hugo", "author": "sunwei", diff --git a/pkg/rand/string.go b/pkg/rand/string.go new file mode 100644 index 00000000..05561574 --- /dev/null +++ b/pkg/rand/string.go @@ -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) +}