This repository was archived by the owner on Aug 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapi.go
More file actions
42 lines (35 loc) · 1.41 KB
/
Copy pathapi.go
File metadata and controls
42 lines (35 loc) · 1.41 KB
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
package main
import (
"context"
"github.com/gorilla/mux"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
"log"
"net/http"
"os"
"time"
)
func main() {
log.Println("Initializing API")
// initialize and validate database connection
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
client, err := mongo.Connect(ctx, options.Client().ApplyURI(os.Getenv("MONGO_URI")))
if err != nil {
log.Fatal("Unable to create database connection: ", err.Error())
}
if err := client.Ping(ctx, readpref.Primary()); err != nil {
log.Fatal("Unable to connect to database: ", err.Error())
}
router := mux.NewRouter()
// Register endpoints
router.HandleFunc("/resources", searchResourcesHandler(client)).Methods(http.MethodGet, http.MethodOptions)
router.HandleFunc("/resources/{resource-id}/content", viewResourceContentHandler(client)).Methods(http.MethodGet, http.MethodOptions)
router.HandleFunc("/resources-stream", resourcesStreamHandler(client)).Methods(http.MethodGet, http.MethodOptions)
router.HandleFunc("/crawled-urls", getCrawledUrls(client)).Methods(http.MethodGet, http.MethodOptions)
router.Use(mux.CORSMethodMiddleware(router))
log.Println("API will listen on: http://0.0.0.0:8080")
if err := http.ListenAndServe("0.0.0.0:8080", router); err != nil {
log.Println("fatal error while trying to listen: " + err.Error())
}
}