Skip to content

Commit

Permalink
internal/config: separate config initialization into serverconfig
Browse files Browse the repository at this point in the history
This change creates a new package that does config initialization and
other GCP-specific operations that were previously done in package
config, so that config can have no cloud dependencies.

For golang/go#61399

Change-Id: I8d78294834e325b47d838892a1cef87003a4b90a
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/522516
Run-TryBot: Michael Matloob <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Robert Findley <[email protected]>
kokoro-CI: kokoro <[email protected]>
  • Loading branch information
matloob committed Aug 25, 2023
1 parent 3841ba3 commit f2193d5
Show file tree
Hide file tree
Showing 16 changed files with 436 additions and 427 deletions.
7 changes: 4 additions & 3 deletions cmd/frontend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"golang.org/x/pkgsite/cmd/internal/cmdconfig"
"golang.org/x/pkgsite/internal"
"golang.org/x/pkgsite/internal/config"
"golang.org/x/pkgsite/internal/config/serverconfig"
"golang.org/x/pkgsite/internal/dcensus"
"golang.org/x/pkgsite/internal/fetch"
"golang.org/x/pkgsite/internal/fetchdatasource"
Expand All @@ -35,7 +36,7 @@ import (
)

var (
queueName = config.GetEnv("GO_DISCOVERY_FRONTEND_TASK_QUEUE", "")
queueName = serverconfig.GetEnv("GO_DISCOVERY_FRONTEND_TASK_QUEUE", "")
workers = flag.Int("workers", 10, "number of concurrent requests to the fetch service, when running locally")
staticFlag = flag.String("static", "static", "path to folder containing static files served")
thirdPartyPath = flag.String("third_party", "third_party", "path to folder containing third-party libraries")
Expand All @@ -53,7 +54,7 @@ var (
func main() {
flag.Parse()
ctx := context.Background()
cfg, err := config.Init(ctx)
cfg, err := serverconfig.Init(ctx)
if err != nil {
log.Fatal(ctx, err)
}
Expand Down Expand Up @@ -169,7 +170,7 @@ func main() {
}
// We are not currently forwarding any ports on AppEngine, so serving debug
// information is broken.
if !cfg.OnAppEngine() {
if !serverconfig.OnAppEngine() {
dcensusServer, err := dcensus.NewServer()
if err != nil {
log.Fatal(ctx, err)
Expand Down
7 changes: 4 additions & 3 deletions cmd/internal/cmdconfig/cmdconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"golang.org/x/pkgsite/internal"
"golang.org/x/pkgsite/internal/config"
"golang.org/x/pkgsite/internal/config/dynconfig"
"golang.org/x/pkgsite/internal/config/serverconfig"
"golang.org/x/pkgsite/internal/database"
"golang.org/x/pkgsite/internal/derrors"
"golang.org/x/pkgsite/internal/log"
Expand All @@ -30,12 +31,12 @@ import (

// Logger configures a middleware.Logger.
func Logger(ctx context.Context, cfg *config.Config, logName string) middleware.Logger {
if cfg.OnGCP() {
if serverconfig.OnGCP() {
opts := []logging.LoggerOption{logging.CommonResource(&mrpb.MonitoredResource{
Type: cfg.MonitoredResource.Type,
Labels: cfg.MonitoredResource.Labels,
})}
if cfg.OnGKE() {
if serverconfig.OnGKE() {
opts = append(opts, logging.CommonLabels(map[string]string{
"k8s-pod/env": cfg.DeploymentEnvironment(),
"k8s-pod/app": cfg.Application(),
Expand All @@ -53,7 +54,7 @@ func Logger(ctx context.Context, cfg *config.Config, logName string) middleware.

// Reporter configures an Error Reporting client.
func Reporter(ctx context.Context, cfg *config.Config) derrors.Reporter {
if !cfg.OnGCP() || cfg.DisableErrorReporting {
if !serverconfig.OnGCP() || cfg.DisableErrorReporting {
return nil
}
reportingClient, err := errorreporting.NewClient(ctx, cfg.ProjectID, errorreporting.Config{
Expand Down
9 changes: 5 additions & 4 deletions cmd/worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
_ "github.com/jackc/pgx/v4/stdlib" // for pgx driver
"golang.org/x/pkgsite/cmd/internal/cmdconfig"
"golang.org/x/pkgsite/internal/config"
"golang.org/x/pkgsite/internal/config/serverconfig"
"golang.org/x/pkgsite/internal/dcensus"
"golang.org/x/pkgsite/internal/index"
"golang.org/x/pkgsite/internal/log"
Expand All @@ -31,8 +32,8 @@ import (
)

var (
timeout = config.GetEnvInt(context.Background(), "GO_DISCOVERY_WORKER_TIMEOUT_MINUTES", 10)
queueName = config.GetEnv("GO_DISCOVERY_WORKER_TASK_QUEUE", "")
timeout = serverconfig.GetEnvInt(context.Background(), "GO_DISCOVERY_WORKER_TIMEOUT_MINUTES", 10)
queueName = serverconfig.GetEnv("GO_DISCOVERY_WORKER_TASK_QUEUE", "")
workers = flag.Int("workers", 10, "number of concurrent requests to the fetch service, when running locally")
// flag used in call to safehtml/template.TrustedSourceFromFlag
_ = flag.String("static", "static", "path to folder containing static files served")
Expand All @@ -44,7 +45,7 @@ func main() {

ctx := context.Background()

cfg, err := config.Init(ctx)
cfg, err := serverconfig.Init(ctx)
if err != nil {
log.Fatal(ctx, err)
}
Expand Down Expand Up @@ -128,7 +129,7 @@ func main() {
}
// We are not currently forwarding any ports on AppEngine, so serving debug
// information is broken.
if !cfg.OnAppEngine() {
if !serverconfig.OnAppEngine() {
dcensusServer, err := dcensus.NewServer()
if err != nil {
log.Fatal(ctx, err)
Expand Down
6 changes: 3 additions & 3 deletions devtools/cmd/db/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"strings"

_ "github.com/jackc/pgx/v4/stdlib" // for pgx driver
"golang.org/x/pkgsite/internal/config"
"golang.org/x/pkgsite/internal/config/serverconfig"
"golang.org/x/pkgsite/internal/database"
"golang.org/x/pkgsite/internal/log"
)
Expand All @@ -39,12 +39,12 @@ func main() {
}

ctx := context.Background()
cfg, err := config.Init(ctx)
cfg, err := serverconfig.Init(ctx)
if err != nil {
log.Fatal(ctx, err)
}

dbName := config.GetEnv("GO_DISCOVERY_DATABASE_NAME", "discovery-db")
dbName := serverconfig.GetEnv("GO_DISCOVERY_DATABASE_NAME", "discovery-db")
if err := run(ctx, flag.Args()[0], dbName, cfg.DBConnInfo()); err != nil {
log.Fatal(ctx, err)
}
Expand Down
3 changes: 2 additions & 1 deletion devtools/cmd/seeddb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"golang.org/x/pkgsite/internal"
"golang.org/x/pkgsite/internal/config"
"golang.org/x/pkgsite/internal/config/dynconfig"
"golang.org/x/pkgsite/internal/config/serverconfig"
"golang.org/x/pkgsite/internal/database"
"golang.org/x/pkgsite/internal/derrors"
"golang.org/x/pkgsite/internal/experiment"
Expand All @@ -45,7 +46,7 @@ func main() {
flag.Parse()

ctx := context.Background()
cfg, err := config.Init(ctx)
cfg, err := serverconfig.Init(ctx)
if err != nil {
log.Fatal(ctx, err)
}
Expand Down
Loading

0 comments on commit f2193d5

Please sign in to comment.