-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (84 loc) · 2.13 KB
/
main.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**********************
* Golang Web Project
* Dev. Dongwon Paek
***********************/
package main
import (
"context"
"fmt"
"goWeb/handler"
"net/http"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/labstack/gommon/log"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Trainer struct {
Name string
Password string
}
func hello(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
}
func MainPage(c echo.Context) error {
return c.String(http.StatusOK, "This is main page. Nothing else.")
}
func main() {
// create instance
e := echo.New()
e.Logger.SetLevel(log.ERROR)
// use middleware for logging
e.Use(middleware.Logger())
e.Use(middleware.JWTWithConfig(middleware.JWTConfig{
SigningKey: func(c echo.Context) bool {
// Skip authentication for and signup login request
if c.Path() == "/login" || c.Path() == "/signup" {
return true
}
return false
},
}))
// connect to mongoDB
// db, err := mgo.Dial("mongo")
// // if err != nil {
// // e.Logger.Fatal(err)
// // }
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
db, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
log.Fatal(err)
}
err = db.Ping(context.TODO(), nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("Connected to MongoDB! :)")
// create index
// if err = db.Copy().DB("bird").C("users").EnsureIndex(mgo.Index{
// Key: []string{"email"},
// Unique: true,
// }); err != nil {
// log.Fatal(err)
// }
collection := db.Database("test").Collection("trainers")
bear := Trainer{"Bear", "12345"}
insertRes, err := collection.InsertOne(context.TODO(), bear)
if err != nil {
log.Fatal(err)
}
fmt.Println("Inserted a single user: ", insertRes.InsertedID)
// Init handler
h := &handler.Handler{DB: db}
// Routing
e.GET("/", MainPage)
e.GET("/hello", hello)
e.POST("/signup", h.Signup)
e.POST("/login", h.Login)
e.POST("/follow/:id", h.Follow)
//e.GET("/product", h.Product)
e.GET("/feed", h.FetchPost)
e.POST("/posts", h.CreatePost)
// Start Server
e.Logger.Fatal(e.Start(":3000"))
}