expose fewer internals in packages, for easier software reuse

- prometheus is now behind an interface, they aren't dependencies for the
  reusable components anymore.
- some dependencies have been inverted: instead of packages importing a main
  package to get configuration, the main package now sets configuration in
  these packages. that means fewer internals are pulled in.
- some functions now have new parameters for values that were retrieved from
  package "mox-".
This commit is contained in:
Mechiel Lukkien
2023-12-05 21:13:57 +01:00
parent fcaa504878
commit 72ac1fde29
51 changed files with 696 additions and 568 deletions

View File

@ -1,27 +0,0 @@
package moxio
import (
"io"
"strings"
"golang.org/x/text/encoding/ianaindex"
)
// DecodeReader returns a reader that reads from r, decoding as charset. If
// charset is empty, us-ascii, utf-8 or unknown, the original reader is
// returned and no decoding takes place.
func DecodeReader(charset string, r io.Reader) io.Reader {
switch strings.ToLower(charset) {
case "", "us-ascii", "utf-8":
return r
}
enc, _ := ianaindex.MIME.Encoding(charset)
if enc == nil {
enc, _ = ianaindex.IANA.Encoding(charset)
}
// todo: ianaindex doesn't know all encodings, e.g. gb2312. should we transform them, with which code?
if enc == nil {
return r
}
return enc.NewDecoder().Reader(r)
}

View File

@ -1,24 +0,0 @@
package moxio
import (
"io"
"strings"
"testing"
)
func TestDecodeReader(t *testing.T) {
check := func(charset, input, output string) {
t.Helper()
buf, err := io.ReadAll(DecodeReader(charset, strings.NewReader(input)))
tcheckf(t, err, "decode")
if string(buf) != output {
t.Fatalf("decoding %q with charset %q, got %q, expected %q", input, charset, buf, output)
}
}
check("", "☺", "☺") // No decoding.
check("us-ascii", "☺", "☺") // No decoding.
check("utf-8", "☺", "☺")
check("iso-8859-1", string([]byte{0xa9}), "©")
check("iso-8859-5", string([]byte{0xd0}), "а")
}

29
moxio/tlsinfo.go Normal file
View File

@ -0,0 +1,29 @@
package moxio
import (
"crypto/tls"
"fmt"
)
// TLSInfo returns human-readable strings about the TLS connection, for use in
// logging.
func TLSInfo(conn *tls.Conn) (version, ciphersuite string) {
st := conn.ConnectionState()
versions := map[uint16]string{
tls.VersionTLS10: "TLS1.0",
tls.VersionTLS11: "TLS1.1",
tls.VersionTLS12: "TLS1.2",
tls.VersionTLS13: "TLS1.3",
}
v, ok := versions[st.Version]
if ok {
version = v
} else {
version = fmt.Sprintf("TLS %x", st.Version)
}
ciphersuite = tls.CipherSuiteName(st.CipherSuite)
return
}