2023-08-04 13:34:18 +02:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"time"
|
2025-02-28 08:29:40 +01:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
"github.com/h44z/wg-portal/internal/app/api/v0/model"
|
2023-08-04 13:34:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type testEndpoint struct{}
|
|
|
|
|
|
|
|
func (e testEndpoint) GetName() string {
|
|
|
|
return "TestEndpoint"
|
|
|
|
}
|
|
|
|
|
2025-02-28 16:11:55 +01:00
|
|
|
func (e testEndpoint) RegisterRoutes(g *gin.RouterGroup, _ *authenticationHandler) {
|
2023-08-04 13:34:18 +02:00
|
|
|
g.GET("/now", e.handleCurrentTimeGet())
|
|
|
|
g.GET("/hostname", e.handleHostnameGet())
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleCurrentTimeGet represents the GET endpoint that responds the current time
|
|
|
|
//
|
|
|
|
// @ID test_handleCurrentTimeGet
|
|
|
|
// @Tags Testing
|
|
|
|
// @Summary Get the current local time.
|
|
|
|
// @Description Nothing more to describe...
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} string
|
|
|
|
// @Failure 500 {object} model.Error
|
|
|
|
// @Router /now [get]
|
|
|
|
func (e testEndpoint) handleCurrentTimeGet() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
if time.Now().Second() == 0 {
|
|
|
|
c.JSON(http.StatusInternalServerError, model.Error{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
Message: "invalid time",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, time.Now().String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleHostnameGet represents the GET endpoint that responds the current hostname
|
|
|
|
//
|
|
|
|
// @ID test_handleHostnameGet
|
|
|
|
// @Tags Testing
|
|
|
|
// @Summary Get the current host name.
|
|
|
|
// @Description Nothing more to describe...
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} string
|
|
|
|
// @Failure 500 {object} model.Error
|
|
|
|
// @Router /hostname [get]
|
|
|
|
func (e testEndpoint) handleHostnameGet() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, model.Error{
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, hostname)
|
|
|
|
}
|
|
|
|
}
|