dedup & fix

This commit is contained in:
6543 2023-02-09 21:27:47 +01:00
parent 3c0ee7e8a3
commit a656335a22
7 changed files with 189 additions and 213 deletions

View File

@ -31,32 +31,14 @@ var Certs = &cli.Command{
Action: migrateCerts, Action: migrateCerts,
}, },
}, },
Flags: []cli.Flag{ Flags: append(CertStorageFlags, []cli.Flag{
// Cert Storage
// TODO: remove in next version
&cli.StringFlag{
// DEPRICATED
Name: "db-pogreb",
Value: "key-database.pogreb",
EnvVars: []string{"DB_POGREB"},
},
&cli.StringFlag{
Name: "db-type",
Value: "", // TODO: "sqlite3" in next version
EnvVars: []string{"DB_TYPE"},
},
&cli.StringFlag{
Name: "db-conn",
Value: "certs.sqlite",
EnvVars: []string{"DB_CONN"},
},
&cli.BoolFlag{ &cli.BoolFlag{
Name: "verbose", Name: "verbose",
Usage: "print trace info", Usage: "print trace info",
EnvVars: []string{"VERBOSE"}, EnvVars: []string{"VERBOSE"},
Value: false, Value: false,
}, },
}, }...),
} }
func migrateCerts(ctx *cli.Context) error { func migrateCerts(ctx *cli.Context) error {
@ -148,32 +130,3 @@ func removeCert(ctx *cli.Context) error {
} }
return nil return nil
} }
func openCertDB(ctx *cli.Context) (certDB database.CertDB, err error) {
if ctx.String("db-type") != "" {
certDB, err = database.NewXormDB(ctx.String("db-type"), ctx.String("db-conn"))
if err != nil {
return nil, fmt.Errorf("could not connect to database: %w", err)
}
} else {
// TODO: remove in next version
fmt.Println(`
######################
## W A R N I N G !!! #
######################
You use "pogreb" witch is deprecated and will be removed in the next version.
Please switch to sqlite, mysql or postgres !!!
The simplest way is, to use './pages certs migrate' and set environment var DB_TYPE to 'sqlite' on next start.
`)
log.Error().Msg("depricated \"pogreb\" used\n")
certDB, err = database.NewPogreb(ctx.String("db-pogreb"))
if err != nil {
return nil, fmt.Errorf("could not create database: %w", err)
}
}
return certDB, nil
}

View File

