Naming, layout and comments: descriptive full-word names, one item per line in wrapped calls and literals, and doc comments on exported names

Short and single-letter names in application code become full words (tests
keep theirs). Wrapped calls, signatures and composite literals put one item
per line, and one-line function bodies are expanded. Exported names and
groups of constants get doc comments, and comments refer to the renamed
identifiers.
This commit is contained in:
bwilliams committed 2026-10-15 16:12:56 +01:00
1 parent 043e307663
commit 465358609a
78 files changed
+2374 -1402

No files matched your search

+26 -23
View File
@@ -26,32 +26,32 @@ func (r *statusRecorder) WriteHeader(code int) {
r.ResponseWriter.WriteHeader(code)
}
func (r *statusRecorder) Write(b []byte) (int, error) {
func (r *statusRecorder) Write(data []byte) (int, error) {
if r.status == 0 {
r.status = http.StatusOK
}
n, err := r.ResponseWriter.Write(b)
r.bytes += n
return n, err
written, err := r.ResponseWriter.Write(data)
r.bytes += written
return written, err
}
// logField escapes a value for a quoted log field the way nginx does
// (quotes, backslashes and control characters become \xHH) and uses "-"
// for empty values.
func logField(s string) string {
if s == "" {
func logField(value string) string {
if value == "" {
return "-"
}
var b strings.Builder
for i := 0; i < len(s); i++ {
c := s[i]
if c == '"' || c == '\\' || c < 0x20 || c == 0x7f {
fmt.Fprintf(&b, "\\x%02X", c)
var builder strings.Builder
for i := 0; i < len(value); i++ {
char := value[i]
if char == '"' || char == '\\' || char < 0x20 || char == 0x7f {
fmt.Fprintf(&builder, "\\x%02X", char)
} else {
b.WriteByte(c)
builder.WriteByte(char)
}
}
return b.String()
return builder.String()
}
// clientKey attempts to produce a client host name as a key identifier to split
@@ -71,26 +71,29 @@ func clientKey(req *http.Request) string {
func AccessLog(logger *log.Logger, combined bool, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
start := time.Now()
rec := &statusRecorder{ResponseWriter: w}
next.ServeHTTP(rec, req)
if rec.status == 0 {
rec.status = http.StatusOK
recorder := &statusRecorder{
ResponseWriter: w,
}
next.ServeHTTP(recorder, req)
if recorder.status == 0 {
recorder.status = http.StatusOK
}
user := "-"
if u, _, ok := req.BasicAuth(); ok && u != "" {
user = logField(u)
if username, _, ok := req.BasicAuth(); ok && username != "" {
user = logField(username)
}
size := "-" // CLF uses "-" for an empty body
if rec.bytes > 0 {
size = strconv.Itoa(rec.bytes)
if recorder.bytes > 0 {
size = strconv.Itoa(recorder.bytes)
}
line := fmt.Sprintf(`%s - %s [%s] "%s" %d %s`,
line := fmt.Sprintf(
`%s - %s [%s] "%s" %d %s`,
clientKey(req),
user,
start.Format("02/Jan/2006:15:04:05 -0700"),
logField(req.Method+" "+req.RequestURI+" "+req.Proto),
rec.status,
recorder.status,
size,
)
if combined {