-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
89 lines (68 loc) · 1.96 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
package main
import (
"context"
"flag"
"log"
"github.com/sxc/specialnight/db"
"github.com/gofiber/fiber/v2"
"github.com/sxc/specialnight/api"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
const dburi = "mongodb://localhost:27017"
const dbname = "specialnight"
const userColl = "users"
var config = fiber.Config{
// Override default error handler
ErrorHandler: func(c *fiber.Ctx, err error) error {
// // Status code defaults to 500
// code := fiber.StatusInternalServerError
// // Retrieve the custom status code if it's a *fiber.Error
// var e *fiber.Error
// if errors.As(err, &e) {
// code = e.Code
// }
// // Send custom error page
// err = ctx.Status(code).SendFile(fmt.Sprintf("./%d.html", code))
// if err != nil {
// // In case the SendFile fails
// return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
// }
// // Return from handler
// return nil
return c.JSON(map[string]string{
"error": err.Error(),
})
},
}
func main() {
// ctx := context.Background()
// coll := client.Database(dbname).Collection(userColl)
// user := types.User{
// FirstName: "John",
// LastName: "Appleseed",
// }
// fmt.Println(res)
// var john types.User
// if err := coll.FindOne(ctx, bson.M{}).Decode(&john); err != nil {
// log.Fatal(err)
// }
// fmt.Println(john)
listenAddr := flag.String("listenAddr", ":3000", "The listen address of the API server")
flag.Parse()
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(dburi))
if err != nil {
log.Fatal(err)
}
// _, err = coll.InsertOne(ctx, user)
// if err != nil {
// log.Fatal(err)
// }
// handlers initialization
userHandler := api.NewUserHandler(db.NewMongoUserStore(client))
app := fiber.New(config)
apiv1 := app.Group("/api/v1")
apiv1.Get("/user", userHandler.HandleGetUsers)
apiv1.Get("/user/:id", userHandler.HandleGetUser)
app.Listen(*listenAddr)
}