github.com/abolfazlbeh/zhycan@v0.0.0-20230819144214-24cf38237387/pkg/http/http.go (about) 1 package http 2 3 import ( 4 "github.com/abolfazlbeh/zhycan/internal/http" 5 "github.com/gofiber/fiber/v2" 6 ) 7 8 // Http Methods 9 const ( 10 MethodGet = "GET" // RFC 7231, 4.3.1 11 MethodHead = "HEAD" // RFC 7231, 4.3.2 12 MethodPost = "POST" // RFC 7231, 4.3.3 13 MethodPut = "PUT" // RFC 7231, 4.3.4 14 MethodPatch = "PATCH" // RFC 5789 15 MethodDelete = "DELETE" // RFC 7231, 4.3.5 16 MethodConnect = "CONNECT" // RFC 7231, 4.3.6 17 MethodOptions = "OPTIONS" // RFC 7231, 4.3.7 18 MethodTrace = "TRACE" // RFC 7231, 4.3.8 19 methodUse = "USE" 20 ) 21 22 // HttpRoute - Structure of the route 23 type HttpRoute struct { 24 Method string 25 Path string 26 RouteName string 27 Versions []string 28 GroupNames []string 29 F *func(c *fiber.Ctx) error 30 Servers []string 31 } 32 33 // HttpGroup - Structure of the group 34 type HttpGroup struct { 35 GroupName string 36 F *func(c *fiber.Ctx) error 37 Groups []string 38 Servers []string 39 } 40 41 // AddHttpRouteByObj - add route by HttpRoute obj 42 func AddHttpRouteByObj(httpRoute HttpRoute) error { 43 return http.GetManager().AddRoute(httpRoute.Method, 44 httpRoute.Path, 45 *httpRoute.F, 46 httpRoute.RouteName, 47 httpRoute.Versions, 48 httpRoute.GroupNames, 49 httpRoute.Servers...) 50 } 51 52 // AddHttpRoute - Add route by parameters 53 func AddHttpRoute(method string, path string, f func(c *fiber.Ctx) error, routeName string, versions []string, groupNames []string, serverName ...string) error { 54 return http.GetManager().AddRoute(method, 55 path, 56 f, 57 routeName, 58 versions, 59 groupNames, 60 serverName...) 61 } 62 63 // AddBulkHttpRoutes - add bulk http routes to the server 64 func AddBulkHttpRoutes(httpRoutes []HttpRoute) error { 65 for _, httpRoute := range httpRoutes { 66 err := http.GetManager().AddRoute(httpRoute.Method, 67 httpRoute.Path, 68 *httpRoute.F, 69 httpRoute.RouteName, 70 httpRoute.Versions, 71 httpRoute.GroupNames, 72 httpRoute.Servers...) 73 if err != nil { 74 return err 75 } 76 } 77 return nil 78 } 79 80 // GetRouteByName - Get route by providing the route name from specific server 81 func GetRouteByName(routeName string, serverName ...string) (*fiber.Route, error) { 82 return http.GetManager().GetRouteByName(routeName, serverName...) 83 } 84 85 // AddHttpGroupByObj - add group by HttpGroup obj 86 func AddHttpGroupByObj(group HttpGroup) error { 87 return http.GetManager().AddGroup( 88 group.GroupName, 89 *group.F, 90 group.Groups, 91 group.Servers..., 92 ) 93 } 94 95 // AddHttpGroup - Add group by parameters 96 func AddHttpGroup(groupName string, f func(c *fiber.Ctx) error, groups []string, serverName ...string) error { 97 return http.GetManager().AddGroup(groupName, 98 f, groups, serverName...) 99 } 100 101 // AddBulkHttpGroups - add bulk http groups to the server 102 func AddBulkHttpGroups(httpGroups []HttpGroup) error { 103 for _, group := range httpGroups { 104 err := http.GetManager().AddGroup( 105 group.GroupName, 106 *group.F, 107 group.Groups, 108 group.Servers..., 109 ) 110 if err != nil { 111 return err 112 } 113 } 114 return nil 115 } 116 117 // AttachHttpErrorHandler - Attach http error handler to the manager 118 func AttachHttpErrorHandler(f func(ctx *fiber.Ctx, err error) error, serverNames ...string) error { 119 return http.GetManager().AttachErrorHandler(f, serverNames...) 120 }