-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
165 lines (148 loc) · 5.29 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"strings"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
"github.com/valyala/fasthttp"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
var client *minio.Client
var database *sql.DB
var template string = "<!DOCTYPE html><html style=\"height:100%%\"><head><meta name=\"viewport\" content=\"width=device-width,minimum-scale=0.1\"><meta name=\"twitter:card\" content=\"summary_large_image\"><meta property=\"og:description\" content=\"%s\"><meta property=\"twitter:image\" content=\"%s\"><meta name=\"theme-color\" content=\"%s\"><link type=\"application/json+oembed\" href=\"%s\"></head><body style=\"margin: 0px;background: #0e0e0e;display:flex;justify-content:center;height:100%%\"><img style=\"-webkit-user-select:none;margin:auto\" src=\"%s\"></body></html>"
// OEmbed smthn oembed stuff
type OEmbed struct {
Type string `json:"type"`
Version string `json:"version"`
Title string `json:"title"`
Author string `json:"author_name,omitempty"`
}
func requestHandler(ctx *fasthttp.RequestCtx) {
requestPath := string(ctx.Path())
base := path.Base(requestPath)
host := "https://" + string(ctx.Request.Header.Peek("X-Forwarded-Host"))
if requestPath == "/" {
ctx.Redirect("https://pxl.blue/?utm_source=proxy", 301)
} else if path.Ext(requestPath) == ".json" && strings.HasPrefix(base, "em") && !strings.Contains(requestPath, "raw") {
// oembed route
var (
embed bool
embedAuthor bool
embedAuthorStr string
embedTitle string
embedDescription string
embedColor string
)
imgPath := base[0 : len(base)-5]
err := database.QueryRow("SELECT \"embed\", \"embedAuthor\", \"embedAuthorStr\", \"embedTitle\", \"embedDescription\", \"embedColor\" FROM pxl.public.\"image\" WHERE \"path\" = $1", imgPath).Scan(&embed, &embedAuthor, &embedAuthorStr, &embedTitle, &embedDescription, &embedColor)
if err != nil {
ctx.Error("Image not found in database", 404)
ctx.Done()
return
}
oembed := OEmbed{Type: "link", Version: "1.0", Title: embedTitle, Author: embedAuthorStr}
if !embedAuthor {
oembed.Author = ""
}
var embedData []byte
embedData, err = json.Marshal(oembed)
if err != nil {
log.Fatalf("Error in json.Marshal: %v", err)
}
ctx.SetBody(embedData)
ctx.SetContentType("application/json; charset=utf-8")
ctx.Done()
} else if path.Ext(requestPath) != "" && strings.HasPrefix(base, "em") && !strings.Contains(requestPath, "raw") {
var (
embed bool
embedDescription string
embedColor string
)
err := database.QueryRow("SELECT \"embed\", \"embedDescription\", \"embedColor\" FROM pxl.public.\"image\" WHERE \"path\" = $1", base).Scan(&embed, &embedDescription, &embedColor)
if err != nil {
ctx.Error("Image not found in database", 404)
ctx.Done()
return
}
if !embed {
ctx.Redirect("https://"+string(ctx.Host())+"/raw/"+base, 301)
return
}
ctx.SetContentType("text/html; charset=utf-8")
imgURL := fmt.Sprintf("%s/raw/%s", host, base)
html := fmt.Sprintf(template, embedDescription, imgURL, embedColor, fmt.Sprintf("%s/%s.json", host, base), imgURL)
ctx.SetBody([]byte(html))
ctx.Done()
} else if path.Ext(requestPath) != "" {
// Request file from GCS
//rc, err := client.Bucket(os.Getenv("STORAGE_BUCKET")).Object().NewReader(context.Background())
filePath := "/" + path.Base(requestPath)
objInfo, err := client.StatObject(context.Background(), os.Getenv("STORAGE_BUCKET"), filePath, minio.StatObjectOptions{})
if err != nil {
ctx.Error(fmt.Sprintf("Image not found (stat failed: %v)", err), 404)
ctx.Done()
return
}
rc, err := client.GetObject(context.Background(), os.Getenv("STORAGE_BUCKET"), filePath, minio.GetObjectOptions{})
if err != nil {
ctx.Error("Image not found", 404)
ctx.Done()
return
}
defer rc.Close()
data, err := ioutil.ReadAll(rc)
if err != nil {
ctx.Error(fmt.Sprintf("Error reading file from Storage (ioutil.ReadAll failed) (error: %v)", err), 500)
ctx.Done()
return
}
ctx.SetContentType(objInfo.ContentType)
ctx.SetBody(data)
ctx.Done()
} else {
var destination string
err := database.QueryRow("SELECT \"destination\" FROM pxl.public.\"short_url\" WHERE \"shortId\" = $1", base).Scan(&destination)
if err != nil {
ctx.Error("Short URL not found", 404)
ctx.Done()
return
}
ctx.Redirect(destination, 301)
}
}
func main() {
err := godotenv.Load()
if err != nil {
log.Printf("Error in godotenv.Load(): %v", err)
}
db, err := sql.Open("postgres", os.Getenv("PG_CONNSTRING"))
database = db
if err != nil {
log.Fatalf("Error in sql.Open: %v", err)
}
endpoint := os.Getenv("STORAGE_ENDPOINT")
accessKeyID := os.Getenv("STORAGE_ACCESS_KEY")
secretAccessKey := os.Getenv("STORAGE_SECRET_KEY")
useSSL := true
minioClient, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Secure: useSSL,
})
if err != nil {
log.Fatalf("Error in minio.New: %v", err)
}
client = minioClient
handler := fasthttp.CompressHandler(requestHandler)
if err := fasthttp.ListenAndServe(":"+os.Getenv("PORT"), handler); err != nil {
log.Fatalf("Error in ListenAndServe: %v", err)
}
log.Printf("Listening on port %s", os.Getenv("PORT"))
}