Skip to content

Commit

Permalink
allow custom sub publish path
Browse files Browse the repository at this point in the history
  • Loading branch information
czyt committed Oct 9, 2024
1 parent 05ff95a commit 95c4c7b
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 7 deletions.
13 changes: 11 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"raycat/internal/pkg/tinypool"
"strconv"
"strings"

"github.com/ServiceWeaver/weaver"
)
Expand Down Expand Up @@ -36,14 +37,22 @@ type app struct {

// serve serves HTTP traffic.
func serve(ctx context.Context, app *app) error {
http.HandleFunc("/x", subShareHandlerApp(app))
config := app.configure.Get()
subPublishPath, err := config.GetSubPublishPath(ctx)
if err != nil {
app.Logger(ctx).Warn("failed to get sub publish path,will use /subscribe as default", "err", err)
subPublishPath = "/subscribe"
}
if !strings.HasPrefix(subPublishPath, "/") {
subPublishPath = "/" + subPublishPath
}
http.HandleFunc(subPublishPath, subShareHandlerApp(app))
app.Logger(ctx).Info("Listening on...", "address", app.lis)
return http.Serve(app.lis, nil)
}

func subShareHandlerApp(app *app) func(w http.ResponseWriter, _ *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {

w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization,profile-web-page-url,profile-update-interval")
Expand Down
14 changes: 12 additions & 2 deletions sub_configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import (
)

var (
noResponseOptionConfiguredError = errors.New("no response option configured")
responseOptionNotConfiguredError = errors.New("no response option configured")
subPublishPathNotConfiguredError = errors.New("sub publish path not configured")
)

var _ subConfigureProvider = (*subConfigure)(nil)

type subConfigureProvider interface {
GetSubFilePaths(ctx context.Context, privateSubToken string) ([]string, error)
GetUrlSubs(ctx context.Context, privateSubToken string) ([]string, int, error)
GetSubPublishPath(ctx context.Context) (string, error)
GetResponseOption(ctx context.Context) (*responseOption, error)
}

Expand All @@ -27,6 +29,7 @@ type subConfig struct {
PrivateUrlSubs []string `toml:"private_url_subs"`

PrivateSubToken string `toml:"private_sub_token"`
SubPublishPath string `toml:"sub_publish_path,omitempty"`
ResponseOption *responseOption `toml:"response_option,omitempty"`
}

Expand Down Expand Up @@ -60,10 +63,17 @@ func (s *subConfigure) GetUrlSubs(ctx context.Context, privateSubToken string) (
return append(config.PrivateUrlSubs, config.PublicUrlSubs...), config.UrlSubFetchTimeoutSeconds, nil
}

func (s *subConfigure) GetSubPublishPath(ctx context.Context) (string, error) {
if s.Config().SubPublishPath == "" {
return "", subPublishPathNotConfiguredError
}
return s.Config().SubPublishPath, nil
}

func (s *subConfigure) GetResponseOption(ctx context.Context) (*responseOption, error) {
config := s.Config()
if config.ResponseOption == nil {
return nil, noResponseOptionConfiguredError
return nil, responseOptionNotConfiguredError
}
return config.ResponseOption, nil
}
1 change: 1 addition & 0 deletions weaver.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ private_url_subs = [
"url4"
]
private_sub_token = "gopher"
sub_publish_path = "/subscibe"
["raycat/subConfigureProvider".response_option]
update_interval_hours = 8
profile_web_page = "https://x.com"
Expand Down
105 changes: 102 additions & 3 deletions weaver_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 95c4c7b

Please sign in to comment.