2021-09-11 11:35:15 +00:00
|
|
|
/*
|
2024-05-11 20:42:21 +00:00
|
|
|
Copyright CAcert Inc.
|
2023-05-13 11:27:19 +00:00
|
|
|
SPDX-License-Identifier: Apache-2.0
|
2021-09-11 11:35:15 +00:00
|
|
|
|
2023-05-13 11:27:19 +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
|
2021-09-11 11:35:15 +00:00
|
|
|
|
2023-07-18 18:37:04 +00:00
|
|
|
https://www.apache.org/licenses/LICENSE-2.0
|
2021-09-11 11:35:15 +00:00
|
|
|
|
2023-05-13 11:27:19 +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-11 11:35:15 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-05-13 11:27:19 +00:00
|
|
|
"fmt"
|
2024-05-11 23:07:34 +00:00
|
|
|
"log/slog"
|
2021-09-11 11:35:15 +00:00
|
|
|
"net/http"
|
|
|
|
"sync/atomic"
|
|
|
|
)
|
|
|
|
|
|
|
|
type key int
|
|
|
|
|
|
|
|
const (
|
2023-05-13 11:27:19 +00:00
|
|
|
requestIDKey key = iota
|
2021-09-11 11:35:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2023-05-13 11:27:19 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return count, fmt.Errorf("could not write response: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return count, nil
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
|
2024-05-11 23:07:34 +00:00
|
|
|
func Logging(logger *slog.Logger) func(http.Handler) http.Handler {
|
2021-09-11 11:35:15 +00:00
|
|
|
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() {
|
2023-05-13 11:27:19 +00:00
|
|
|
requestID, ok := r.Context().Value(requestIDKey).(string)
|
2021-09-11 11:35:15 +00:00
|
|
|
if !ok {
|
2023-05-13 11:27:19 +00:00
|
|
|
requestID = "unknown"
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
2024-05-11 20:42:21 +00:00
|
|
|
|
2024-05-11 23:07:34 +00:00
|
|
|
logger.Info(
|
|
|
|
fmt.Sprintf(
|
|
|
|
"[%s] %s \"%s %s\" %d %d \"%s\"",
|
|
|
|
requestID,
|
|
|
|
r.RemoteAddr,
|
|
|
|
r.Method,
|
|
|
|
r.URL.Path,
|
|
|
|
interceptor.code,
|
|
|
|
interceptor.count,
|
|
|
|
r.UserAgent(),
|
|
|
|
),
|
2021-09-11 11:35:15 +00:00
|
|
|
)
|
|
|
|
}()
|
|
|
|
next.ServeHTTP(interceptor, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
func Tracing(nextRequestID func() string) func(http.Handler) http.Handler {
|
2021-09-11 11:35:15 +00:00
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2023-05-13 11:27:19 +00:00
|
|
|
requestID := r.Header.Get("X-Request-Id")
|
|
|
|
if requestID == "" {
|
|
|
|
requestID = nextRequestID()
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
2024-05-11 20:42:21 +00:00
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
ctx := context.WithValue(r.Context(), requestIDKey, requestID)
|
2024-05-11 20:42:21 +00:00
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
w.Header().Set("X-Request-Id", requestID)
|
2021-09-11 11:35:15 +00:00
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var Healthy int32
|
|
|
|
|
|
|
|
func NewHealthHandler() http.Handler {
|
2024-05-11 20:42:21 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
2021-09-11 11:35:15 +00:00
|
|
|
if atomic.LoadInt32(&Healthy) == 1 {
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
return
|
|
|
|
}
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
|
|
|
})
|
|
|
|
}
|