| 1 | // SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | |
| 3 | package web |
| 4 | |
| 5 | import ( |
| 6 | "io/fs" |
| 7 | "log/slog" |
| 8 | "path/filepath" |
| 9 | |
| 10 | "github.com/jackc/pgx/v5/pgxpool" |
| 11 | |
| 12 | "github.com/tenseleyFlow/shithub/internal/auth/audit" |
| 13 | "github.com/tenseleyFlow/shithub/internal/auth/throttle" |
| 14 | "github.com/tenseleyFlow/shithub/internal/infra/config" |
| 15 | "github.com/tenseleyFlow/shithub/internal/infra/storage" |
| 16 | profileh "github.com/tenseleyFlow/shithub/internal/web/handlers/profile" |
| 17 | "github.com/tenseleyFlow/shithub/internal/web/render" |
| 18 | ) |
| 19 | |
| 20 | // buildObjectStore returns an S3-backed ObjectStore when the config |
| 21 | // supplies a bucket name, else nil. Avatar uploads (S10) are silently |
| 22 | // disabled when this is nil. |
| 23 | func buildObjectStore(s config.S3StorageConfig, logger *slog.Logger) (storage.ObjectStore, error) { |
| 24 | if s.Bucket == "" { |
| 25 | logger.Info("storage: no bucket configured; avatar uploads disabled") |
| 26 | return nil, nil |
| 27 | } |
| 28 | return storage.NewS3Store(storage.S3Config{ |
| 29 | Endpoint: s.Endpoint, |
| 30 | Region: s.Region, |
| 31 | AccessKeyID: s.AccessKeyID, |
| 32 | SecretAccessKey: s.SecretAccessKey, |
| 33 | Bucket: s.Bucket, |
| 34 | UseSSL: s.UseSSL, |
| 35 | ForcePathStyle: s.ForcePathStyle, |
| 36 | }) |
| 37 | } |
| 38 | |
| 39 | // buildProfileHandlers constructs the read-only profile handler set. |
| 40 | // objectStore may be nil — handlers fall back to the identicon path. |
| 41 | func buildProfileHandlers( |
| 42 | cfg config.Config, pool *pgxpool.Pool, objectStore storage.ObjectStore, tmplFS fs.FS, |
| 43 | logger *slog.Logger, |
| 44 | ) (*profileh.Handlers, error) { |
| 45 | rr, err := render.New(tmplFS, render.Options{Octicons: render.BuiltinOcticons()}) |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | var repoFS *storage.RepoFS |
| 50 | if cfg.Storage.ReposRoot != "" { |
| 51 | root, err := filepath.Abs(cfg.Storage.ReposRoot) |
| 52 | if err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | repoFS, err = storage.NewRepoFS(root) |
| 56 | if err != nil { |
| 57 | return nil, err |
| 58 | } |
| 59 | } |
| 60 | return profileh.New(profileh.Deps{ |
| 61 | Logger: logger, |
| 62 | Render: rr, |
| 63 | Pool: pool, |
| 64 | RepoFS: repoFS, |
| 65 | ObjectStore: objectStore, |
| 66 | Limiter: throttle.NewLimiter(), |
| 67 | Audit: audit.NewRecorder(), |
| 68 | }) |
| 69 | } |
| 70 |