~bigbes/core-go

ref: 398f1a7022cd03486502bc2eca9da0629af82d54 core-go/config/config.go -rw-r--r-- 4.2 KiB
398f1a70 — Conrad Hoffmann 10 months ago
Refactor config loading and server initialization

As is, LoadConfig() does some things that are not strictly related to
the configuration, such as parsing command line arguments. This has led
to a proliferation of different ways to load the config based on various
needs and also prevents tools that need a config but are not services to
use custom command line arguments.

This commit aims to decouple config loading from everything else and
do nothing but loading the config files.

On a high level, this commit:
- renames server.NewServer() to server.New()
- moves config.Debug and config.Addr into the server package
- moves crypto.InitCrypto() call into server.New()
- moves command line parsing into server.New(), using passed-in values
  rather than os.Args

The only changes required for services would be changing

   cfg := config.LoadConfig(":5100")
   server := server.NewServer("meta.sr.ht", cfg)

to

   cfg := config.LoadConfig()
   server := server.New("meta.sr.ht", ":5100", cfg, os.Args)

All other tools will be switched to just LoadConfig() and, optionally, a
call to crypto.InitCrypto(). I managed to completely remove some global
state (addr) and at least make the rest private, so that users are
forced to use the designated functions.

The config module gained support for custom FS implementation, mainly
for testing.
304e09bb — Drew DeVault 10 months ago
config: don't log config path

This is kind of useful but it's also pretty noisy in a lot of places.
3e1d5d3a — Drew DeVault 11 months ago
config: add GetAPI
c1d572b5 — Conrad Hoffmann 1 year, 3 months ago
config: add GetBool convenience method

It's the boolean equivalent of `GetInt()`. The value handling is taken
from the Python version (`srht.config.cfgb()`).
55ab6c4b — Simon Martin 1 year, 4 months ago
config: add missing private ip ranges

I noticed that 172.16.0.0/12 is missing from the default private IP
address space; following a quick discussion in [1], this patches adds
it, as well as link local ranges.

[1] https://lists.sr.ht/~sircmpwn/sr.ht-dev/%3C20241218140750.2393901-1-robin@jarry.cc%3E

Fixes: deb699c9d01a ("add global internal-ipnet setting")
Signed-off-by: Simon Martin <simon@nasilyan.com>
deb699c9 — Robin Jarry 1 year, 7 months ago
config: add global internal-ipnet setting

Add support for a general [sr.ht]internal-ipnet list of networks that
can be considered OK for internal operations.

The default is loopback addresses (127.0.0.0/8 and ::1/128) and private
routable unicast addresses (192.168.0.0/16, 10.0.0.0/8 and fc00::/7)

Since parsing IP networks and addresses can be costly, store the result
of [sr.ht]internal-ipnet into a global list of net.IPNet and
a convenience IsInternalIP() function to be called by services.

Signed-off-by: Robin Jarry <robin@jarry.cc>
17315205 — Robin Jarry 1 year, 8 months ago
config: factorize integer parsing

Add a new GetInt() function to parse an integer value from a parsed
ini.File. Use that function instead of duplicated code.

Signed-off-by: Robin Jarry <robin@jarry.cc>
b57564ae — Conrad Hoffmann 1 year, 8 months ago
config: decouple file loading from crypto init

Now that the algorithm for loading the config is non-trivial, the code
for it should be re-used everywhere. However, it is currently coupled
with a call to `InitCrypto()`, which requires certain things like
webhook keys to be configured. This is not suitable for many components
that still need the configuration.

This commit introduces `config.LoadFiles()` to do only the file loading.
`LoadConfig()` uses it, and so can any components that do not care about
crypto initialization.
296f02e4 — Drew DeVault 2 years ago
config: print loaded config(s) to log
3c1346e6 — Conrad Hoffmann 2 years ago
config: allow spreading config over multiple files

This is the Go-equivalent to
https://lists.sr.ht/~sircmpwn/sr.ht-dev/patches/47657

This commit enables spreading the config in /etc/sr.ht - and, crucially,
_only_ in /etc/sr.ht - over multiple .ini files.

If a file config.ini is found (either in current or parent directory, or
/etc/sr.ht) it (and only it) is loaded and any other ini files are
ignored. To utilize multiple configs, they must be in /etc/sr.ht, and
none of them must be called config.ini.

Spreading the config over multiple files will make it much easier to
create containerized versions, where e.g. different secrets can be made
available in different files, but rendering it all into one big file
would require some preprocessing.
f44afb10 — Simon Ser 4 years ago
go fmt
f9731e15 — Drew DeVault 5 years ago
Various bug fixes per pages.sr.ht
0d5262df — Drew DeVault 5 years ago
config: add GetOrigin function

This implements the same behavior as Python's srht.config.get_origin.
789f4a81 — Drew DeVault 5 years ago
Simplify server module