Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit 1a2a396

Browse files
committed
Initial commit
1 parent 5eb36bf commit 1a2a396

15 files changed

+402
-0
lines changed

.envrc.dist

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export SERVER_API_KEY=""
2+
export IFRAMELY_API_KEY=""

.github/workflows/build.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: build
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- '*'
8+
tags-ignore:
9+
- '*'
10+
11+
jobs:
12+
test:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v4
17+
18+
- name: Set up Go
19+
uses: actions/setup-go@v5
20+
with:
21+
go-version: 1.23
22+
23+
- name: Build
24+
run: go build
25+
26+
- name: Run tests
27+
run: go test -v ./...
28+

.github/workflows/release.yml

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: release
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*.*.*'
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v4
14+
15+
- name: Set up Go
16+
uses: actions/setup-go@v5
17+
with:
18+
go-version: 1.23
19+
20+
- name: Build
21+
run: go build
22+
23+
- name: Run tests
24+
run: go test -v ./...
25+
26+
release:
27+
runs-on: ubuntu-latest
28+
needs: test
29+
steps:
30+
- name: Checkout
31+
uses: actions/checkout@v4
32+
with:
33+
fetch-depth: 0
34+
35+
- name: Set up Go
36+
uses: actions/setup-go@v5
37+
with:
38+
go-version: 1.23
39+
40+
- name: Set up QEMU
41+
uses: docker/setup-qemu-action@v3
42+
43+
- name: Set up Docker Buildx
44+
uses: docker/setup-buildx-action@v3
45+
46+
- name: Login to GitHub Container Registry
47+
uses: docker/login-action@v3
48+
with:
49+
registry: ghcr.io
50+
username: ${{ github.actor }}
51+
password: ${{ secrets.GITHUB_TOKEN }}
52+
53+
- name: Run GoReleaser
54+
uses: goreleaser/goreleaser-action@v5
55+
with:
56+
distribution: goreleaser
57+
version: latest
58+
args: release --clean
59+
env:
60+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.envrc
2+
dist
3+
4+
### Go template
5+
# If you prefer the allow list template instead of the deny list, see community template:
6+
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
7+
#
8+
# Binaries for programs and plugins
9+
*.exe
10+
*.exe~
11+
*.dll
12+
*.so
13+
*.dylib
14+
15+
# Test binary, built with `go test -c`
16+
*.test
17+
18+
# Output of the go coverage tool, specifically when used with LiteIDE
19+
*.out
20+
21+
# Dependency directories (remove the comment below to include it)
22+
# vendor/
23+
24+
# Go workspace file
25+
go.work
26+

.golangci.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
linters-settings:
2+
govet:
3+
enable-all: true
4+
disable:
5+
- fieldalignment
6+
7+
sloglint:
8+
# Enforce not mixing key-value pairs and attributes.
9+
# Default: true
10+
no-mixed-args: false
11+
# Enforce using key-value pairs only (overrides no-mixed-args, incompatible with attr-only).
12+
# Default: false
13+
kv-only: true
14+
# Enforce using attributes only (overrides no-mixed-args, incompatible with kv-only).
15+
# Default: false
16+
attr-only: true
17+
# Enforce using methods that accept a context.
18+
# Default: false
19+
context: all
20+
# Enforce using static values for log messages.
21+
# Default: false
22+
static-msg: true
23+
# Enforce using constants instead of raw keys.
24+
# Default: false
25+
no-raw-keys: true
26+
# Enforce a single key naming convention.
27+
# Values: snake, kebab, camel, pascal
28+
# Default: ""
29+
key-naming-case: snake
30+
# Enforce putting arguments on separate lines.
31+
# Default: false
32+
args-on-sep-lines: true

.goreleaser.yml

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
before:
2+
hooks:
3+
- go mod tidy
4+
builds:
5+
- env:
6+
- CGO_ENABLED=0
7+
binary: iframely-server
8+
ldflags:
9+
- -s -w
10+
goos:
11+
- darwin
12+
- linux
13+
goarch:
14+
- amd64
15+
- arm64
16+
checksum:
17+
name_template: 'checksums.txt'
18+
dockers:
19+
- dockerfile: Dockerfile
20+
use: buildx
21+
build_flag_templates:
22+
- "--platform=linux/amd64"
23+
- "--pull"
24+
image_templates:
25+
- "ghcr.io/emgag/iframely-server:{{ .Tag }}-amd64"
26+
goos: linux
27+
goarch: amd64
28+
ids:
29+
- iframely-server
30+
- dockerfile: Dockerfile
31+
use: buildx
32+
build_flag_templates:
33+
- "--platform=linux/arm64"
34+
- "--pull"
35+
image_templates:
36+
- "ghcr.io/emgag/iframely-server:{{ .Tag }}-arm64"
37+
goos: linux
38+
goarch: arm64
39+
ids:
40+
- iframely-server
41+
docker_manifests:
42+
- name_template: ghcr.io/emgag/iframely-server:{{ .Tag }}
43+
image_templates:
44+
- "ghcr.io/emgag/iframely-server:{{ .Tag }}-amd64"
45+
- "ghcr.io/emgag/iframely-server:{{ .Tag }}-arm64"
46+
- name_template: ghcr.io/emgag/iframely-server:latest
47+
image_templates:
48+
- "ghcr.io/emgag/iframely-server:{{ .Tag }}-amd64"
49+
- "ghcr.io/emgag/iframely-server:{{ .Tag }}-arm64"
50+

