Skip to content

Commit

Permalink
add Mongodb support
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoMengXinX committed Mar 5, 2022
1 parent 52f4e59 commit 307a477
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 14 deletions.
85 changes: 77 additions & 8 deletions api/feed.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
package api

import (
"context"
"coolapk"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
)

type feedData struct {
ID string `bson:"id"`
ShareUrl string `bson:"share_url"`
Message string `bson:"message"`
ReqTimes int64 `bson:"requested_times"`
CreatedAt time.Time `bson:"created_at"`
}

var collection *mongo.Collection
var html = `
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
Expand All @@ -18,27 +33,81 @@ var html = `
</head>
`

func connectDB(uri string) (*mongo.Client, error) {
serverAPIOptions := options.ServerAPI(options.ServerAPIVersion1)
clientOptions := options.Client().
ApplyURI(uri).
SetServerAPIOptions(serverAPIOptions)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
return mongo.Connect(ctx, clientOptions)
}

func init() {
if os.Getenv("MONGO_URI") != "" {
client, err := connectDB(os.Getenv("MONGO_URI"))
if err == nil {
collection = client.Database("coolapk").Collection("feeds")
}
}
}

func UrlHandler(w http.ResponseWriter, r *http.Request) {
if len(r.URL.Path) <= 1 {
_, _ = fmt.Fprintf(w, "Invaid Feed ID")
return
}
feedID, _ := strconv.Atoi(strings.Split(strings.Trim(r.URL.Path[1:], "/feed/"), "?")[0])
feedDetail, err := coolapk.GetFeedDetail(feedID)
feedID, err := strconv.Atoi(strings.Split(strings.Trim(r.URL.Path[1:], "/feed/"), "?")[0])
if err != nil {
w.WriteHeader(500)
_, _ = fmt.Fprintf(w, "Internal Error")
_, _ = fmt.Fprintf(w, "Invaid Feed ID")
return
}
if feedDetail.Data.ShareUrl == "" {
_, _ = fmt.Fprintf(w, "Invaid Feed ID")

ctx, cancel := context.WithTimeout(context.TODO(), time.Second*2)
defer cancel()

var data feedData
if collection != nil {
err = collection.FindOne(ctx, bson.M{"id": fmt.Sprintf("%d", feedID)}).Decode(&data)
}

if err != nil || collection == nil {
ctx, cancel := context.WithTimeout(context.TODO(), time.Second*5)
defer cancel()

feedDetail, err := coolapk.GetFeedDetailWithContext(feedID, ctx)
if err != nil { // 超时刷新重试
scheme := "http://"
if r.TLS != nil {
scheme = "https://"
}
url := scheme + r.Host + r.RequestURI
http.Redirect(w, r, url, http.StatusMovedPermanently)
return
}
if feedDetail.Data.ShareUrl == "" {
_, _ = fmt.Fprintf(w, "Invaid Feed ID")
return
}

data.ID = fmt.Sprintf("%d", feedID)
data.ShareUrl = feedDetail.Data.ShareUrl
data.Message = feedDetail.Data.Message
data.CreatedAt = time.Now()
} else {
data.ReqTimes++
}

if collection != nil {
_, err = collection.UpdateOne(ctx, bson.M{"id": data.ID}, bson.M{"$set": data}, options.Update().SetUpsert(true))
}

if strings.Contains(r.UserAgent(), "bot") || strings.Contains(r.UserAgent(), "Bot") {
re := regexp.MustCompile("\\<[\\S\\s]+?\\>")
message := re.ReplaceAllString(feedDetail.Data.Message, "")
message := re.ReplaceAllString(data.Message, "")
_, _ = fmt.Fprintf(w, fmt.Sprintf(html, message))
} else {
http.Redirect(w, r, feedDetail.Data.ShareUrl, http.StatusMovedPermanently)
http.Redirect(w, r, data.ShareUrl, http.StatusMovedPermanently)
}
return
}
9 changes: 7 additions & 2 deletions coolapk/api.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package coolapk

import (
"context"
"encoding/json"
"fmt"
)

func GetFeedDetail(id int) (result FeedData, err error) {
func GetFeedDetailWithContext(id int, ctx context.Context) (result FeedData, err error) {
path := "/v6/feed/detail"
parameters := make(map[string]string)
parameters["id"] = fmt.Sprintf("%d", id)
response, err := request(path, parameters)
response, err := request(path, parameters, ctx)
if err != nil {
return
}
err = json.Unmarshal(response, &result)
return
}

func GetFeedDetail(id int) (result FeedData, err error) {
return GetFeedDetailWithContext(id, context.Background())
}
5 changes: 3 additions & 2 deletions coolapk/request.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package coolapk

import (
"context"
"io/ioutil"
"net/http"
"net/url"
)

var userAgent = `Dalvik/2.1.0 (Linux; U; Android 11) +CoolMarket/10.5.3-2009271`

func request(path string, paramters map[string]string) (response []byte, err error) {
func request(path string, paramters map[string]string, ctx context.Context) (response []byte, err error) {
params := url.Values{}
for key, value := range paramters {
params.Add(key, value)
}
client := &http.Client{}
req, err := http.NewRequest("GET", "https://api.coolapk.com"+path+"?"+params.Encode(), nil)
req, err := http.NewRequestWithContext(ctx, "GET", "https://api.coolapk.com"+path+"?"+params.Encode(), nil)
if err != nil {
return
}
Expand Down
20 changes: 18 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ go 1.17

replace coolapk => ./coolapk

require coolapk v0.0.0-00010101000000-000000000000
require (
coolapk v0.0.0-00010101000000-000000000000
go.mongodb.org/mongo-driver v1.8.4
)

require github.com/google/uuid v1.3.0 // indirect
require (
github.com/go-stack/stack v1.8.0 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.0.2 // indirect
github.com/xdg-go/stringprep v1.0.2 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f // indirect
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
golang.org/x/text v0.3.5 // indirect
)
56 changes: 56 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,58 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
github.com/golang/snappy v0.0.1/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/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
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.0.2 h1:akYIkZ28e6A96dkWNJQu3nmCzH3YfwMPQExUYDaRv7w=
github.com/xdg-go/scram v1.0.2/go.mod h1:1WAq6h33pAW+iRreB34OORO2Nf7qel3VV3fjBj+hCSs=
github.com/xdg-go/stringprep v1.0.2 h1:6iq84/ryjjeRmMJwxutI51F2GIPlP5BfTvXHeYjyhBc=
github.com/xdg-go/stringprep v1.0.2/go.mod h1:8F9zXuvzgwmyT5DUm4GUfZGDdT3W+LCvS6+da4O5kxM=
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA=
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
go.mongodb.org/mongo-driver v1.8.4 h1:NruvZPPL0PBcRJKmbswoWSrmHeUvzdxA3GCPfD/NEOA=
go.mongodb.org/mongo-driver v1.8.4/go.mod h1:0sQWfOeY63QTntERDJJ/0SuKK0T1uVSgKCuAROlKEPY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f h1:aZp0e2vLN4MToVqnjNEYEtrEA8RH8U8FN1CU7JgqsPU=
golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
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/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

1 comment on commit 307a477

@vercel
Copy link

@vercel vercel bot commented on 307a477 Mar 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.