Router accepts any required dependencies and returns the main server's handler.
(db sql.Database, secret string)
| 14 | |
| 15 | // Router accepts any required dependencies and returns the main server's handler. |
| 16 | func Router(db sql.Database, secret string) func(iris.Party) { |
| 17 | return func(r iris.Party) { |
| 18 | r.Use(requestid.New()) |
| 19 | |
| 20 | signer := jwt.NewSigner(jwt.HS256, secret, 15*time.Minute) |
| 21 | r.Get("/token", writeToken(signer)) |
| 22 | |
| 23 | verify := jwt.NewVerifier(jwt.HS256, secret).Verify(nil) |
| 24 | r.Use(verify) |
| 25 | // Generate a token for testing by navigating to |
| 26 | // http://localhost:8080/token endpoint. |
| 27 | // Copy-paste it to a ?token=$token url parameter or |
| 28 | // open postman and put an Authentication: Bearer $token to get |
| 29 | // access on create, update and delete endpoinds. |
| 30 | |
| 31 | var ( |
| 32 | categoryService = service.NewCategoryService(db) |
| 33 | productService = service.NewProductService(db) |
| 34 | ) |
| 35 | |
| 36 | cat := r.Party("/category") |
| 37 | { |
| 38 | // TODO: new Use to add middlewares to specific |
| 39 | // routes per METHOD ( we already have the per path through parties.) |
| 40 | handler := NewCategoryHandler(categoryService) |
| 41 | |
| 42 | cat.Get("/", handler.List) |
| 43 | cat.Post("/", handler.Create) |
| 44 | cat.Put("/", handler.Update) |
| 45 | |
| 46 | cat.Get("/{id:int64}", handler.GetByID) |
| 47 | cat.Patch("/{id:int64}", handler.PartialUpdate) |
| 48 | cat.Delete("/{id:int64}", handler.Delete) |
| 49 | /* You can also do something like that: |
| 50 | cat.PartyFunc("/{id:int64}", func(c iris.Party) { |
| 51 | c.Get("/", handler.GetByID) |
| 52 | c.Post("/", handler.PartialUpdate) |
| 53 | c.Delete("/", handler.Delete) |
| 54 | }) |
| 55 | */ |
| 56 | |
| 57 | cat.Get("/{id:int64}/products", handler.ListProducts) |
| 58 | cat.Post("/{id:int64}/products", handler.InsertProducts(productService)) |
| 59 | } |
| 60 | |
| 61 | prod := r.Party("/product") |
| 62 | { |
| 63 | handler := NewProductHandler(productService) |
| 64 | |
| 65 | prod.Get("/", handler.List) |
| 66 | prod.Post("/", handler.Create) |
| 67 | prod.Put("/", handler.Update) |
| 68 | |
| 69 | prod.Get("/{id:int64}", handler.GetByID) |
| 70 | prod.Patch("/{id:int64}", handler.PartialUpdate) |
| 71 | prod.Delete("/{id:int64}", handler.Delete) |
| 72 | } |
| 73 |
nothing calls this directly
no test coverage detected
searching dependent graphs…