.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/iframely-server.iml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Dockerfile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM cgr.dev/chainguard/static:latest
2+
LABEL org.opencontainers.image.source = "https://github.com/emgag/iframely-server"
3+
4+
COPY /iframely-server /iframely-server
5+
6+
CMD ["/iframely-server"]

go.mod

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module github.com/emgag/iframely-server
2+
3+
go 1.23.0
4+
5+
require github.com/gofiber/fiber/v2 v2.52.5
6+
7+
require (
8+
github.com/andybalholm/brotli v1.1.0 // indirect
9+
github.com/google/uuid v1.6.0 // indirect
10+
github.com/k0kubun/pp/v3 v3.2.0 // indirect
11+
github.com/klauspost/compress v1.17.9 // indirect
12+
github.com/mattn/go-colorable v0.1.13 // indirect
13+
github.com/mattn/go-isatty v0.0.20 // indirect
14+
github.com/mattn/go-runewidth v0.0.16 // indirect
15+
github.com/rivo/uniseg v0.4.7 // indirect
16+
github.com/valyala/bytebufferpool v1.0.0 // indirect
17+
github.com/valyala/fasthttp v1.55.0 // indirect
18+
github.com/valyala/tcplisten v1.0.0 // indirect
19+
golang.org/x/sys v0.24.0 // indirect
20+
golang.org/x/text v0.16.0 // indirect
21+
)

go.sum

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
2+
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
3+
github.com/gofiber/fiber/v2 v2.52.5 h1:tWoP1MJQjGEe4GB5TUGOi7P2E0ZMMRx5ZTG4rT+yGMo=
4+
github.com/gofiber/fiber/v2 v2.52.5/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ=
5+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
6+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
7+
github.com/k0kubun/pp/v3 v3.2.0 h1:h33hNTZ9nVFNP3u2Fsgz8JXiF5JINoZfFq4SvKJwNcs=
8+
github.com/k0kubun/pp/v3 v3.2.0/go.mod h1:ODtJQbQcIRfAD3N+theGCV1m/CBxweERz2dapdz1EwA=
9+
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
10+
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
11+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
12+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
13+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
14+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
15+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
16+
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
17+
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
18+
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
19+
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
20+
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
21+
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
22+
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
23+
github.com/valyala/fasthttp v1.55.0 h1:Zkefzgt6a7+bVKHnu/YaYSOPfNYNisSVBo/unVCf8k8=
24+
github.com/valyala/fasthttp v1.55.0/go.mod h1:NkY9JtkrpPKmgwV3HTaS2HWaJss9RSIsRVfcxxoHiOM=
25+
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
26+
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
27+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
28+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
29+
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
30+
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
31+
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
32+
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=

main.go

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"crypto/subtle"
6+
"encoding/json"
7+
"fmt"
8+
"io"
9+
"log"
10+
"net/http"
11+
"net/url"
12+
"os"
13+
"strings"
14+
"time"
15+
16+
"github.com/gofiber/fiber/v2"
17+
)
18+
19+
func main() {
20+
apiKey := os.Getenv("SERVER_API_KEY")
21+
iframelyKey := os.Getenv("IFRAMELY_API_KEY")
22+
23+
if apiKey == "" || iframelyKey == "" {
24+
fmt.Println("Environment variables SERVER_API_KEY and IFRAMELY_API_KEY must be set")
25+
return
26+
}
27+
28+
app := fiber.New()
29+
30+
app.Get("/info", func(c *fiber.Ctx) error {
31+
target := c.Query("url")
32+
key := c.Query("api_key")
33+
34+
if subtle.ConstantTimeCompare([]byte(key), []byte(apiKey)) == 0 {
35+
return fiber.NewError(fiber.StatusBadRequest, "Invalid API key")
36+
}
37+
38+
u, err := url.Parse("https://iframe.ly/api/oembed")
39+
query := u.Query()
40+
query.Add("api_key", iframelyKey)
41+
query.Add("iframe", "0")
42+
query.Add("url", target)
43+
u.RawQuery = query.Encode()
44+
45+
ctx, cancel := context.WithTimeout(c.Context(), time.Duration(time.Second))
46+
defer cancel()
47+
48+
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
49+
req = req.WithContext(ctx)
50+
51+
client := &http.Client{}
52+
resp, err := client.Do(req)
53+
54+
if err != nil {
55+
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
56+
}
57+
58+
defer resp.Body.Close()
59+
60+
if resp.StatusCode != http.StatusOK {
61+
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("failed to fetch data: %s", resp.Status))
62+
}
63+
64+
body, err := io.ReadAll(resp.Body)
65+
66+
if err != nil {
67+
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
68+
}
69+
70+
var iframelyResp IframelyResponse
71+
err = json.Unmarshal(body, &iframelyResp)
72+
73+
if err != nil {
74+
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
75+
}
76+
77+
return c.JSON(Response{
78+
Provider: strings.ToLower(iframelyResp.ProviderName),
79+
URL: iframelyResp.URL,
80+
HTML: iframelyResp.HTML,
81+
Image: iframelyResp.ThumbnailURL,
82+
Error: iframelyResp.Error,
83+
})
84+
})
85+
86+
err := app.Listen(":3000")
87+
log.Fatal(err)
88+
}

0 commit comments

Comments
 (0)