@ -4,139 +4,149 @@ import (
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
var ServeFlags = []cli.Flag{ var (
// MainDomainSuffix specifies the main domain (starting with a dot) for which subdomains shall be served as static CertStorageFlags = []cli.Flag{
// pages, or used for comparison in CNAME lookups. Static pages can be accessed through &cli.StringFlag{
// https://{owner}.{MainDomain}[/{repo}], with repo defaulting to "pages". // TODO: remove in next version
&cli.StringFlag{ // DEPRICATED
Name: "pages-domain", Name: "db-pogreb",
Usage: "specifies the main domain (starting with a dot) for which subdomains shall be served as static pages", Value: "key-database.pogreb",
EnvVars: []string{"PAGES_DOMAIN"}, EnvVars: []string{"DB_POGREB"},
Value: "codeberg.page", },
}, &cli.StringFlag{
// GiteaRoot specifies the root URL of the Gitea instance, without a trailing slash. Name: "db-type",
&cli.StringFlag{ Value: "", // TODO: "sqlite3" in next version
Name: "gitea-root", EnvVars: []string{"DB_TYPE"},
Usage: "specifies the root URL of the Gitea instance, without a trailing slash.", },
EnvVars: []string{"GITEA_ROOT"}, &cli.StringFlag{
Value: "https://codeberg.org", Name: "db-conn",
}, Value: "certs.sqlite",
// GiteaApiToken specifies an api token for the Gitea instance EnvVars: []string{"DB_CONN"},
&cli.StringFlag{ },
Name: "gitea-api-token", }
Usage: "specifies an api token for the Gitea instance",
EnvVars: []string{"GITEA_API_TOKEN"},
Value: "",
},
// RawDomain specifies the domain from which raw repository content shall be served in the following format:
// https://{RawDomain}/{owner}/{repo}[/{branch|tag|commit}/{version}]/{filepath...}
// (set to []byte(nil) to disable raw content hosting)
&cli.StringFlag{
Name: "raw-domain",
Usage: "specifies the domain from which raw repository content shall be served, not set disable raw content hosting",
EnvVars: []string{"RAW_DOMAIN"},
Value: "raw.codeberg.page",
},
// RawInfoPage will be shown (with a redirect) when trying to access RawDomain directly (or without owner/repo/path).
&cli.StringFlag{
Name: "raw-info-page",
Usage: "will be shown (with a redirect) when trying to access $RAW_DOMAIN directly (or without owner/repo/path)",
EnvVars: []string{"RAW_INFO_PAGE"},
Value: "https://docs.codeberg.org/codeberg-pages/raw-content/",
},
// Server ServerFlags = append(CertStorageFlags, []cli.Flag{
&cli.StringFlag{ // #############
Name: "host", // ### Gitea ###
Usage: "specifies host of listening address", // #############
EnvVars: []string{"HOST"}, // GiteaRoot specifies the root URL of the Gitea instance, without a trailing slash.
Value: "[::]", &cli.StringFlag{
}, Name: "gitea-root",
&cli.StringFlag{ Usage: "specifies the root URL of the Gitea instance, without a trailing slash.",
Name: "port", EnvVars: []string{"GITEA_ROOT"},
Usage: "specifies port of listening address", Value: "https://codeberg.org",
EnvVars: []string{"PORT"}, },
Value: "443", // GiteaApiToken specifies an api token for the Gitea instance
}, &cli.StringFlag{
&cli.BoolFlag{ Name: "gitea-api-token",
Name: "enable-http-server", Usage: "specifies an api token for the Gitea instance",
// TODO: desc EnvVars: []string{"GITEA_API_TOKEN"},
EnvVars: []string{"ENABLE_HTTP_SERVER"}, Value: "",
}, },
// Server Options
&cli.BoolFlag{
Name: "enable-lfs-support",
Usage: "enable lfs support, require gitea v1.17.0 as backend",
EnvVars: []string{"ENABLE_LFS_SUPPORT"},
Value: true,
},
&cli.BoolFlag{
Name: "enable-symlink-support",
Usage: "follow symlinks if enabled, require gitea v1.18.0 as backend",
EnvVars: []string{"ENABLE_SYMLINK_SUPPORT"},
Value: true,
},
&cli.StringFlag{
Name: "log-level",
Value: "warn",
Usage: "specify at which log level should be logged. Possible options: info, warn, error, fatal",
EnvVars: []string{"LOG_LEVEL"},
},
// ACME // ###########################
&cli.StringFlag{ // ### Page Server Domains ###
Name: "acme-api-endpoint", // ###########################
EnvVars: []string{"ACME_API"}, // MainDomainSuffix specifies the main domain (starting with a dot) for which subdomains shall be served as static
Value: "https://acme-v02.api.letsencrypt.org/directory", // pages, or used for comparison in CNAME lookups. Static pages can be accessed through
}, // https://{owner}.{MainDomain}[/{repo}], with repo defaulting to "pages".
&cli.StringFlag{ &cli.StringFlag{
Name: "acme-email", Name: "pages-domain",
EnvVars: []string{"ACME_EMAIL"}, Usage: "specifies the main domain (starting with a dot) for which subdomains shall be served as static pages",
Value: "noreply@example.email", EnvVars: []string{"PAGES_DOMAIN"},
}, Value: "codeberg.page",
&cli.BoolFlag{ },
Name: "acme-use-rate-limits", // RawDomain specifies the domain from which raw repository content shall be served in the following format:
// TODO: Usage // https://{RawDomain}/{owner}/{repo}[/{branch|tag|commit}/{version}]/{filepath...}
EnvVars: []string{"ACME_USE_RATE_LIMITS"}, // (set to []byte(nil) to disable raw content hosting)
Value: true, &cli.StringFlag{
}, Name: "raw-domain",
&cli.BoolFlag{ Usage: "specifies the domain from which raw repository content shall be served, not set disable raw content hosting",
Name: "acme-accept-terms", EnvVars: []string{"RAW_DOMAIN"},
// TODO: Usage Value: "raw.codeberg.page",
EnvVars: []string{"ACME_ACCEPT_TERMS"}, },
}, // RawInfoPage will be shown (with a redirect) when trying to access RawDomain directly (or without owner/repo/path).
&cli.StringFlag{ &cli.StringFlag{
Name: "acme-eab-kid", Name: "raw-info-page",
// TODO: Usage Usage: "will be shown (with a redirect) when trying to access $RAW_DOMAIN directly (or without owner/repo/path)",
EnvVars: []string{"ACME_EAB_KID"}, EnvVars: []string{"RAW_INFO_PAGE"},
}, Value: "https://docs.codeberg.org/codeberg-pages/raw-content/",
&cli.StringFlag{ },
Name: "acme-eab-hmac",
// TODO: Usage
EnvVars: []string{"ACME_EAB_HMAC"},
},
&cli.StringFlag{
Name: "dns-provider",
// TODO: Usage
EnvVars: []string{"DNS_PROVIDER"},
},
// Cert Storage // Server
// TODO: remove in next version &cli.StringFlag{
&cli.StringFlag{ Name: "host",
// DEPRICATED Usage: "specifies host of listening address",
Name: "db-pogreb", EnvVars: []string{"HOST"},
Value: "key-database.pogreb", Value: "[::]",
EnvVars: []string{"DB_POGREB"}, },
}, &cli.StringFlag{
&cli.StringFlag{ Name: "port",
Name: "db-type", Usage: "specifies port of listening address",
Value: "", // TODO: "sqlite3" in next version EnvVars: []string{"PORT"},
EnvVars: []string{"DB_TYPE"}, Value: "443",
}, },
&cli.StringFlag{ &cli.BoolFlag{
Name: "db-conn", Name: "enable-http-server",
Value: "certs.sqlite", // TODO: desc
EnvVars: []string{"DB_CONN"}, EnvVars: []string{"ENABLE_HTTP_SERVER"},
}, },
} // Server Options
&cli.BoolFlag{
Name: "enable-lfs-support",
Usage: "enable lfs support, require gitea v1.17.0 as backend",
EnvVars: []string{"ENABLE_LFS_SUPPORT"},
Value: true,
},
&cli.BoolFlag{
Name: "enable-symlink-support",
Usage: "follow symlinks if enabled, require gitea v1.18.0 as backend",
EnvVars: []string{"ENABLE_SYMLINK_SUPPORT"},
Value: true,
},
&cli.StringFlag{
Name: "log-level",
Value: "warn",
Usage: "specify at which log level should be logged. Possible options: info, warn, error, fatal",
EnvVars: []string{"LOG_LEVEL"},
},
// ACME
&cli.StringFlag{
Name: "acme-api-endpoint",
EnvVars: []string{"ACME_API"},
Value: "https://acme-v02.api.letsencrypt.org/directory",
},
&cli.StringFlag{
Name: "acme-email",
EnvVars: []string{"ACME_EMAIL"},
Value: "noreply@example.email",
},
&cli.BoolFlag{
Name: "acme-use-rate-limits",
// TODO: Usage
EnvVars: []string{"ACME_USE_RATE_LIMITS"},
Value: true,
},
&cli.BoolFlag{
Name: "acme-accept-terms",
// TODO: Usage
EnvVars: []string{"ACME_ACCEPT_TERMS"},
},
&cli.StringFlag{
Name: "acme-eab-kid",
// TODO: Usage
EnvVars: []string{"ACME_EAB_KID"},
},
&cli.StringFlag{
Name: "acme-eab-hmac",
// TODO: Usage
EnvVars: []string{"ACME_EAB_HMAC"},
},
&cli.StringFlag{
Name: "dns-provider",
// TODO: Usage
EnvVars: []string{"DNS_PROVIDER"},
},
}...)
)

