-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
44 lines (34 loc) · 1.11 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
package main
import (
"context"
"fmt"
"net/http"
"github.com/anthdm/ggcommerce/api"
"github.com/anthdm/ggcommerce/store"
"github.com/anthdm/weavebox"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func handleAPIError(ctx *weavebox.Context, err error) {
fmt.Println("API error:", err)
ctx.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
}
func main() {
app := weavebox.New()
app.ErrorHandler = handleAPIError
adminMW := &api.AdminAuthMiddleware{}
adminRoute := app.Box("/admin")
adminRoute.Use(adminMW.Authenticate)
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
panic(err)
}
productStore := store.NewMongoProductStore(client.Database("ggcommerce"))
productHandler := api.NewProductHandler(productStore)
// admin/product
adminProductRoute := adminRoute.Box("/product")
adminProductRoute.Get("/:id", productHandler.HandleGetProductByID)
adminProductRoute.Get("/", productHandler.HandleGetProducts)
adminProductRoute.Post("/", productHandler.HandlePostProduct)
app.Serve(3001)
}