2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-09-22 18:48:01 +00:00
|
|
|
package api
|
2017-10-05 21:17:27 +00:00
|
|
|
|
|
|
|
import (
|
2021-10-18 23:25:20 +00:00
|
|
|
"net/http"
|
|
|
|
|
2021-10-12 07:25:13 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-10-18 23:25:20 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
2021-10-12 07:25:13 +00:00
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/version"
|
2017-10-05 21:17:27 +00:00
|
|
|
)
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// Health
|
|
|
|
//
|
|
|
|
// @Summary Health information
|
2023-09-02 11:31:10 +00:00
|
|
|
// @Description If everything is fine, just a 204 will be returned, a 500 signals server state is unhealthy.
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Router /healthz [get]
|
|
|
|
// @Produce plain
|
2023-09-02 11:31:10 +00:00
|
|
|
// @Success 204
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Failure 500
|
|
|
|
// @Tags System
|
2017-10-05 21:17:27 +00:00
|
|
|
func Health(c *gin.Context) {
|
|
|
|
if err := store.FromContext(c).Ping(); err != nil {
|
2023-03-19 12:52:58 +00:00
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
2017-10-05 21:17:27 +00:00
|
|
|
return
|
|
|
|
}
|
2023-09-02 11:31:10 +00:00
|
|
|
c.Status(http.StatusNoContent)
|
2017-10-05 21:17:27 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// Version
|
|
|
|
//
|
|
|
|
// @Summary Get version
|
|
|
|
// @Description Endpoint returns the server version and build information.
|
|
|
|
// @Router /version [get]
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} string{source=string,version=string}
|
|
|
|
// @Tags System
|
2017-10-05 21:17:27 +00:00
|
|
|
func Version(c *gin.Context) {
|
2023-03-19 12:52:58 +00:00
|
|
|
c.JSON(http.StatusOK, gin.H{
|
2021-05-25 12:08:27 +00:00
|
|
|
"source": "https://github.com/woodpecker-ci/woodpecker",
|
2019-11-12 20:08:17 +00:00
|
|
|
"version": version.String(),
|
2017-10-05 21:17:27 +00:00
|
|
|
})
|
|
|
|
}
|
2021-10-18 23:25:20 +00:00
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// LogLevel
|
|
|
|
//
|
|
|
|
// @Summary Current log level
|
|
|
|
// @Description Endpoint returns the current logging level. Requires admin rights.
|
|
|
|
// @Router /log-level [get]
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} string{log-level=string}
|
|
|
|
// @Tags System
|
2021-10-18 23:25:20 +00:00
|
|
|
func LogLevel(c *gin.Context) {
|
2023-03-19 12:52:58 +00:00
|
|
|
c.JSON(http.StatusOK, gin.H{
|
2021-10-18 23:25:20 +00:00
|
|
|
"log-level": zerolog.GlobalLevel().String(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// SetLogLevel
|
|
|
|
//
|
|
|
|
// @Summary Set log level
|
|
|
|
// @Description Endpoint sets the current logging level. Requires admin rights.
|
|
|
|
// @Router /log-level [post]
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} string{log-level=string}
|
|
|
|
// @Tags System
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
|
|
|
// @Param log-level body string{log-level=string} true "the new log level, one of <debug,trace,info,warn,error,fatal,panic,disabled>"
|
2021-10-18 23:25:20 +00:00
|
|
|
func SetLogLevel(c *gin.Context) {
|
|
|
|
logLevel := struct {
|
|
|
|
LogLevel string `json:"log-level"`
|
|
|
|
}{}
|
|
|
|
if err := c.Bind(&logLevel); err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusBadRequest, err)
|
2021-10-18 23:25:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
lvl, err := zerolog.ParseLevel(logLevel.LogLevel)
|
|
|
|
if err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
_ = c.AbortWithError(http.StatusBadRequest, err)
|
2021-10-18 23:25:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Log().Msgf("log level set to %s", lvl.String())
|
|
|
|
zerolog.SetGlobalLevel(lvl)
|
2023-03-19 12:52:58 +00:00
|
|
|
c.JSON(http.StatusOK, logLevel)
|
2021-10-18 23:25:20 +00:00
|
|
|
}
|