View File

@ -18,7 +18,6 @@ import (
"codeberg.org/codeberg/pages/server" "codeberg.org/codeberg/pages/server"
"codeberg.org/codeberg/pages/server/cache" "codeberg.org/codeberg/pages/server/cache"
"codeberg.org/codeberg/pages/server/certificates" "codeberg.org/codeberg/pages/server/certificates"
"codeberg.org/codeberg/pages/server/database"
"codeberg.org/codeberg/pages/server/gitea" "codeberg.org/codeberg/pages/server/gitea"
"codeberg.org/codeberg/pages/server/handler" "codeberg.org/codeberg/pages/server/handler"
) )
@ -75,33 +74,7 @@ func Serve(ctx *cli.Context) error {
} }
// Init ssl cert database // Init ssl cert database
var certDB database.CertDB certDB, err := openCertDB(ctx)
if ctx.String("db-type") != "" {
log.Trace().Msg("use xorm mode")
certDB, err = database.NewXormDB(ctx.String("db-type"), ctx.String("db-conn"))
if err != nil {
return fmt.Errorf("could not connect to database: %w", err)
}
} else {
// TODO: remove in next version
fmt.Println(`
######################
## W A R N I N G !!! #
######################
You use "pogreb" witch is deprecated and will be removed in the next version.
Please switch to sqlite, mysql or postgres !!!
The simplest way is, to use './pages certs migrate' and set environment var DB_TYPE to 'sqlite' on next start.
`)
log.Error().Msg("depricated \"pogreb\" used\n")
certDB, err = database.NewPogreb(ctx.String("db-pogreb"))
if err != nil {
return fmt.Errorf("could not create database: %w", err)
}
}
defer certDB.Close() defer certDB.Close()
keyCache := cache.NewKeyValueCache() keyCache := cache.NewKeyValueCache()

