2018-02-20 00:24:10 +02:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02: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 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02: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 20:48:01 +02:00
|
|
|
package api
|
2017-10-05 23:17:27 +02:00
|
|
|
|
|
|
|
import (
|
2021-10-19 01:25:20 +02:00
|
|
|
"net/http"
|
|
|
|
|
2021-10-12 09:25:13 +02:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-10-19 01:25:20 +02:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
2021-10-12 09:25:13 +02:00
|
|
|
|
2023-12-08 09:15:08 +02:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/version"
|
2017-10-05 23:17:27 +02:00
|
|
|
)
|
|
|
|
|
2023-06-03 21:38:36 +02:00
|
|
|
// Health
|
|
|
|
//
|
|
|
|
// @Summary Health information
|
2023-09-02 13:31:10 +02:00
|
|
|
// @Description If everything is fine, just a 204 will be returned, a 500 signals server state is unhealthy.
|
2023-06-03 21:38:36 +02:00
|
|
|
// @Router /healthz [get]
|
|
|
|
// @Produce plain
|
2023-09-02 13:31:10 +02:00
|
|
|
// @Success 204
|
2023-06-03 21:38:36 +02:00
|
|
|
// @Failure 500
|
|
|
|
// @Tags System
|
2017-10-05 23:17:27 +02:00
|
|
|
func Health(c *gin.Context) {
|
|
|
|
if err := store.FromContext(c).Ping(); err != nil {
|
2023-03-19 14:52:58 +02:00
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
2017-10-05 23:17:27 +02:00
|
|
|
return
|
|
|
|
}
|
2023-09-02 13:31:10 +02:00
|
|
|
c.Status(http.StatusNoContent)
|
2017-10-05 23:17:27 +02:00
|
|
|
}
|
|
|
|
|
2023-06-03 21:38:36 +02: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 23:17:27 +02:00
|
|
|
func Version(c *gin.Context) {
|
2023-03-19 14:52:58 +02:00
|
|
|
c.JSON(http.StatusOK, gin.H{
|
2021-05-25 14:08:27 +02:00
|
|
|
"source": "https://github.com/woodpecker-ci/woodpecker",
|
2019-11-12 22:08:17 +02:00
|
|
|
"version": version.String(),
|
2017-10-05 23:17:27 +02:00
|
|
|
})
|
|
|
|
}
|
2021-10-19 01:25:20 +02:00
|
|
|
|
2023-06-03 21:38:36 +02:00
|
|
|
// LogLevel
|
|
|
|
//
|
|
|
|
// @Summary Current log level
|
|
|
|
// @Description Endpoint returns the current logging level. Requires admin rights.
|
|
|
|
// @Router /log-level [get]
|
|
|
|
// @Produce json
|
2024-07-13 15:55:13 +02:00
|
|
|
// @Success 200 {object} object{log-level=string}
|
2023-06-03 21:38:36 +02:00
|
|
|
// @Tags System
|
2021-10-19 01:25:20 +02:00
|
|
|
func LogLevel(c *gin.Context) {
|
2023-03-19 14:52:58 +02:00
|
|
|
c.JSON(http.StatusOK, gin.H{
|
2021-10-19 01:25:20 +02:00
|
|
|
"log-level": zerolog.GlobalLevel().String(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-06-03 21:38:36 +02:00
|
|
|
// SetLogLevel
|
|
|
|
//
|
|
|
|
// @Summary Set log level
|
|
|
|
// @Description Endpoint sets the current logging level. Requires admin rights.
|
|
|
|
// @Router /log-level [post]
|
|
|
|
// @Produce json
|
2024-07-13 15:55:13 +02:00
|
|
|
// @Success 200 {object} object{log-level=string}
|
2023-06-03 21:38:36 +02:00
|
|
|
// @Tags System
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2024-07-13 15:55:13 +02:00
|
|
|
// @Param log-level body object{log-level=string} true "the new log level, one of <debug,trace,info,warn,error,fatal,panic,disabled>"
|
2021-10-19 01:25:20 +02:00
|
|
|
func SetLogLevel(c *gin.Context) {
|
|
|
|
logLevel := struct {
|
|
|
|
LogLevel string `json:"log-level"`
|
|
|
|
}{}
|
|
|
|
if err := c.Bind(&logLevel); err != nil {
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(http.StatusBadRequest, err)
|
2021-10-19 01:25:20 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
lvl, err := zerolog.ParseLevel(logLevel.LogLevel)
|
|
|
|
if err != nil {
|
2021-11-23 16:36:52 +02:00
|
|
|
_ = c.AbortWithError(http.StatusBadRequest, err)
|
2021-10-19 01:25:20 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Log().Msgf("log level set to %s", lvl.String())
|
|
|
|
zerolog.SetGlobalLevel(lvl)
|
2023-03-19 14:52:58 +02:00
|
|
|
c.JSON(http.StatusOK, logLevel)
|
2021-10-19 01:25:20 +02:00
|
|
|
}
|