Skip to content

Commit

Permalink
add Turbocharger support
Browse files Browse the repository at this point in the history
  • Loading branch information
joonas-fi committed Aug 2, 2021
1 parent 931890c commit 92a1cd0
Show file tree
Hide file tree
Showing 11 changed files with 374 additions and 8 deletions.
3 changes: 2 additions & 1 deletion cmd/happy-api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"

"github.com/function61/gokit/crypto/cryptoutil"
"github.com/function61/happy-api/static"
"github.com/rwcarlsen/goexif/exif"
"github.com/spf13/cobra"
)
Expand All @@ -32,7 +33,7 @@ func randBetween(min, max int) int {
}

func findAttributionFromExifArtist(id string) (string, error) {
f, err := images.Open("images/" + id + ".jpg")
f, err := static.Files.Open("images/" + id + ".jpg")
if err != nil {
return "", err
}
Expand Down
12 changes: 6 additions & 6 deletions cmd/happy-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@ import (
"github.com/function61/gokit/log/logex"
"github.com/function61/gokit/net/http/httputils"
"github.com/function61/gokit/os/osutil"
"github.com/function61/edgerouter/pkg/turbocharger/turbochargerapp"
"github.com/function61/happy-api/static"
"github.com/gorilla/mux"
"github.com/spf13/cobra"
)

//go:embed item.html
var templates embed.FS

//go:embed images/*
var images embed.FS

func main() {
rand.Seed(time.Now().UnixNano())

Expand Down Expand Up @@ -54,6 +53,7 @@ func main() {
}

app.AddCommand(newEntry())
app.AddCommand(turbochargerapp.StaticFilesExportEntrypoint(static.Files))

osutil.ExitIfError(app.Execute())
}
Expand All @@ -64,7 +64,7 @@ func httpHandler() http.Handler {
panic(err)
}

happiness, err := images.ReadDir("images")
happiness, err := static.Files.ReadDir("images")
if err != nil {
panic(err)
}
Expand All @@ -77,7 +77,7 @@ func httpHandler() http.Handler {
http.Redirect(w, r, "/happy/"+fileIdFromFilename(happiness[idx].Name()), http.StatusFound)
}

routes.PathPrefix("/happy/images/").Handler(http.StripPrefix("/happy/", http.FileServer(http.FS(images))))
routes.PathPrefix("/happy/static").Handler(turbochargerapp.FileHandler("/happy/static", static.Files))

routes.HandleFunc("/happy", redirectToRandomItem)
routes.HandleFunc("/happy/", redirectToRandomItem)
Expand All @@ -101,7 +101,7 @@ func httpHandler() http.Handler {
ImgSrc string
Attribution string
}{
ImgSrc: "/happy/images/" + id + ".jpg",
ImgSrc: "/happy/static/images/" + id + ".jpg",
Attribution: attribution,
})
})
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module github.com/function61/happy-api
go 1.16

require (
github.com/aws/aws-lambda-go v1.13.2
github.com/aws/aws-lambda-go v1.14.0
github.com/function61/edgerouter v0.0.0-20210729173323-2093d095c684 // indirect
github.com/function61/gokit v0.0.0-20210207144405-1f1e50ad6dcc
github.com/gorilla/mux v1.7.4
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd
Expand Down
356 changes: 356 additions & 0 deletions go.sum

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions static/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package static

import (
"embed"
)

//go:embed images/*
var Files embed.FS
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes

0 comments on commit 92a1cd0

Please sign in to comment.