From e305d3d4c37f5af2cc30fa9cbda19e76a7149dee Mon Sep 17 00:00:00 2001 From: SuTang Date: Mon, 8 Jan 2024 11:23:08 +0800 Subject: [PATCH] feat: init. --- .env | 9 ++ .gitignore | 12 +++ api/img2color.go | 249 +++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 30 ++++++ go.sum | 92 +++++++++++++++++ readme.md | 54 ++++++++++ vercel.json | 8 ++ 7 files changed, 454 insertions(+) create mode 100644 .env create mode 100644 .gitignore create mode 100644 api/img2color.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 readme.md create mode 100644 vercel.json diff --git a/.env b/.env new file mode 100644 index 0000000..c64f024 --- /dev/null +++ b/.env @@ -0,0 +1,9 @@ +REDIS_ADDRESS=${REDIS_ADDRESS} +REDIS_PASSWORD=${REDIS_PASSWORD} +USE_REDIS_CACHE=${USE_REDIS_CACHE} +REDIS_DB=${REDIS_DB} +USE_MONGODB=${USE_MONGODB} +MONGO_URI=${MONGO_URI} +MONGO_DB=${MONGO_DB} +PORT=${PORT} +ALLOWED_REFERERS=${ALLOWED_REFERERS} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c8fd758 --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# Folders +_obj/ +_test/ + +# IDE +.idea/ +.vscode/ diff --git a/api/img2color.go b/api/img2color.go new file mode 100644 index 0000000..3384c10 --- /dev/null +++ b/api/img2color.go @@ -0,0 +1,249 @@ +package handler + +import ( + "crypto/md5" + "encoding/base64" + "encoding/json" + "fmt" + "image" + "log" + "net/http" + "os" + "path/filepath" + "regexp" + "strconv" + "strings" + + "github.com/go-redis/redis/v8" + "github.com/joho/godotenv" + "github.com/lucasb-eyer/go-colorful" + "github.com/nfnt/resize" + "github.com/disintegration/imaging" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" + "golang.org/x/net/context" +) + +var redisClient *redis.Client +var mongoClient *mongo.Client +var cacheEnabled bool +var useMongoDB bool +var redisDB int +var mongoDB string +var ctx = context.Background() +var colorsCollection *mongo.Collection +var allowedReferers []string + +func init() { + currentDir, err := os.Getwd() + if err != nil { + fmt.Printf("获取当前工作目录路径时出错:%v\n", err) + return + } + + envFile := filepath.Join(currentDir, ".env") + + err = godotenv.Load(envFile) + if err != nil { + fmt.Printf("加载 .env 文件时出错:%v\n", err) + return + } + + redisAddr := os.Getenv("REDIS_ADDRESS") + redisPassword := os.Getenv("REDIS_PASSWORD") + cacheEnabledStr := os.Getenv("USE_REDIS_CACHE") + redisDBStr := os.Getenv("REDIS_DB") + mongoDB = os.Getenv("MONGO_DB") + mongoURI := os.Getenv("MONGO_URI") + referers := os.Getenv("ALLOWED_REFERERS") + + redisDB, err = strconv.Atoi(redisDBStr) + if err != nil { + redisDB = 0 + } + + redisClient = redis.NewClient(&redis.Options{ + Addr: redisAddr, + Password: redisPassword, + DB: redisDB, + }) + + cacheEnabled = cacheEnabledStr == "true" + + useMongoDBStr := os.Getenv("USE_MONGODB") + useMongoDB = useMongoDBStr == "true" + if useMongoDB { + log.Println("连接到MongoDB...") + clientOptions := options.Client().ApplyURI(mongoURI) + mongoClient, err = mongo.Connect(ctx, clientOptions) + if err != nil { + log.Fatalf("连接到MongoDB时出错:%v", err) + } + log.Println("已连接到MongoDB!") + + colorsCollection = mongoClient.Database(mongoDB).Collection("colors") + } + + allowedReferers = parseReferers(referers) +} + +func calculateMD5Hash(data []byte) string { + hash := md5.Sum(data) + return base64.StdEncoding.EncodeToString(hash[:]) +} + +func extractMainColor(imgURL string) (string, error) { + md5Hash := calculateMD5Hash([]byte(imgURL)) + + if cacheEnabled && redisClient != nil { + cachedColor, err := redisClient.Get(ctx, md5Hash).Result() + if err == nil && cachedColor != "" { + return cachedColor, nil + } + } + + req, err := http.NewRequest("GET", imgURL, nil) + if err != nil { + return "", err + } + + req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.253") + + client := http.DefaultClient + resp, err := client.Do(req) + if err != nil { + return "", err + } + defer resp.Body.Close() + + var img image.Image + + img, err = imaging.Decode(resp.Body) + + if err != nil { + return "", err + } + + img = resize.Resize(50, 0, img, resize.Lanczos3) + + bounds := img.Bounds() + var r, g, b uint32 + for y := bounds.Min.Y; y < bounds.Max.Y; y++ { + for x := bounds.Min.X; x < bounds.Max.X; x++ { + c := img.At(x, y) + r0, g0, b0, _ := c.RGBA() + r += r0 + g += g0 + b += b0 + } + } + + totalPixels := uint32(bounds.Dx() * bounds.Dy()) + averageR := r / totalPixels + averageG := g / totalPixels + averageB := b / totalPixels + + mainColor := colorful.Color{R: float64(averageR) / 0xFFFF, G: float64(averageG) / 0xFFFF, B: float64(averageB) / 0xFFFF} + + colorHex := mainColor.Hex() + + if cacheEnabled && redisClient != nil { + _, err := redisClient.Set(ctx, md5Hash, colorHex, 0).Result() + if err != nil { + log.Printf("将结果存储在缓存中时出错:%v\n", err) + } + } + + if useMongoDB && colorsCollection != nil { + _, err := colorsCollection.InsertOne(ctx, bson.M{ + "url": imgURL, + "color": colorHex, + }) + if err != nil { + log.Printf("将结果存储在MongoDB中时出错:%v\n", err) + } + } + + return colorHex, nil +} + +func handleImageColor(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS") + w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Referer") + + if r.Method == http.MethodOptions { + w.WriteHeader(http.StatusOK) + return + } + + referer := r.Header.Get("Referer") + if !isRefererAllowed(referer) { + http.Error(w, "禁止访问", http.StatusForbidden) + return + } + + imgURL := r.URL.Query().Get("img") + if imgURL == "" { + http.Error(w, "缺少img参数", http.StatusBadRequest) + return + } + + color, err := extractMainColor(imgURL) + if err != nil { + http.Error(w, fmt.Sprintf("提取主色调失败:%v", err), http.StatusInternalServerError) + return + } + + data := map[string]string{ + "RGB": color, + } + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(data) +} + +func Handler(w http.ResponseWriter, r *http.Request) { + handleImageColor(w, r) +} + +func parseReferers(referers string) []string { + refererList := strings.Split(referers, ",") + for i, referer := range refererList { + refererList[i] = strings.TrimSpace(referer) + } + return refererList +} + +func isRefererAllowed(referer string) bool { + if len(allowedReferers) == 0 { + return true + } + + for _, allowedReferer := range allowedReferers { + allowedReferer = strings.ReplaceAll(allowedReferer, ".", "\\.") + allowedReferer = strings.ReplaceAll(allowedReferer, "*", ".*") + match, _ := regexp.MatchString(allowedReferer, referer) + if match { + return true + } + } + + return false +} + +func main() { + http.HandleFunc("/api", Handler) + + port := os.Getenv("PORT") + if port == "" { + port = "3000" + } + + log.Printf("服务器监听在:%s...\n", port) + err := http.ListenAndServe(":"+port, nil) + if err != nil { + log.Fatalf("启动服务器时出错:%v\n", err) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..16982e1 --- /dev/null +++ b/go.mod @@ -0,0 +1,30 @@ +module img2color + +go 1.20 + +require ( + github.com/chai2010/webp v1.1.1 + github.com/disintegration/imaging v1.6.2 + github.com/go-redis/redis/v8 v8.11.5 + github.com/joho/godotenv v1.5.1 + github.com/lucasb-eyer/go-colorful v1.2.0 + github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 + go.mongodb.org/mongo-driver v1.12.0 + golang.org/x/net v0.12.0 +) + +require ( + github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/golang/snappy v0.0.4 // indirect + github.com/klauspost/compress v1.16.7 // indirect + github.com/montanaflynn/stats v0.7.1 // indirect + github.com/xdg-go/pbkdf2 v1.0.0 // indirect + github.com/xdg-go/scram v1.1.2 // indirect + github.com/xdg-go/stringprep v1.0.4 // indirect + github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect + golang.org/x/crypto v0.11.0 // indirect + golang.org/x/image v0.0.0-20211028202545-6944b10bf410 // indirect + golang.org/x/sync v0.3.0 // indirect + golang.org/x/text v0.11.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..19dcb4e --- /dev/null +++ b/go.sum @@ -0,0 +1,92 @@ +github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/chai2010/webp v1.1.1 h1:jTRmEccAJ4MGrhFOrPMpNGIJ/eybIgwKpcACsrTEapk= +github.com/chai2010/webp v1.1.1/go.mod h1:0XVwvZWdjjdxpUEIf7b9g9VkHFnInUSYujwqTLEuldU= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c= +github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4= +github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= +github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI= +github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo= +github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM= +github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= +github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I= +github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= +github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= +github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE= +github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow= +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ= +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8= +github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= +github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= +github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE= +github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= +github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= +github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY= +github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4= +github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8= +github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM= +github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= +github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a h1:fZHgsYlfvtyqToslyjUt3VOPF4J7aK/3MPcK7xp3PDk= +github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a/go.mod h1:ul22v+Nro/R083muKhosV54bj5niojjWZvU8xrevuH4= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +go.mongodb.org/mongo-driver v1.12.0 h1:aPx33jmn/rQuJXPQLZQ8NtfPQG8CaqgLThFtqRb0PiE= +go.mongodb.org/mongo-driver v1.12.0/go.mod h1:AZkxhPnFJUoH7kZlFkVKucV20K387miPfm7oimrSmK0= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA= +golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio= +golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/image v0.0.0-20211028202545-6944b10bf410 h1:hTftEOvwiOq2+O8k2D5/Q7COC7k5Qcrgc2TFURJYnvQ= +golang.org/x/image v0.0.0-20211028202545-6944b10bf410/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +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-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= +golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/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.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +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/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..df110f1 --- /dev/null +++ b/readme.md @@ -0,0 +1,54 @@ +# Img2color + +本项目使用go作为基础,具有较高的性能 + +支持vercel与服务器部署 + +## vercel部署 + +1. 点击项目右上角fork叉子 + +2. 登录[vercel](https://vercel.com/) + +3. 在[vercel](https://vercel.com/)导入项目 + +4. 部署时添加环境变量 + +5. 国内访问需绑定自定义域名 + +## 服务器部署 + +需要go环境 + +1. 安装依赖 +```bash +go mod tidy +``` +2. 运行 +``` +go run /api/img2color.go +``` +此处不赘述守护进程。 + +## 使用 + +例如:https://img2color-go.vercel.app/api?img=https://npm.elemecdn.com/anzhiyu-blog@1.1.6/img/post/banner/神里.webp + +部署后只需要 域名/api 访问 + +必填参数img: url + +.env文件配置说明 + + +| 配置项 | 说明 | +|-------------------------|--------------------------------------| +| REDIS_ADDRESS | REDIS地址 | +| REDIS_PASSWORD | REDIS密码 | +| USE_REDIS_CACHE | bool值,是否启用REDIS | +| REDIS_DB | REDIS数据库名 | +| USE_MONGODB | bool值,是否启用mongodb | +| MONGO_URI | mongodb地址 | +| MONGO_DB | mongodb数据库名 | +| PORT | 端口 | +| ALLOWED_REFERERS | 允许的refer域名,支持通配符,如果有多个地址可以用英文半角,隔开 | diff --git a/vercel.json b/vercel.json new file mode 100644 index 0000000..c83e022 --- /dev/null +++ b/vercel.json @@ -0,0 +1,8 @@ +{ + "routes": [ + { + "src": "/api", + "dest": "/api/img2color.go" + } + ] +} \ No newline at end of file