feat: some refactoring
This commit is contained in:
37
internal/apiserver/handlers/home.go
Normal file
37
internal/apiserver/handlers/home.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"git.nwaifu.su/sergey/MyGoServer/internal/apiserver/models"
|
||||
)
|
||||
|
||||
// HomeHandler handles the root endpoint
|
||||
type HomeHandler struct{}
|
||||
|
||||
func NewHomeHandler() *HomeHandler {
|
||||
return &HomeHandler{}
|
||||
}
|
||||
|
||||
// ServeHTTP implements http.Handler
|
||||
func (h *HomeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
h.handleHome(w, r)
|
||||
}
|
||||
|
||||
// @Summary Health check
|
||||
// @Description Проверка состояния сервера
|
||||
// @Tags Health
|
||||
// @Success 200 {object} models.Response "Server is running"
|
||||
// @Router / [get]
|
||||
func (h *HomeHandler) handleHome(w http.ResponseWriter, r *http.Request) {
|
||||
// Create a simple success response
|
||||
response := models.NewSuccessResponse(map[string]string{
|
||||
"status": "ok",
|
||||
"message": "API Server is running",
|
||||
})
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
41
internal/apiserver/handlers/home_test.go
Normal file
41
internal/apiserver/handlers/home_test.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHomeHandler_ServeHTTP(t *testing.T) {
|
||||
// Create a new home handler
|
||||
handler := NewHomeHandler()
|
||||
|
||||
// Create a test request
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
|
||||
// Create a test response recorder
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
// Call the handler
|
||||
handler.ServeHTTP(rr, req)
|
||||
|
||||
// Check status code
|
||||
if status := rr.Code; status != http.StatusOK {
|
||||
t.Errorf("handler returned wrong status code: got %v want %v",
|
||||
status, http.StatusOK)
|
||||
}
|
||||
|
||||
// Check content type
|
||||
if ct := rr.Header().Get("Content-Type"); ct != "application/json" {
|
||||
t.Errorf("handler returned wrong content type: got %v want %v",
|
||||
ct, "application/json")
|
||||
}
|
||||
|
||||
// Check response body contains expected data
|
||||
expectedBody := `"success":true`
|
||||
if body := rr.Body.String(); !strings.Contains(body, expectedBody) {
|
||||
t.Errorf("handler returned unexpected body: got %v want %v",
|
||||
body, expectedBody)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user