-
Notifications
You must be signed in to change notification settings - Fork 0
/
altcrawlhq_server.go
77 lines (65 loc) · 1.73 KB
/
altcrawlhq_server.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
package altcrawlhqserver
import (
"context"
"fmt"
"net/http"
"os"
"time"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type FeedRequest struct {
Size int `json:"size"`
Strategy string `json:"strategy"`
}
var MONGODB_URI string = os.Getenv("MONGODB_URI")
var mongoDatabase *mongo.Database
const MONGODB_DB string = "crawlhq"
func connect_to_mongodb() {
fmt.Println("Connecting to MongoDB...")
fmt.Println("MONGODB_URI: len=", len(MONGODB_URI))
serverAPI := options.ServerAPI(options.ServerAPIVersion1)
opts := options.Client().ApplyURI(MONGODB_URI).SetServerAPIOptions(serverAPI).SetAppName("altcrawlhq").SetCompressors([]string{"zstd", "zlib", "snappy"})
fmt.Println("AppName: ", *opts.AppName)
fmt.Println("Compressors: ", opts.Compressors)
client, err := mongo.Connect(context.TODO(), opts)
if err != nil {
panic(err)
}
err = client.Ping(context.TODO(), nil)
if err != nil {
panic(err)
}
fmt.Println("Connected to MongoDB!")
db := client.Database(MONGODB_DB)
mongoDatabase = db
}
func init() {
connect_to_mongodb()
}
func ServeHTTP() {
g := gin.New()
g.GET("/", func(c *gin.Context) {
time.Sleep(1 * time.Second)
c.JSON(http.StatusNotFound, gin.H{
"error": "Not Found",
})
})
apiGroup := g.Group("/api")
{
projectsGroup := apiGroup.Group("/projects/:project/")
{
projectsGroup.POST("/urls", addHandler)
projectsGroup.GET("/urls", feedHandler)
projectsGroup.DELETE("/urls", finishHandler)
projectsGroup.POST("/seencheck", seencheckHandler)
projectsGroup.POST("/reset", resetHandler)
}
apiGroup.GET("/ws", websocketHandler)
apiGroup.GET("/online", onlineClientsHandler)
}
if err := g.Run(); err != nil {
panic(err)
}
}