Skip to content

Commit

Permalink
Add custom domain message config
Browse files Browse the repository at this point in the history
  • Loading branch information
kiootic committed Nov 29, 2023
1 parent 04498ea commit b01fdeb
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 7 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ PAGESHIP_TOKEN_AUTHORITY=http://api.localtest.me:8001
PAGESHIP_CLEANUP_EXPIRED_CRONTAB=* * * * *
# PAGESHIP_HOST_ID_SCHEME=suffix

# PAGESHIP_CUSTOM_DOMAIN_MESSAGE=
21 changes: 14 additions & 7 deletions cmd/controller/app/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"time"

"github.com/carlmjohnson/versioninfo"
"github.com/dustin/go-humanize"
"github.com/oursky/pageship/internal/command"
"github.com/oursky/pageship/internal/config"
Expand Down Expand Up @@ -59,6 +60,8 @@ func init() {
startCmd.PersistentFlags().String("token-authority", "pageship", "auth token authority")
startCmd.PersistentFlags().String("token-signing-key", "", "auth token signing key")

startCmd.PersistentFlags().String("custom-domain-message", "", "message for custom domain users")

startCmd.PersistentFlags().String("cleanup-expired-crontab", "", "cleanup expired schedule")
startCmd.PersistentFlags().Duration("keep-after-expired", time.Hour*24, "keep-after-expired")

Expand Down Expand Up @@ -101,6 +104,8 @@ type StartControllerConfig struct {
TokenAuthority string `mapstructure:"token-authority"`
ReservedApps []string `mapstructure:"reserved-apps"`
APIACLFile string `mapstructure:"api-acl" validate:"omitempty,filepath"`

CustomDomainMessage string `mapstructure:"custom-domain-message"`
}

type StartCronConfig struct {
Expand Down Expand Up @@ -171,13 +176,15 @@ func (s *setup) controller(domain string, conf StartControllerConfig, sitesConf
}

controllerConf := controller.Config{
MaxDeploymentSize: int64(maxDeploymentSize),
StorageKeyPrefix: conf.StorageKeyPrefix,
HostIDScheme: sitesConf.HostIDScheme,
HostPattern: config.NewHostPattern(sitesConf.HostPattern),
ReservedApps: reservedApps,
TokenSigningKey: []byte(tokenSigningKey),
TokenAuthority: conf.TokenAuthority,
MaxDeploymentSize: int64(maxDeploymentSize),
StorageKeyPrefix: conf.StorageKeyPrefix,
HostIDScheme: sitesConf.HostIDScheme,
HostPattern: config.NewHostPattern(sitesConf.HostPattern),
ReservedApps: reservedApps,
TokenSigningKey: []byte(tokenSigningKey),
TokenAuthority: conf.TokenAuthority,
ServerVersion: versioninfo.Short(),
CustomDomainMessage: conf.CustomDomainMessage,
}

if conf.APIACLFile != "" {
Expand Down
11 changes: 11 additions & 0 deletions cmd/pageship/app/domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ var domainsCmd = &cobra.Command{
return fmt.Errorf("app ID is not set")
}

manifest, err := API().GetManifest(cmd.Context())
if err != nil {
return fmt.Errorf("failed to get manifest: %w", err)
}

app, err := API().GetApp(cmd.Context(), appID)
if err != nil {
return fmt.Errorf("failed to get app: %w", err)
Expand Down Expand Up @@ -93,6 +98,12 @@ var domainsCmd = &cobra.Command{
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", domain.name, site, createdAt, status)
}
w.Flush()

if manifest.CustomDomainMessage != "" {
os.Stdout.WriteString("\n")
Info(manifest.CustomDomainMessage)
}

return nil
},
}
Expand Down
23 changes: 23 additions & 0 deletions internal/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,29 @@ func (c *Client) attachToken(r *http.Request) error {
return nil
}

func (c *Client) GetManifest(ctx context.Context) (*APIManifest, error) {
endpoint, err := url.JoinPath(c.endpoint, "api", "v1", "manifest")
if err != nil {
return nil, err
}

req, err := http.NewRequestWithContext(ctx, "GET", endpoint, nil)
if err != nil {
return nil, err
}
if err := c.attachToken(req); err != nil {
return nil, err
}

resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

return decodeJSONResponse[*APIManifest](resp)
}

func (c *Client) CreateApp(ctx context.Context, appID string) (*APIApp, error) {
endpoint, err := url.JoinPath(c.endpoint, "api", "v1", "apps")
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions internal/api/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import (
"github.com/oursky/pageship/internal/models"
)

type APIManifest struct {
Version string `json:"version"`
CustomDomainMessage string `json:"customDomainMessage,omitempty"`
}

type APIApp struct {
*models.App
URL string `json:"url"`
Expand Down
3 changes: 3 additions & 0 deletions internal/handler/controller/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ type Config struct {
TokenAuthority string
TokenSigningKey []byte
ACL *watch.File[config.ACL]

ServerVersion string
CustomDomainMessage string
}
2 changes: 2 additions & 0 deletions internal/handler/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ func (c *Controller) Handler() http.Handler {
})
})

r.With(requireAuth).Get("/manifest", c.handleManifest)

r.With(requireAuth).Get("/auth/me", c.handleMe)
r.Get("/auth/github-ssh", c.handleAuthGithubSSH)
r.Post("/auth/github-oidc", c.handleAuthGithubOIDC)
Expand Down
17 changes: 17 additions & 0 deletions internal/handler/controller/manifest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package controller

import (
"net/http"
)

type apiManifest struct {
Version string `json:"version"`
CustomDomainMessage string `json:"customDomainMessage,omitempty"`
}

func (c *Controller) handleManifest(w http.ResponseWriter, r *http.Request) {
writeResponse(w, &apiManifest{
Version: c.Config.ServerVersion,
CustomDomainMessage: c.Config.CustomDomainMessage,
}, nil)
}

0 comments on commit b01fdeb

Please sign in to comment.