feat: ServerMux to gorilla/mux, time route
This commit is contained in:
43
internal/apiserver/handlers/time.go
Normal file
43
internal/apiserver/handlers/time.go
Normal file
@@ -0,0 +1,43 @@
|
||||
// It's just test file. I'll remove it later
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.nwaifu.su/sergey/MyGoServer/internal/apiserver/models"
|
||||
)
|
||||
|
||||
// TimeHandler handles the time endpoint
|
||||
type TimeHandler struct{}
|
||||
|
||||
func NewTimeHandler() *TimeHandler {
|
||||
return &TimeHandler{}
|
||||
}
|
||||
|
||||
// ServeHTTP implements http.Handler
|
||||
func (h *TimeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
h.handleTime(w, r)
|
||||
}
|
||||
|
||||
// @Summary Get server time
|
||||
// @Description Возвращает текущее серверное время в формате ISO 8601
|
||||
// @Tags Time
|
||||
// @Success 200 {object} models.Response "Текущее время сервера"
|
||||
// @Router /time [get]
|
||||
func (h *TimeHandler) handleTime(w http.ResponseWriter, r *http.Request) {
|
||||
// Get current time in UTC
|
||||
currentTime := time.Now().UTC()
|
||||
|
||||
// Create response with time data
|
||||
response := models.NewSuccessResponse(map[string]interface{}{
|
||||
"server_time": currentTime.Format(time.RFC3339),
|
||||
"unix_timestamp": currentTime.Unix(),
|
||||
"timezone": "UTC",
|
||||
})
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
Reference in New Issue
Block a user