Skip to content

Commit

Permalink
Fix query params and small refactor (heroku#22)
Browse files Browse the repository at this point in the history
* Fix query params and small refactor

Adds a helper function to remove some of the error checking boilerplate,
and loops through files we need to build instead of doing copy pasta.

* Use log.Panic over log.Error in server
  • Loading branch information
jsncmgs1 authored Jan 11, 2021
1 parent 266b495 commit 9ba88d4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 35 deletions.
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59/go.mod h1:q/89r3U
github.com/buildpack/libbuildpack v1.11.0 h1:yIjsdJuQaTspDE/6wjkUX4OF56YRtMHajbARQLmm8KU=
github.com/buildpack/libbuildpack v1.11.0/go.mod h1:tKGgGnBYfOL23Ma4JWM8ucBcGX6Im3p1WTmKPixDPPM=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
Expand Down Expand Up @@ -62,6 +63,7 @@ github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1Cpa
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.2-0.20190227000051-27936f6d90f9/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/fastuuid v1.1.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rollbar/rollbar-go v1.0.2/go.mod h1:AcFs5f0I+c71bpHlXNNDbOWJiKwjFDtISeXco0L5PKQ=
Expand All @@ -78,6 +80,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tj/assert v0.0.0-20171129193455-018094318fb0/go.mod h1:mZ9/Rh9oLWpLLDRpvE+3b7gP/C2YyLFYxNmcLnPTMe0=
github.com/tj/assert v0.0.3/go.mod h1:Ne6X72Q+TB1AteidzQncjw9PabbMp4PBMZ1k+vd1Pvk=
Expand Down Expand Up @@ -115,4 +118,5 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c h1:grhR+C34yXImVGp7EzNk+DTIk+323eIUWOmEevy6bDo=
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
68 changes: 33 additions & 35 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,31 @@ func main() {
r.HandleFunc("/health", HealthHandler)

port := fmt.Sprintf(":%s", conf.Port)
err := rollbar.WrapAndWait(http.ListenAndServe(port, handlers.CompressHandler(r)))
rollbar.WrapAndWait(http.ListenAndServe(port, handlers.CompressHandler(r)))

if err == nil {
log.Error(err)
}
log.Info("server started")
}

func NameHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := fmt.Sprintf("%s/%s", vars["namespace"], vars["name"])
var version, name, api, stacks string
var found bool
params := r.URL.Query()

if version, found = mux.Vars(r)["version"]; !found {
if version = params.Get("version"); version == "" {
version = "0.1"
}

if name, found = mux.Vars(r)["name"]; !found {
if name = params.Get("name"); name == "" {
name = "0.1"
}

if api, found = mux.Vars(r)["api"]; !found {
if api = params.Get("api"); api == "" {
api = "0.4"
}

if stacks, found = mux.Vars(r)["stacks"]; !found {
if stack, found := mux.Vars(r)["stack"]; !found {
if stacks = params.Get("stacks"); stacks == "" {
if stack := params.Get("stack"); stack == "" {
stacks = "heroku-18,heroku-20"
} else {
stacks = stack
Expand All @@ -67,27 +64,25 @@ func NameHandler(w http.ResponseWriter, r *http.Request) {
dir, _ := os.Getwd()
dir, _ = ioutil.TempDir(dir, uuid.New().String())
defer os.RemoveAll(dir)
_ = os.Chdir(dir)
handlePanic(os.Chdir(dir))

log.Infof("at=shim file=%s", shimmedBuildpack)

_ = os.Mkdir("bin", 0777)
input, _ := ioutil.ReadFile(fmt.Sprintf("%s/bin/build", shimDir))
_ = ioutil.WriteFile("bin/build", input, 0700)

input, _ = ioutil.ReadFile(fmt.Sprintf("%s/bin/detect", shimDir))
_ = ioutil.WriteFile("bin/detect", input, 0700)

input, _ = ioutil.ReadFile(fmt.Sprintf("%s/bin/release", shimDir))
_ = ioutil.WriteFile("bin/release", input, 0700)
handlePanic(os.Mkdir("bin", 0777))

input, _ = ioutil.ReadFile(fmt.Sprintf("%s/bin/exports", shimDir))
_ = ioutil.WriteFile("bin/exports", input, 0700)
files := []string{"build", "detect", "release", "exports"}
for _, f := range files {
input, err := ioutil.ReadFile(fmt.Sprintf("%s/bin/%s", shimDir, f))
handlePanic(err)
err = ioutil.WriteFile(fmt.Sprintf("bin/%s", f), input, 0700)
handlePanic(err)
}

log.Infof("at=descriptor file=%s api=%s id=%s version=%s name=%s stacks=%s",
shimmedBuildpack, api, id, version, name, stacks)

file, _ := os.Create("buildpack.toml")
file, err := os.Create("buildpack.toml")
handlePanic(err)

bp := fmt.Sprintf("api = \"%s\"\n\n[buildpack]\nid = \"%s\"\nversion = \"%s\"\nname = \"%s\"\n", api, id, version, name)

Expand All @@ -96,31 +91,28 @@ func NameHandler(w http.ResponseWriter, r *http.Request) {
bp = bp + s
}

_, _ = file.WriteString(bp)
_, err = file.WriteString(bp)
handlePanic(err)
target_dir := "target"
_ = os.Mkdir(target_dir, 0777)
handlePanic(os.Mkdir(target_dir, 0777))

url := fmt.Sprintf("https://buildpack-registry.s3.amazonaws.com/buildpacks/%s.tgz", id)
log.Infof("at=download file=%s url=%s", shimmedBuildpack, url)
cmd := fmt.Sprintf(`curl --retry 3 --silent --location "%s" | tar xzm -C %s`, url, target_dir)

_, err := exec.Command("bash", "-c", cmd).Output()
if err != nil {
log.Error(err)
}
_, err = exec.Command("bash", "-c", cmd).Output()
handlePanic(err)
handlePanic(os.Chdir(shimDir))

_ = os.Chdir(shimDir)
cmd = fmt.Sprintf("tar cz --file=%s --directory=%s .", shimmedBuildpack, dir)

_, err = exec.Command("bash", "-c", cmd).Output()
handlePanic(err)
defer fmt.Printf("at=cleanup file=%s", shimmedBuildpack)
defer os.Remove(shimmedBuildpack)

if err != nil {
log.Error(err)
}

fstat, _ := file.Stat()
fstat, err := file.Stat()
handlePanic(err)
log.Infof("at=send file=%s size=%d", shimmedBuildpack, fstat.Size())
w.Header().Add("Content-Type", "application/x-gzip")
http.ServeFile(w, r, shimmedBuildpack)
Expand All @@ -131,3 +123,9 @@ func HealthHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "health check ok")
log.Info("health check ok")
}

func handlePanic(e error) {
if e != nil {
log.Panic(e)
}
}

0 comments on commit 9ba88d4

Please sign in to comment.