mirror of
https://github.com/mxpv/podsync.git
synced 2024-05-11 05:55:04 +00:00
Initial lambda proxy implementation
This commit is contained in:
82
cmd/lambda/main.go
Normal file
82
cmd/lambda/main.go
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/aws/aws-sdk-go/aws/session"
|
||||||
|
"github.com/aws/aws-sdk-go/service/lambda"
|
||||||
|
"github.com/go-chi/chi"
|
||||||
|
"github.com/go-chi/chi/middleware"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
sess = session.Must(session.NewSession())
|
||||||
|
svc = lambda.New(sess)
|
||||||
|
)
|
||||||
|
|
||||||
|
type response struct {
|
||||||
|
StatusCode int `json:"statusCode"`
|
||||||
|
StatusDesc string `json:"statusDescription"`
|
||||||
|
Headers map[string]string `json:"headers"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func resolve(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var (
|
||||||
|
feedID = chi.URLParam(r, "feedID")
|
||||||
|
videoID = chi.URLParam(r, "videoID")
|
||||||
|
path = fmt.Sprintf("/download/%s/%s", feedID, videoID)
|
||||||
|
params = map[string]string{"path": path}
|
||||||
|
)
|
||||||
|
|
||||||
|
// Serialize lambda request payload
|
||||||
|
payload, err := json.Marshal(params)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
request := &lambda.InvokeInput{}
|
||||||
|
request.SetPayload(payload)
|
||||||
|
request.SetFunctionName("Resolver")
|
||||||
|
request.SetQualifier("PROD")
|
||||||
|
|
||||||
|
// Invoke lambda function
|
||||||
|
output, err := svc.Invoke(request)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deserialize lambda response
|
||||||
|
var out response
|
||||||
|
if err := json.Unmarshal(output.Payload, &out); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write response
|
||||||
|
for name, value := range out.Headers {
|
||||||
|
w.Header().Set(name, value)
|
||||||
|
}
|
||||||
|
w.WriteHeader(out.StatusCode)
|
||||||
|
fmt.Fprintln(w, out.StatusDesc)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := chi.NewRouter()
|
||||||
|
|
||||||
|
r.Use(middleware.Logger)
|
||||||
|
r.Use(middleware.Recoverer)
|
||||||
|
r.Use(middleware.GetHead)
|
||||||
|
|
||||||
|
r.Use(middleware.Timeout(15 * time.Second))
|
||||||
|
|
||||||
|
r.Route("/download", func(r chi.Router) {
|
||||||
|
r.Get("/{feedID}/{videoID}", resolve)
|
||||||
|
})
|
||||||
|
|
||||||
|
http.ListenAndServe(":5002", r)
|
||||||
|
}
|
1
go.mod
1
go.mod
@ -13,6 +13,7 @@ require (
|
|||||||
github.com/gin-contrib/gzip v0.0.1
|
github.com/gin-contrib/gzip v0.0.1
|
||||||
github.com/gin-contrib/sessions v0.0.0-20170731012558-a71ea9167c61
|
github.com/gin-contrib/sessions v0.0.0-20170731012558-a71ea9167c61
|
||||||
github.com/gin-gonic/gin v1.3.0
|
github.com/gin-gonic/gin v1.3.0
|
||||||
|
github.com/go-chi/chi v4.0.2+incompatible
|
||||||
github.com/go-pg/pg v6.14.2+incompatible
|
github.com/go-pg/pg v6.14.2+incompatible
|
||||||
github.com/go-redis/redis v6.15.2+incompatible
|
github.com/go-redis/redis v6.15.2+incompatible
|
||||||
github.com/golang/mock v1.2.0
|
github.com/golang/mock v1.2.0
|
||||||
|
2
go.sum
2
go.sum
@ -30,6 +30,8 @@ github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7 h1:AzN37oI0cOS+cou
|
|||||||
github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
|
github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
|
||||||
github.com/gin-gonic/gin v1.3.0 h1:kCmZyPklC0gVdL728E6Aj20uYBJV93nj/TkwBTKhFbs=
|
github.com/gin-gonic/gin v1.3.0 h1:kCmZyPklC0gVdL728E6Aj20uYBJV93nj/TkwBTKhFbs=
|
||||||
github.com/gin-gonic/gin v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y=
|
github.com/gin-gonic/gin v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y=
|
||||||
|
github.com/go-chi/chi v4.0.2+incompatible h1:maB6vn6FqCxrpz4FqWdh4+lwpyZIQS7YEAUcHlgXVRs=
|
||||||
|
github.com/go-chi/chi v4.0.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
|
||||||
github.com/go-pg/pg v6.14.2+incompatible h1:FrOgsHDUhC3V3wkBGAIN5LVj4nJczFPyy1YNFnetfIQ=
|
github.com/go-pg/pg v6.14.2+incompatible h1:FrOgsHDUhC3V3wkBGAIN5LVj4nJczFPyy1YNFnetfIQ=
|
||||||
github.com/go-pg/pg v6.14.2+incompatible/go.mod h1:a2oXow+aFOrvwcKs3eIA0lNFmMilrxK2sOkB5NWe0vA=
|
github.com/go-pg/pg v6.14.2+incompatible/go.mod h1:a2oXow+aFOrvwcKs3eIA0lNFmMilrxK2sOkB5NWe0vA=
|
||||||
github.com/go-redis/redis v6.15.2+incompatible h1:9SpNVG76gr6InJGxoZ6IuuxaCOQwDAhzyXg+Bs+0Sb4=
|
github.com/go-redis/redis v6.15.2+incompatible h1:9SpNVG76gr6InJGxoZ6IuuxaCOQwDAhzyXg+Bs+0Sb4=
|
||||||
|
Reference in New Issue
Block a user