@@ -48,10 +48,15 @@ func NewHandler(calc Calculator, validator Validator, renderer Renderer) *Handle
48
48
49
49
// Home handles GET requests to the root path, rendering the home page.
50
50
func (h * Handler ) Home (ctx RequestContext ) {
51
- if err := h .renderer .RenderHome (ctx .GetContext ()); err != nil {
51
+ c := ctx .GetContext ()
52
+ if err := h .renderer .RenderHome (c ); err != nil {
52
53
slog .Error ("Failed to render home page" , "error" , err )
53
- ctx .GetContext ().AbortWithStatus (http .StatusInternalServerError )
54
+ c .AbortWithStatus (http .StatusInternalServerError )
55
+
56
+ return
54
57
}
58
+
59
+ c .Status (http .StatusOK )
55
60
}
56
61
57
62
// HomeAdapter adapts the Home method to a gin.HandlerFunc.
@@ -66,12 +71,13 @@ func (h *Handler) Calculate(ctx RequestContext) {
66
71
mac := ctx .FormValue ("mac" )
67
72
prefix := ctx .FormValue ("ip-start" )
68
73
interfaceID , fullIP , errorMsg := "" , "" , ""
74
+ c := ctx .GetContext ()
69
75
70
76
if err := h .validator .ValidateMAC (mac ); err != nil {
71
77
errorMsg = "Please enter a valid MAC address (e.g., 00-14-22-01-23-45)"
72
78
73
79
slog .Warn ("MAC validation failed" , "mac" , mac , "error" , err )
74
- h .renderResult (ctx , interfaceID , fullIP , errorMsg )
80
+ h .renderResult (c , interfaceID , fullIP , errorMsg )
75
81
76
82
return
77
83
}
@@ -80,7 +86,7 @@ func (h *Handler) Calculate(ctx RequestContext) {
80
86
errorMsg = "Please enter a valid IPv6 prefix (e.g., 2001:db8::)"
81
87
82
88
slog .Warn ("Prefix validation failed" , "prefix" , prefix , "error" , err )
83
- h .renderResult (ctx , interfaceID , fullIP , errorMsg )
89
+ h .renderResult (c , interfaceID , fullIP , errorMsg )
84
90
85
91
return
86
92
}
@@ -90,9 +96,12 @@ func (h *Handler) Calculate(ctx RequestContext) {
90
96
errorMsg = "Failed to calculate EUI-64 address"
91
97
92
98
slog .Error ("EUI-64 calculation failed" , "mac" , mac , "prefix" , prefix , "error" , err )
99
+ h .renderError (c , http .StatusInternalServerError , errorMsg )
100
+
101
+ return
93
102
}
94
103
95
- h .renderResult (ctx , interfaceID , fullIP , errorMsg )
104
+ h .renderResult (c , interfaceID , fullIP , errorMsg )
96
105
}
97
106
98
107
// CalculateAdapter adapts the Calculate method to a gin.HandlerFunc.
@@ -102,12 +111,21 @@ func (h *Handler) CalculateAdapter() gin.HandlerFunc {
102
111
}
103
112
}
104
113
105
- // renderResult renders the calculation result to the HTTP response.
106
- func (h * Handler ) renderResult (ctx RequestContext , interfaceID , fullIP , errorMsg string ) {
107
- if err := h .renderer .RenderResult (ctx .GetContext (), interfaceID , fullIP , errorMsg ); err != nil {
108
- slog .Error ("Failed to render result" , "error" , err )
109
- ctx .GetContext ().AbortWithStatus (http .StatusInternalServerError )
114
+ // renderResult renders a successful calculation result.
115
+ func (h * Handler ) renderResult (c * gin.Context , interfaceID , fullIP , errorMsg string ) {
116
+ if err := h .renderer .RenderResult (c , interfaceID , fullIP , errorMsg ); err != nil {
117
+ h .renderError (c , http .StatusInternalServerError , err .Error ())
118
+
119
+ return
110
120
}
121
+
122
+ c .Status (http .StatusOK )
123
+ }
124
+
125
+ // renderError renders an error response without committing headers prematurely.
126
+ func (h * Handler ) renderError (c * gin.Context , status int , errorMsg string ) {
127
+ slog .Error ("Rendering error response" , "status" , status , "message" , errorMsg )
128
+ c .AbortWithStatusJSON (status , gin.H {"error" : errorMsg })
111
129
}
112
130
113
131
// ginRequestContext adapts gin.Context to RequestContext.
0 commit comments