You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
2.5 KiB
Go
110 lines
2.5 KiB
Go
3 years ago
|
/*
|
||
1 year ago
|
Copyright 2020-2023 CAcert Inc.
|
||
|
SPDX-License-Identifier: Apache-2.0
|
||
3 years ago
|
|
||
1 year ago
|
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
|
||
3 years ago
|
|
||
1 year ago
|
https://www.apache.org/licenses/LICENSE-2.0
|
||
3 years ago
|
|
||
1 year ago
|
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.
|
||
3 years ago
|
*/
|
||
|
|
||
|
package handlers
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
1 year ago
|
"fmt"
|
||
3 years ago
|
"net/http"
|
||
|
"sync/atomic"
|
||
|
|
||
|
log "github.com/sirupsen/logrus"
|
||
|
)
|
||
|
|
||
|
type key int
|
||
|
|
||
|
const (
|
||
1 year ago
|
keyRequestID key = iota
|
||
3 years ago
|
)
|
||
|
|
||
|
type statusCodeInterceptor struct {
|
||
|
http.ResponseWriter
|
||
|
code int
|
||
|
count int
|
||
|
}
|
||
|
|
||
|
func (sci *statusCodeInterceptor) WriteHeader(code int) {
|
||
|
sci.code = code
|
||
|
sci.ResponseWriter.WriteHeader(code)
|
||
|
}
|
||
|
|
||
|
func (sci *statusCodeInterceptor) Write(content []byte) (int, error) {
|
||
|
count, err := sci.ResponseWriter.Write(content)
|
||
|
sci.count += count
|
||
1 year ago
|
|
||
|
if err != nil {
|
||
|
return count, fmt.Errorf("%w", err)
|
||
|
}
|
||
|
|
||
|
return count, nil
|
||
3 years ago
|
}
|
||
|
|
||
|
func Logging(logger *log.Logger) func(http.Handler) http.Handler {
|
||
|
return func(next http.Handler) http.Handler {
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
interceptor := &statusCodeInterceptor{w, http.StatusOK, 0}
|
||
|
defer func() {
|
||
1 year ago
|
requestID, ok := r.Context().Value(keyRequestID).(string)
|
||
3 years ago
|
if !ok {
|
||
1 year ago
|
requestID = "unknown"
|
||
3 years ago
|
}
|
||
1 year ago
|
|
||
3 years ago
|
logger.Infof(
|
||
1 year ago
|
"[%s] %s \"%s %s\" %d %d \"%s\"",
|
||
|
requestID,
|
||
3 years ago
|
r.RemoteAddr,
|
||
|
r.Method,
|
||
|
r.URL.Path,
|
||
|
interceptor.code,
|
||
|
interceptor.count,
|
||
|
r.UserAgent(),
|
||
|
)
|
||
|
}()
|
||
|
next.ServeHTTP(interceptor, r)
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
func Tracing(nextRequestID func() string) func(http.Handler) http.Handler {
|
||
3 years ago
|
return func(next http.Handler) http.Handler {
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
1 year ago
|
requestID := r.Header.Get("X-Request-Id")
|
||
|
if requestID == "" {
|
||
|
requestID = nextRequestID()
|
||
3 years ago
|
}
|
||
1 year ago
|
|
||
|
w.Header().Set("X-Request-Id", requestID)
|
||
|
next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), keyRequestID, requestID)))
|
||
3 years ago
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
var Healthy int32
|
||
|
|
||
|
func NewHealthHandler() http.Handler {
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
if atomic.LoadInt32(&Healthy) == 1 {
|
||
|
w.WriteHeader(http.StatusNoContent)
|
||
1 year ago
|
|
||
3 years ago
|
return
|
||
|
}
|
||
1 year ago
|
|
||
3 years ago
|
w.WriteHeader(http.StatusServiceUnavailable)
|
||
|
})
|
||
|
}
|