-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoint.go
126 lines (117 loc) · 5.21 KB
/
endpoint.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Package intake provides HTTP routing utilities.
// This file contains the endpoint definition and convenience functions for
// creating endpoints for different HTTP methods.
package intake
import "net/http"
// endpoint represents a single HTTP route with its associated handler and middleware.
// An endpoint encapsulates all the information needed to register and handle
// a specific HTTP route, including its HTTP method, path, handler function,
// and any middleware specific to this endpoint.
type endpoint struct {
// Verb is the HTTP method (GET, POST, etc.)
Verb string
// Path is the URL path for this endpoint
Path string
// EndpointHandler is the main handler function for this endpoint
EndpointHandler http.HandlerFunc
// MiddlewareHandlers are the middleware functions specific to this endpoint
MiddlewareHandlers []MiddleWare
}
// NewEndpoint creates a new endpoint with the specified HTTP method, path, handler, and optional middleware.
// This is the general constructor function for creating endpoints. For convenience,
// method-specific constructors (GET, POST, etc.) are also provided.
//
// Parameters:
// - method: The HTTP method (GET, POST, PUT, etc.)
// - path: The URL path for this endpoint
// - endpointHandler: The handler function for this endpoint
// - mid: Optional middleware functions specific to this endpoint
//
// Returns:
// - A new endpoint instance configured with the provided parameters
func NewEndpoint(method, path string, endpointHandler http.HandlerFunc, mid ...MiddleWare) endpoint {
return endpoint{
Verb: method,
Path: path,
EndpointHandler: endpointHandler,
MiddlewareHandlers: mid,
}
}
// GET creates a new endpoint for handling HTTP GET requests at the specified path.
// This is a convenience function that calls NewEndpoint with http.MethodGet as the method.
//
// Parameters:
// - path: The URL path for this endpoint
// - endpointHandler: The handler function for this endpoint
// - mid: Optional middleware functions specific to this endpoint
//
// Returns:
// - A new endpoint instance configured for GET requests
func GET(path string, endpointHandler http.HandlerFunc, mid ...MiddleWare) endpoint {
return NewEndpoint(http.MethodGet, path, endpointHandler, mid...)
}
// POST creates a new endpoint for handling HTTP POST requests at the specified path.
// This is a convenience function that calls NewEndpoint with http.MethodPost as the method.
//
// Parameters:
// - path: The URL path for this endpoint
// - endpointHandler: The handler function for this endpoint
// - mid: Optional middleware functions specific to this endpoint
//
// Returns:
// - A new endpoint instance configured for POST requests
func POST(path string, endpointHandler http.HandlerFunc, mid ...MiddleWare) endpoint {
return NewEndpoint(http.MethodPost, path, endpointHandler, mid...)
}
// PUT creates a new endpoint for handling HTTP PUT requests at the specified path.
// This is a convenience function that calls NewEndpoint with http.MethodPut as the method.
//
// Parameters:
// - path: The URL path for this endpoint
// - endpointHandler: The handler function for this endpoint
// - mid: Optional middleware functions specific to this endpoint
//
// Returns:
// - A new endpoint instance configured for PUT requests
func PUT(path string, endpointHandler http.HandlerFunc, mid ...MiddleWare) endpoint {
return NewEndpoint(http.MethodPut, path, endpointHandler, mid...)
}
// DELETE creates a new endpoint for handling HTTP DELETE requests at the specified path.
// This is a convenience function that calls NewEndpoint with http.MethodDelete as the method.
//
// Parameters:
// - path: The URL path for this endpoint
// - endpointHandler: The handler function for this endpoint
// - mid: Optional middleware functions specific to this endpoint
//
// Returns:
// - A new endpoint instance configured for DELETE requests
func DELETE(path string, endpointHandler http.HandlerFunc, mid ...MiddleWare) endpoint {
return NewEndpoint(http.MethodDelete, path, endpointHandler, mid...)
}
// PATCH creates a new endpoint for handling HTTP PATCH requests at the specified path.
// This is a convenience function that calls NewEndpoint with http.MethodPatch as the method.
//
// Parameters:
// - path: The URL path for this endpoint
// - endpointHandler: The handler function for this endpoint
// - mid: Optional middleware functions specific to this endpoint
//
// Returns:
// - A new endpoint instance configured for PATCH requests
func PATCH(path string, endpointHandler http.HandlerFunc, mid ...MiddleWare) endpoint {
return NewEndpoint(http.MethodPatch, path, endpointHandler, mid...)
}
// HEAD creates a new endpoint for handling HTTP HEAD requests at the specified path.
// This is a convenience function that calls NewEndpoint with http.MethodHead as the method.
//
// Parameters:
// - path: The URL path for this endpoint
// - endpointHandler: The handler function for this endpoint
// - mid: Optional middleware functions specific to this endpoint
//
// Returns:
// - A new endpoint instance configured for HEAD requests
func HEAD(path string, endpointHandler http.HandlerFunc, mid ...MiddleWare) endpoint {
return NewEndpoint(http.MethodHead, path, endpointHandler, mid...)
}