Skip to content

Commit

Permalink
feat(zbpack): implement static provider generate static files
Browse files Browse the repository at this point in the history
Signed-off-by: hackerchai <[email protected]>
  • Loading branch information
hackerchai committed Oct 27, 2023
1 parent 53ea50b commit 16c324b
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 65 deletions.
15 changes: 4 additions & 11 deletions internal/static/TransformServerless.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,10 @@ 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, isHTMLStatic bool) error {
if isHTMLStatic {
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
}
func TransformServerless(image, workdir string, meta types.PlanMeta) error {
err := utils.CopyFromImage(image, path.Join("/usr/share/nginx/html/static", 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
4 changes: 0 additions & 4 deletions internal/static/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ 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
42 changes: 0 additions & 42 deletions internal/utils/copy_from_source.go

This file was deleted.

3 changes: 0 additions & 3 deletions pkg/zeaburpack/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ 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
11 changes: 6 additions & 5 deletions pkg/zeaburpack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,17 @@ 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, false)
err = static.TransformServerless(*opt.ResultImage, *opt.Path, m)
if err != nil {
println("Failed to transform serverless: " + err.Error())
handleBuildFailed(err)
return err
}
}

if t == types.PlanTypeStatic && m["framework"] == "html-static" {
if t == types.PlanTypeStatic {
println("Transforming build output to serverless format ...")
err = static.TransformServerless(*opt.ResultImage, *opt.Path, m, true)
err = static.TransformServerless(*opt.ResultImage, *opt.Path, m)
if err != nil {
println("Failed to transform static serverless: " + err.Error())
handleBuildFailed(err)
Expand All @@ -225,8 +225,9 @@ func Build(opt *BuildOptions) error {
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 if t == types.PlanTypeStatic {
handleLog("docker run -p 8080:8080 -it " + *opt.ResultImage)
handleLog("or you can find generated static file in .zeabur/output/static")
} else {
handleLog("docker run -p 8080:8080 -it " + *opt.ResultImage)
}
Expand Down

0 comments on commit 16c324b

Please sign in to comment.