-
Notifications
You must be signed in to change notification settings - Fork 290
/
Copy pathserver.go
61 lines (53 loc) · 1.69 KB
/
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
package main
import (
echojwt "github.com/labstack/echo-jwt/v4"
echo "github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/echox/cookbook/twitter/handler"
"github.com/labstack/gommon/log"
"golang.org/x/net/context"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
func main() {
e := echo.New()
e.Logger.SetLevel(log.ERROR)
e.Use(middleware.Logger())
e.Use(echojwt.WithConfig(echojwt.Config{
SigningKey: []byte(handler.Key),
Skipper: func(c echo.Context) bool {
// Skip authentication for signup and login requests
if c.Path() == "/login" || c.Path() == "/signup" {
return true
}
return false
},
}))
// Mongo Database connection
// Use the SetServerAPIOptions() method to set the Stable API version to 1
serverAPI := options.ServerAPI(options.ServerAPIVersion1)
opts := options.Client().ApplyURI("mongoDBURI").SetServerAPIOptions(serverAPI)
// Create a new client and connect to the server
client, err := mongo.Connect(opts)
if err != nil {
e.Logger.Fatal(err)
}
// Create indices
if _, err = client.Database("twitter").Collection("users").Indexes().CreateOne(context.TODO(), mongo.IndexModel{
Keys: bson.D{{Key: "email", Value: 1}}, // Create an index on the "email" field
Options: options.Index().SetUnique(true), // Make the index unique
}); err != nil {
log.Fatal(err)
}
// Initialize handler
h := &handler.Handler{Client: client}
// Routes
e.POST("/signup", h.Signup)
e.POST("/login", h.Login)
e.POST("/follow/:id", h.Follow)
e.POST("/posts", h.CreatePost)
e.GET("/feed", h.FetchPost)
// Start server
e.Logger.Fatal(e.Start(":1323"))
}