40
cmd/setup.go Normal file
View File

@ -0,0 +1,40 @@
package cmd
import (
"fmt"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
"codeberg.org/codeberg/pages/server/database"
)
func openCertDB(ctx *cli.Context) (certDB database.CertDB, err error) {
if ctx.String("db-type") != "" {
log.Trace().Msg("use xorm mode")
certDB, err = database.NewXormDB(ctx.String("db-type"), ctx.String("db-conn"))
if err != nil {
return nil, fmt.Errorf("could not connect to database: %w", err)
}
} else {
// TODO: remove in next version
fmt.Println(`
######################
## W A R N I N G !!! #
######################
You use "pogreb" witch is deprecated and will be removed in the next version.
Please switch to sqlite, mysql or postgres !!!
The simplest way is, to use './pages certs migrate' and set environment var DB_TYPE to 'sqlite' on next start.
`)
log.Error().Msg("depricated \"pogreb\" used\n")
certDB, err = database.NewPogreb(ctx.String("db-pogreb"))
if err != nil {
return nil, fmt.Errorf("could not create database: %w", err)
}
}
return certDB, nil
}

View File

@ -44,7 +44,7 @@ func startServer(ctx context.Context) error {
app := cli.NewApp() app := cli.NewApp()
app.Name = "pages-server" app.Name = "pages-server"
app.Action = cmd.Serve app.Action = cmd.Serve
app.Flags = cmd.ServeFlags app.Flags = cmd.ServerFlags
go func() { go func() {
if err := app.RunContext(ctx, args); err != nil { if err := app.RunContext(ctx, args); err != nil {

View File

@ -19,7 +19,7 @@ func main() {
app.Version = version app.Version = version
app.Usage = "pages server" app.Usage = "pages server"
app.Action = cmd.Serve app.Action = cmd.Serve
app.Flags = cmd.ServeFlags app.Flags = cmd.ServerFlags
app.Commands = []*cli.Command{ app.Commands = []*cli.Command{
cmd.Certs, cmd.Certs,
} }

View File

@ -90,7 +90,7 @@ func (x xDB) Compact() (string, error) {
// Items return al certs from db, if pageSize is 0 it does not use limit // Items return al certs from db, if pageSize is 0 it does not use limit
func (x xDB) Items(page, pageSize int) ([]*Cert, error) { func (x xDB) Items(page, pageSize int) ([]*Cert, error) {
// paginated return // paginated return
if pageSize >= 0 { if pageSize > 0 {
certs := make([]*Cert, 0, pageSize) certs := make([]*Cert, 0, pageSize)
if page >= 0 { if page >= 0 {
page = 1 page = 1