mirror of
https://github.com/versia-pub/versia-go.git
synced 2026-03-13 20:49:15 +01:00
refactor: move config into internal package
This commit is contained in:
parent
3dd35d7182
commit
d691d26e02
11 changed files with 10 additions and 10 deletions
169
internal/config/config.go
Normal file
169
internal/config/config.go
Normal file
|
|
@ -0,0 +1,169 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"os"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.devminer.xyz/devminer/unitel"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type Mode string
|
||||
|
||||
const (
|
||||
ModeCombined Mode = "combined"
|
||||
ModeWeb Mode = "web"
|
||||
ModeConsumer Mode = "consumer"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Port int
|
||||
TLSKey *string
|
||||
TLSCert *string
|
||||
|
||||
PublicAddress *url.URL
|
||||
Host string
|
||||
SharedInboxURL *url.URL
|
||||
|
||||
InstanceName string
|
||||
InstanceDescription *string
|
||||
|
||||
NATSURI string
|
||||
NATSStreamName string
|
||||
|
||||
Mode Mode
|
||||
Consumers []string
|
||||
|
||||
DatabaseURI string
|
||||
|
||||
Telemetry unitel.Opts
|
||||
ForwardTracesTo *regexp.Regexp
|
||||
}
|
||||
|
||||
var C Config
|
||||
|
||||
func Load() {
|
||||
if err := godotenv.Load(".env.local", ".env"); err != nil {
|
||||
log.Warn().Err(err).Msg("Failed to load .env file")
|
||||
}
|
||||
|
||||
publicAddress, err := url.Parse(os.Getenv("VERSIA_INSTANCE_ADDRESS"))
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Failed to parse VERSIA_INSTANCE_ADDRESS")
|
||||
}
|
||||
|
||||
var forwardTracesTo *regexp.Regexp
|
||||
{
|
||||
rawForwardTracesTo := optionalEnvStr("FORWARD_TRACES_TO")
|
||||
if rawForwardTracesTo == nil {
|
||||
s := "matchnothing^"
|
||||
rawForwardTracesTo = &s
|
||||
}
|
||||
if forwardTracesTo, err = regexp.Compile(*rawForwardTracesTo); err != nil {
|
||||
log.Fatal().Err(err).Str("raw", *rawForwardTracesTo).Msg("Failed to compile")
|
||||
}
|
||||
}
|
||||
|
||||
tlsKey := optionalEnvStr("VERSIA_TLS_KEY")
|
||||
tlsCert := optionalEnvStr("VERSIA_TLS_CERT")
|
||||
if (tlsKey != nil && tlsCert == nil) || (tlsKey == nil && tlsCert != nil) {
|
||||
log.Fatal().
|
||||
Msg("Both VERSIA_TLS_KEY and VERSIA_TLS_CERT have to be set if you want to use in-process TLS termination.")
|
||||
}
|
||||
|
||||
mode := getEnvStrOneOf("VERSIA_MODE", ModeCombined, ModeCombined, ModeWeb, ModeConsumer)
|
||||
|
||||
var consumers []string
|
||||
if raw := optionalEnvStr("VERSIA_TQ_CUSTOMERS"); raw != nil {
|
||||
consumers = strings.Split(*raw, ",")
|
||||
}
|
||||
|
||||
C = Config{
|
||||
Port: getEnvInt("VERSIA_PORT", 80),
|
||||
TLSCert: tlsCert,
|
||||
TLSKey: tlsKey,
|
||||
|
||||
PublicAddress: publicAddress,
|
||||
Host: publicAddress.Host,
|
||||
SharedInboxURL: publicAddress.ResolveReference(&url.URL{Path: "/api/inbox"}),
|
||||
|
||||
InstanceName: os.Getenv("VERSIA_INSTANCE_NAME"),
|
||||
InstanceDescription: optionalEnvStr("VERSIA_INSTANCE_DESCRIPTION"),
|
||||
|
||||
NATSURI: os.Getenv("NATS_URI"),
|
||||
NATSStreamName: getEnvStr("NATS_STREAM_NAME", "versia-go"),
|
||||
|
||||
Mode: mode,
|
||||
Consumers: consumers,
|
||||
|
||||
DatabaseURI: os.Getenv("DATABASE_URI"),
|
||||
|
||||
ForwardTracesTo: forwardTracesTo,
|
||||
Telemetry: unitel.ParseOpts("versia-go"),
|
||||
}
|
||||
}
|
||||
|
||||
func optionalEnvStr(key string) *string {
|
||||
value := os.Getenv(key)
|
||||
if value == "" {
|
||||
return nil
|
||||
}
|
||||
return &value
|
||||
}
|
||||
|
||||
func getEnvBool(key string, default_ bool) bool {
|
||||
if value, ok := os.LookupEnv(key); ok {
|
||||
b, err := strconv.ParseBool(value)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
return default_
|
||||
}
|
||||
|
||||
func getEnvStr(key, default_ string) string {
|
||||
if value, ok := os.LookupEnv(key); ok {
|
||||
return value
|
||||
}
|
||||
|
||||
return default_
|
||||
}
|
||||
|
||||
func getEnvInt(key string, default_ int) int {
|
||||
if value, ok := os.LookupEnv(key); ok {
|
||||
parsed, err := strconv.Atoi(value)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return parsed
|
||||
}
|
||||
|
||||
return default_
|
||||
}
|
||||
|
||||
func getEnvStrOneOf[T ~string](key string, default_ T, enum ...T) T {
|
||||
if value, ok := os.LookupEnv(key); ok {
|
||||
if !slices.Contains(enum, T(value)) {
|
||||
sb := strings.Builder{}
|
||||
sb.WriteString(key)
|
||||
sb.WriteString(" can only be one of ")
|
||||
for _, v := range enum {
|
||||
sb.WriteString(string(v))
|
||||
}
|
||||
|
||||
panic(sb.String())
|
||||
}
|
||||
|
||||
return T(value)
|
||||
}
|
||||
|
||||
return default_
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@ package follow_handler
|
|||
import (
|
||||
"github.com/go-logr/logr"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/versia-pub/versia-go/config"
|
||||
"github.com/versia-pub/versia-go/internal/config"
|
||||
"github.com/versia-pub/versia-go/internal/service"
|
||||
"github.com/versia-pub/versia-go/pkg/webfinger"
|
||||
)
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ package meta_handler
|
|||
import (
|
||||
"github.com/go-logr/logr"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/versia-pub/versia-go/config"
|
||||
"github.com/versia-pub/versia-go/internal/config"
|
||||
"github.com/versia-pub/versia-go/internal/service"
|
||||
"github.com/versia-pub/versia-go/pkg/webfinger"
|
||||
)
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ package note_handler
|
|||
import (
|
||||
"github.com/go-logr/logr"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/versia-pub/versia-go/config"
|
||||
"github.com/versia-pub/versia-go/internal/config"
|
||||
"github.com/versia-pub/versia-go/internal/service"
|
||||
"github.com/versia-pub/versia-go/internal/validators"
|
||||
"github.com/versia-pub/versia-go/pkg/webfinger"
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@ package user_handler
|
|||
import (
|
||||
"errors"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/versia-pub/versia-go/config"
|
||||
"github.com/versia-pub/versia-go/internal/api_schema"
|
||||
"github.com/versia-pub/versia-go/internal/config"
|
||||
"github.com/versia-pub/versia-go/internal/helpers"
|
||||
"github.com/versia-pub/versia-go/pkg/webfinger"
|
||||
)
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import (
|
|||
"context"
|
||||
"crypto/ed25519"
|
||||
"errors"
|
||||
"github.com/versia-pub/versia-go/config"
|
||||
"github.com/versia-pub/versia-go/internal/config"
|
||||
"github.com/versia-pub/versia-go/internal/repository"
|
||||
"github.com/versia-pub/versia-go/internal/service"
|
||||
"github.com/versia-pub/versia-go/pkg/versia"
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@ package svc_impls
|
|||
|
||||
import (
|
||||
"context"
|
||||
"github.com/versia-pub/versia-go/config"
|
||||
"github.com/versia-pub/versia-go/ent"
|
||||
"github.com/versia-pub/versia-go/internal/config"
|
||||
"github.com/versia-pub/versia-go/internal/repository"
|
||||
"github.com/versia-pub/versia-go/internal/service"
|
||||
|
||||
|
|
|
|||
|
|
@ -12,8 +12,8 @@ import (
|
|||
"git.devminer.xyz/devminer/unitel"
|
||||
"github.com/go-logr/logr"
|
||||
"github.com/google/uuid"
|
||||
"github.com/versia-pub/versia-go/config"
|
||||
"github.com/versia-pub/versia-go/ent/schema"
|
||||
"github.com/versia-pub/versia-go/internal/config"
|
||||
"github.com/versia-pub/versia-go/internal/entity"
|
||||
"github.com/versia-pub/versia-go/internal/utils"
|
||||
"github.com/versia-pub/versia-go/pkg/webfinger"
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ package utils
|
|||
import (
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"github.com/versia-pub/versia-go/config"
|
||||
"github.com/versia-pub/versia-go/internal/config"
|
||||
versiautils "github.com/versia-pub/versia-go/pkg/versia/utils"
|
||||
"net/url"
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue