Skip to content

Commit

Permalink
✨ ja almoçou?
Browse files Browse the repository at this point in the history
  • Loading branch information
perebaj committed Aug 13, 2023
1 parent 31a073a commit 6b8c586
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 67 deletions.
20 changes: 0 additions & 20 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,11 @@ on:

jobs:
publish:
permissions:
contents: read
id-token: write

runs-on: ubuntu-22.04
steps:
- name: Checkout code
uses: actions/checkout@v3

- id: "auth"
name: "Authenticate to Google Cloud"
uses: "google-github-actions/auth@v1"
with:
token_format: access_token
workload_identity_provider: ${{ vars.GOOGLE_WORKLOAD_IDENTITY_PROVIDER }}
service_account: ${{ vars.GOOGLE_SERVICE_ACCOUNT }}
access_token_lifetime: 300s

- name: Login to Artifact Registry
uses: docker/login-action@v1
with:
registry: us-central1-docker.pkg.dev/birdie-org/birdie
username: oauth2accesstoken
password: ${{ steps.auth.outputs.access_token }}

- name: Publish image
run: |
make image/publish version=latest
Expand Down
39 changes: 0 additions & 39 deletions .github/workflows/release.yaml

This file was deleted.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# armoco
Ja almoçou hoje?

![](assets/japodealmocar.gif)
80 changes: 74 additions & 6 deletions api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,98 @@ package api

import (
"embed"
"encoding/json"
"io"
"math/rand"
"net/http"
"os"

"github.com/cloudflare/cloudflare-go"
"github.com/go-openapi/runtime/middleware"
"github.com/gorilla/mux"
"golang.org/x/exp/slog"
)

