From b136a9f6179cc5291d47aa90f20d11e90b3ee00d Mon Sep 17 00:00:00 2001 From: Josh Zarrabi Date: Mon, 30 Nov 2020 19:51:05 +0000 Subject: [PATCH] Add support for custom root directory Co-authored-by: Arjun Sreedharan --- README.md | 2 +- buildpack_yml_parser.go | 4 +++ buildpack_yml_parser_test.go | 16 ++++++++++ integration/nginx_test.go | 31 +++++++++++++++++++ .../testdata/custom_root_app/buildpack.yml | 4 +++ .../custom_root_app/dir1/dir2/index.html | 10 ++++++ server_configs/nginx.conf | 2 +- 7 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 integration/testdata/custom_root_app/buildpack.yml create mode 100644 integration/testdata/custom_root_app/dir1/dir2/index.html diff --git a/README.md b/README.md index f4709d9..f88550c 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ This builds the buildpack's Go source using `GOOS=linux` by default. You can sup ```yaml staticfile: nginx: - root: + root: # default value: "public" host_dot_files: location_include: directory: diff --git a/buildpack_yml_parser.go b/buildpack_yml_parser.go index 395023d..d46db2e 100644 --- a/buildpack_yml_parser.go +++ b/buildpack_yml_parser.go @@ -48,6 +48,10 @@ func parse(path string) (Config, error) { return Config{}, fmt.Errorf("unable to parse buildpack.yml: %q", err) } + if buildpack.Staticfile.Nginx != nil && buildpack.Staticfile.Nginx.RootDir == "" { + buildpack.Staticfile.Nginx.RootDir = "public" + } + return buildpack.Staticfile, err } diff --git a/buildpack_yml_parser_test.go b/buildpack_yml_parser_test.go index 985a810..e95fafe 100644 --- a/buildpack_yml_parser_test.go +++ b/buildpack_yml_parser_test.go @@ -71,6 +71,22 @@ staticfile: "some-staus": "some-code", })) }) + + when("the root dir is not specified", func() { + it.Before(func() { + err := ioutil.WriteFile(path, []byte(`--- +staticfile: + nginx: {} +`), os.ModePerm) + Expect(err).NotTo(HaveOccurred()) + }) + it("sets RootDir to 'public'", func() { + configData, err := buildpackYMLParser.Parse(path) + Expect(err).NotTo(HaveOccurred()) + + Expect(configData.Nginx.RootDir).To(Equal("public")) + }) + }) }) }) diff --git a/integration/nginx_test.go b/integration/nginx_test.go index fd3a6c8..902367c 100644 --- a/integration/nginx_test.go +++ b/integration/nginx_test.go @@ -79,5 +79,36 @@ func testNginx(t *testing.T, context spec.G, it spec.S) { Expect(string(content)).To(ContainSubstring("helloworld")) }) + + context("app with custom root", func() { + it("builds and runs successfully", func() { + var err error + source, err = occam.Source(filepath.Join("testdata", "custom_root_app")) + Expect(err).NotTo(HaveOccurred()) + + var logs fmt.Stringer + image, logs, err = pack.WithNoColor().Build. + WithPullPolicy("never"). + WithBuildpacks(nginxBuildpack, buildpack). + Execute(name, source) + Expect(err).NotTo(HaveOccurred(), logs.String) + + container, err = docker.Container.Run.Execute(image.ID) + Expect(err).NotTo(HaveOccurred()) + + Eventually(container).Should(BeAvailable()) + + response, err := http.Get(fmt.Sprintf("http://localhost:%s", container.HostPort())) + Expect(err).NotTo(HaveOccurred()) + defer response.Body.Close() + + Expect(response.StatusCode).To(Equal(http.StatusOK)) + + content, err := ioutil.ReadAll(response.Body) + Expect(err).NotTo(HaveOccurred()) + + Expect(string(content)).To(ContainSubstring("helloworld")) + }) + }) }) } diff --git a/integration/testdata/custom_root_app/buildpack.yml b/integration/testdata/custom_root_app/buildpack.yml new file mode 100644 index 0000000..d4fee68 --- /dev/null +++ b/integration/testdata/custom_root_app/buildpack.yml @@ -0,0 +1,4 @@ +--- +staticfile: + nginx: + root: "dir1/dir2" diff --git a/integration/testdata/custom_root_app/dir1/dir2/index.html b/integration/testdata/custom_root_app/dir1/dir2/index.html new file mode 100644 index 0000000..419c364 --- /dev/null +++ b/integration/testdata/custom_root_app/dir1/dir2/index.html @@ -0,0 +1,10 @@ + + + hello World + + +

+ helloworld +

+ + diff --git a/server_configs/nginx.conf b/server_configs/nginx.conf index 1feeb63..75f7986 100644 --- a/server_configs/nginx.conf +++ b/server_configs/nginx.conf @@ -110,7 +110,7 @@ http { listen {{port}}; server_name localhost; - root {{ env "APP_ROOT" }}/public; + root {{ env "APP_ROOT" }}/$(( .RootDir )); $(( if .ForceHTTPS )) set $updated_host $host;