Skip to content

Commit

Permalink
feat(zbpack): implement handle raw html static build
Browse files Browse the repository at this point in the history
Signed-off-by: hackerchai <[email protected]>
  • Loading branch information
hackerchai committed Oct 26, 2023
1 parent 73b9e75 commit 17e3e04
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea
bin
.DS_Store
tests/*/.zeabur
.tmp
15 changes: 11 additions & 4 deletions internal/static/TransformServerless.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ import (
)

// TransformServerless copies the static files from output to .zeabur/output/static and creates a config.json file for SPA
func TransformServerless(image, workdir string, meta types.PlanMeta) error {
err := utils.CopyFromImage(image, path.Join("/src", meta["outputDir"])+"/.", path.Join(workdir, ".zeabur/output/static"))
if err != nil {
return err
func TransformServerless(image, workdir string, meta types.PlanMeta, isHtmlStatic bool) error {

Check warning on line 13 in internal/static/TransformServerless.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: func parameter isHtmlStatic should be isHTMLStatic (revive)
if isHtmlStatic == true {

Check failure on line 14 in internal/static/TransformServerless.go

View workflow job for this annotation

GitHub Actions / lint

S1002: should omit comparison to bool constant, can be simplified to `isHtmlStatic` (gosimple)
err := utils.CopyFromSource(path.Join(workdir, meta["outputDir"])+"/.", path.Join(workdir, ".zeabur/output/static"))
if err != nil {
return err
}
} else {
err := utils.CopyFromImage(image, path.Join("/src", meta["outputDir"])+"/.", path.Join(workdir, ".zeabur/output/static"))
if err != nil {
return err
}
}

config := types.ZeaburOutputConfig{Containerized: false, Routes: make([]types.ZeaburOutputConfigRoute, 0)}
Expand Down
6 changes: 5 additions & 1 deletion internal/static/identify.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ func (i *identify) PlanType() types.PlanType {
}

func (i *identify) Match(fs afero.Fs) bool {
return utils.HasFile(fs, "index.html", "hugo.toml", "config/_default/hugo.toml")
return utils.HasFile(fs, "index.html", "hugo.toml", "config/_default/hugo.toml", "public/index.html", "dist/index.html")
}

func (i *identify) PlanMeta(options plan.NewPlannerOptions) types.PlanMeta {

if utils.HasFile(options.Source, "index.html", "public/index.html", "dist/index.html") {
return types.PlanMeta{"framework": "html-static"}
}

if utils.HasFile(options.Source, "hugo.toml", "config/_default/hugo.toml") {
return types.PlanMeta{"framework": "hugo"}
}
Expand Down
4 changes: 4 additions & 0 deletions internal/static/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ RUN echo "server { listen 8080; root /usr/share/nginx/html/static; absolute_redi
EXPOSE 8080`, nil
}

if meta["framework"] == "html-static" {
return `HTML-STATIC`, nil
}

dockerfile := `FROM docker.io/library/nginx:alpine as runtime
WORKDIR /usr/share/nginx/html/static
COPY . .
Expand Down
41 changes: 41 additions & 0 deletions internal/utils/copy_from_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package utils

import (
"fmt"
"os"
"os/exec"
"strings"
)

func CopyFromSource(dirInSrc, destOnHost string) error {

Check warning on line 10 in internal/utils/copy_from_source.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported function CopyFromSource should have comment or be unexported (revive)
if err := os.MkdirAll(".tmp", 0755); err != nil {
return fmt.Errorf("create directory: %w", err)
}
defer func() {
removeCmd := exec.Command("rm", "-rf", ".tmp")
removeCmd.Stderr = os.Stderr
if err := removeCmd.Run(); err != nil {
fmt.Println(err)
}
}()
var stderr strings.Builder
tempCopyCmd := exec.Command("cp", "-r", dirInSrc, ".tmp")
tempCopyCmd.Stderr = &stderr
err := tempCopyCmd.Run()
if err != nil {
return fmt.Errorf("copy from source code: %s: %w", stderr.String(), err)
}

if err := os.MkdirAll(destOnHost, 0o755); err != nil {
return fmt.Errorf("create directory: %w", err)
}
copyCmd := exec.Command("cp", "-r", ".tmp/.", destOnHost)

copyCmd.Stderr = &stderr
err = copyCmd.Run()
if err != nil {
return fmt.Errorf("copy from source code: %s: %w", stderr.String(), err)
}

return nil
}
3 changes: 3 additions & 0 deletions pkg/zeaburpack/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func buildImage(opt *buildImageOptions) error {
resolvedVars := envexpander.ResolveEnvVariable(opt.UserVars)

refConstructor := newReferenceConstructor(opt.ProxyRegistry)
if opt.Dockerfile == "HTML-STATIC" {
return nil
}
lines := strings.Split(opt.Dockerfile, "\n")
stageLines := make([]int, 0)

Expand Down
17 changes: 16 additions & 1 deletion pkg/zeaburpack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ func Build(opt *BuildOptions) error {
}

_ = os.RemoveAll(".zeabur")
if opt.Path != nil || *opt.Path != "" {
_ = os.RemoveAll(*opt.Path + "/.zeabur")
}

if t == types.PlanTypeNodejs && m["framework"] == string(types.NodeProjectFrameworkNextJs) && m["serverless"] == "true" {
println("Transforming build output to serverless format ...")
Expand All @@ -199,19 +202,31 @@ func Build(opt *BuildOptions) error {

if t == types.PlanTypeNodejs && m["outputDir"] != "" {
println("Transforming build output to serverless format ...")
err = static.TransformServerless(*opt.ResultImage, *opt.Path, m)
err = static.TransformServerless(*opt.ResultImage, *opt.Path, m, false)
if err != nil {
println("Failed to transform serverless: " + err.Error())
handleBuildFailed(err)
return err
}
}

if t == types.PlanTypeStatic && m["framework"] == "html-static" {
println("Transforming build output to serverless format ...")
err = static.TransformServerless(*opt.ResultImage, *opt.Path, m, true)
if err != nil {
println("Failed to transform static serverless: " + err.Error())
handleBuildFailed(err)
return err
}
}

if opt.Interactive != nil && *opt.Interactive {
handleLog("\n\033[32mBuild successful\033[0m\n")
handleLog("\033[90m" + "To run the image, use the following command:" + "\033[0m")
if t == types.PlanTypeNodejs && m["outputDir"] != "" {
handleLog("npx serve .zeabur/output/static")
} else if t == types.PlanTypeStatic && m["framework"] == "html-static" {
handleLog("npx serve .zeabur/output/static")
} else {
handleLog("docker run -p 8080:8080 -it " + *opt.ResultImage)
}
Expand Down
9 changes: 9 additions & 0 deletions tests/static-html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>Hello world</title>
</head>
<body>
<h1>Hello world</h1>
</body>
</html>

0 comments on commit 17e3e04

Please sign in to comment.