func handlePostImage(w http.ResponseWriter, r *http.Request) {
type Application struct {
Cloudflare *cloudflare.API
}

func (app *Application) handlerPostImage(w http.ResponseWriter, r *http.Request) {
slog.Info("handlePostImage")
err := r.ParseMultipartForm(32 << 20) // 32MB is the maximum size of a file we can upload
if err != nil {
slog.Error("failed to parse multipart form", "error", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}

slog.Info("form", "form", r.Form)
slog.Info("form", "form name", r.Form["name"])

file, handler, err := r.FormFile("image")
if err != nil {
slog.Error("failed to get image from form", "error", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
defer file.Close()

f, err := os.OpenFile("./uploads/"+handler.Filename, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
slog.Error("failed to open file", "error", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
defer f.Close()

_, err = io.Copy(f, file)
if err != nil {
slog.Error("failed to copy file", "error", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write([]byte("File uploaded successfully :)"))
}

func handleGetImage(w http.ResponseWriter, r *http.Request) {
slog.Info("handleGetImage")
func (app *Application) handlerGetImage(w http.ResponseWriter, r *http.Request) {
images, err := app.Cloudflare.ListImages(r.Context(), nil, cloudflare.ListImagesParams{})

if err != nil {
slog.Error("failed to list images", "error", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
slog.Info("images", "images", images)

selectedImage := images[rand.Intn(len(images))]

image, err := app.Cloudflare.GetImage(r.Context(), nil, selectedImage.ID)

if err != nil {
slog.Error("failed to get image", "error", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
imageByte, err := json.Marshal(image)
if err != nil {
slog.Error("failed to marshal image", "error", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}

w.Write(imageByte)
}

//go:embed openapi.yaml
var swaggerFs embed.FS

func Routes() *mux.Router {
func (app *Application) Routes() *mux.Router {
router := mux.NewRouter()
router.HandleFunc("/v1/images", handlePostImage).Methods("POST")
router.HandleFunc("/v1/images", handleGetImage).Methods("GET")
router.HandleFunc("/v1/images", app.handlerPostImage).Methods("POST")
router.HandleFunc("/v1/images", app.handlerGetImage).Methods("GET")

opts := middleware.SwaggerUIOpts{SpecURL: "openapi.yaml"}
sh := middleware.SwaggerUI(opts, nil)
Expand Down
Binary file added assets/japodealmocar.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 22 additions & 2 deletions cmd/armoco/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,38 @@ import (

"github.com/birdie-ai/armoco/api"
"github.com/birdie-ai/golibs/slog"
"github.com/kelseyhightower/envconfig"
)

const serviceName = "armoco"

type cfg struct {
CfApiKey string `envconfig:"CF_API_KEY"`
CfApiEmail string `envconfig:"CF_API_EMAIL"`
}

func main() {
logcfg, err := slog.LoadConfig("ARMOCO")
logcfg, err := slog.LoadConfig(serviceName)
abortonerr(err)

if err := envconfig.Process(serviceName, &cfg{}); err != nil {
slog.Fatal("failed to load config from environment", "error", err.Error())
}

err = slog.Configure(logcfg)
abortonerr(err)

// _, err = cloudflare.New(os.Getenv("CF_API_KEY"), os.Getenv("CF_API_EMAIL"))

// abortonerr(err)

app := &api.Application{
Cloudflare: nil,
}

srv := &http.Server{
Addr: ":8080",
Handler: api.Routes(),
Handler: app.Routes(),
IdleTimeout: time.Minute,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
Expand Down
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require github.com/birdie-ai/golibs/slog v0.0.5
require (
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/birdie-ai/gootstrap v0.0.0-20230810110120-308640d5fc29 // indirect
github.com/cloudflare/cloudflare-go v0.74.0 // indirect
github.com/go-openapi/analysis v0.21.4 // indirect
github.com/go-openapi/errors v0.20.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
Expand All @@ -17,12 +18,19 @@ require (
github.com/go-openapi/strfmt v0.21.7 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-openapi/validate v0.22.1 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.4 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/kelseyhightower/envconfig v1.4.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/oklog/ulid v1.3.1 // indirect
go.mongodb.org/mongo-driver v1.11.3 // indirect
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/time v0.3.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/birdie-ai/golibs/slog v0.0.5 h1:N3fq0a7t85CHyufMrdTc2wrNUebjqxVU+90eU
github.com/birdie-ai/golibs/slog v0.0.5/go.mod h1:3dc4562RKBL6q7MaAHQuMo3UPNDlwfZP+dXcSSx3TtM=
github.com/birdie-ai/gootstrap v0.0.0-20230810110120-308640d5fc29 h1:AUd49QcA0bQ/FaYHhlUsJuX2AMQuWYIgCHrl3Rl9hsI=
github.com/birdie-ai/gootstrap v0.0.0-20230810110120-308640d5fc29/go.mod h1:ck14NIaLq1hpNn6AB9Emn1BwF9/+ObC2dNt+tU3F7P0=
github.com/cloudflare/cloudflare-go v0.74.0 h1:4grnoT7XiNpRSf/Sta2Gz9N6B6ljvGQyLnevIzo6U88=
github.com/cloudflare/cloudflare-go v0.74.0/go.mod h1:5xOc5nIVnd+5ai+10r+5NdFHf92RPRx5AM+aekMIhco=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -73,15 +75,24 @@ github.com/gobuffalo/packr/v2 v2.2.0/go.mod h1:CaAwI0GPIAv+5wKLtv8Afwl+Cm78K/I/V
github.com/gobuffalo/syncx v0.0.0-20190224160051-33c29581e754/go.mod h1:HhnNqWY95UYwwW3uSASeV7vtgYkT2t16hJgV3AEPUpw=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-retryablehttp v0.7.4 h1:ZQgVdpTdAL7WpMIwLzCfbalOcSUdkDZnpUv3/+BxzFA=
github.com/hashicorp/go-retryablehttp v0.7.4/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/karrick/godirwalk v1.8.0/go.mod h1:H5KPZjojv4lE+QYImBI8xVtrBRgYrIVsaRPx4tDPEn4=
github.com/karrick/godirwalk v1.10.3/go.mod h1:RoGL9dQei4vP9ilrpETWE8CLOZ1kiN0LhBygSwrAsHA=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down Expand Up @@ -149,6 +160,8 @@ golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1Kcs5dz7/ng1VjMUvfKvpfy+jM=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50=
golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190412183630-56d357773e84/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -170,6 +183,10 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190329151228-23e29df326fe/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190416151739-9c9e1878f421/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
Expand Down

0 comments on commit 6b8c586

Please sign in to comment.