-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge commit 'pullreqs/44' into develop
- Loading branch information
Showing
12 changed files
with
760 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
|
||
"github.com/nextdotid/proof_server/headless" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
var ( | ||
flagPort = flag.Int("port", 9801, "Listen port") | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
logrus.SetLevel(logrus.DebugLevel) | ||
headless.Init("") | ||
|
||
fmt.Printf("Server now running on 0.0.0.0:%d", *flagPort) | ||
headless.Engine.Run(fmt.Sprintf("0.0.0.0:%d", *flagPort)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# syntax=docker/dockerfile:1 | ||
|
||
# https://docs.docker.com/language/golang/build-images/ | ||
FROM golang:1.18-buster AS build | ||
|
||
WORKDIR /app | ||
|
||
COPY go.mod ./ | ||
COPY go.sum ./ | ||
RUN go mod download | ||
COPY . ./ | ||
|
||
# Remember to build your handler executable for Linux! | ||
# https://github.com/aws/aws-lambda-go/blob/main/README.md#building-your-function | ||
RUN env GOOS=linux GOARCH=amd64 CGO_ENABLED=0 \ | ||
go build -o /main ./cmd/lambda_headless/main.go | ||
|
||
|
||
# Install chromium | ||
FROM public.ecr.aws/lambda/provided:al2 as chromium | ||
|
||
# install brotli, so we can decompress chromium | ||
# we don't have access to brotli out of the box, to install we first need epel | ||
# https://docs.fedoraproject.org/en-US/epel/#what_is_extra_packages_for_enterprise_linux_or_epel | ||
RUN yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm && \ | ||
yum -y install brotli && \ | ||
yum clean all | ||
|
||
# download chromium | ||
# s/o to https://github.com/alixaxel/chrome-aws-lambda for the binary | ||
RUN yum -y install wget && \ | ||
wget --progress=dot:giga https://raw.githubusercontent.com/alixaxel/chrome-aws-lambda/master/bin/chromium.br -O /chromium.br && \ | ||
yum clean all | ||
|
||
# decompress chromium | ||
RUN brotli -d /chromium.br | ||
|
||
# copy artifacts to a clean image | ||
FROM public.ecr.aws/lambda/provided:al2 | ||
|
||
# install chromium dependencies | ||
RUN yum -y install \ | ||
libX11 \ | ||
nano \ | ||
unzip \ | ||
wget \ | ||
xclock \ | ||
xorg-x11-xauth \ | ||
xterm && \ | ||
yum clean all | ||
|
||
# copy in chromium from chromium stage | ||
COPY --from=chromium /chromium /opt/chromium | ||
|
||
# grant our program access to chromium | ||
RUN chmod 777 /opt/chromium | ||
|
||
# copy in lambda fn from build stage | ||
COPY --from=build /main /main | ||
|
||
ENTRYPOINT ["/main"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/akrylysov/algnhsa" | ||
"github.com/sirupsen/logrus" | ||
"github.com/nextdotid/proof_server/headless" | ||
) | ||
|
||
func init() { | ||
logrus.SetLevel(logrus.WarnLevel) | ||
headless.Init("/opt/chromium") | ||
} | ||
|
||
func main() { | ||
algnhsa.ListenAndServe(headless.Engine, nil) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package headless | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"path" | ||
|
||
"golang.org/x/xerrors" | ||
) | ||
|
||
// HeadlessClient handles communication for headless browser service | ||
type HeadlessClient struct { | ||
url string | ||
client *http.Client | ||
} | ||
|
||
// NewHeadlessClient creates a new headless client | ||
func NewHeadlessClient(url string) *HeadlessClient { | ||
return &HeadlessClient{url, http.DefaultClient} | ||
} | ||
|
||
// Find find whether the target matching payload exists | ||
func (h *HeadlessClient) Find(ctx context.Context, payload *FindRequest) (string, error) { | ||
u, err := url.Parse(h.url) | ||
if err != nil { | ||
return "", xerrors.Errorf("%w", err) | ||
} | ||
|
||
u.Path = path.Join(u.Path, "/v1/find") | ||
body, err := json.Marshal(payload) | ||
if err != nil { | ||
return "", xerrors.Errorf("%w", err) | ||
} | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), bytes.NewReader(body)) | ||
if err != nil { | ||
return "", xerrors.Errorf("%w", err) | ||
} | ||
|
||
req.Header.Add("Content-Type", "application/json") | ||
|
||
res, err := h.client.Do(req) | ||
if res != nil && err != nil { | ||
if _, err := io.Copy(io.Discard, res.Body); err != nil { | ||
return "", xerrors.Errorf("%w", err) | ||
} | ||
} | ||
|
||
if res != nil { | ||
defer res.Body.Close() | ||
} | ||
|
||
if err != nil { | ||
return "", xerrors.Errorf("%w", err) | ||
} | ||
|
||
contents, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
return "", xerrors.Errorf("%w", err) | ||
} | ||
|
||
var resBody FindRespond | ||
if err := json.Unmarshal(contents, &resBody); err != nil { | ||
return "", xerrors.Errorf("%w", err) | ||
} | ||
|
||
return resBody.Content, nil | ||
} |
Oops, something went wrong.