Skip to content

Commit

Permalink
add response option
Browse files Browse the repository at this point in the history
  • Loading branch information
czyt committed Oct 9, 2024
1 parent 4b6e9c9 commit f0fad8d
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 20 deletions.
25 changes: 25 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/base64"
"fmt"
"io"
"log"
"net/http"
Expand Down Expand Up @@ -42,6 +43,16 @@ func serve(ctx context.Context, app *app) error {

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")

if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}

encoder := base64.NewEncoder(base64.StdEncoding, w)
defer encoder.Close()

Expand Down Expand Up @@ -75,6 +86,20 @@ func subShareHandlerApp(app *app) func(w http.ResponseWriter, _ *http.Request) {
}
}

// handle for client experience
responseOpt, err := app.configure.Get().GetResponseOption(context.Background())
if err != nil {
app.Logger(context.Background()).Warn("failed to get response option", "error", err)
}
if responseOpt != nil {
if responseOpt.ProfileWebPage != "" {
w.Header().Set("profile-web-page-url", responseOpt.ProfileWebPage)
}
if responseOpt.UpdateIntervalHours > 0 {
w.Header().Set("profile-update-interval", fmt.Sprintf("%d", responseOpt.UpdateIntervalHours))
}
}

_, err = io.Copy(encoder, buf)
if err != nil {
app.Logger(context.Background()).Error("failed to copy file to http response", "error", err)
Expand Down
25 changes: 24 additions & 1 deletion sub_configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ package main

import (
"context"
"errors"
"github.com/ServiceWeaver/weaver"
)

var (
noResponseOptionConfiguredError = errors.New("no response option 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)
GetResponseOption(ctx context.Context) (*responseOption, error)
}

type subConfig struct {
Expand All @@ -20,7 +26,16 @@ type subConfig struct {
PublicUrlSubs []string `toml:"public_url_subs"`
PrivateUrlSubs []string `toml:"private_url_subs"`

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

type responseOption struct {
weaver.AutoMarshal
//return to the client see https://www.clashverge.dev/guide/url_schemes.html#_
UpdateIntervalHours int `toml:"update_interval_hours,omitempty"`
ProfileWebPage string `toml:"profile_web_page,omitempty"`
// subscription-userinfo: upload=1234; download=2234; total=1024000; expire=2218532293
}

type subConfigure struct {
Expand All @@ -44,3 +59,11 @@ func (s *subConfigure) GetUrlSubs(ctx context.Context, privateSubToken string) (
}
return append(config.PrivateUrlSubs, config.PublicUrlSubs...), config.UrlSubFetchTimeoutSeconds, nil
}

func (s *subConfigure) GetResponseOption(ctx context.Context) (*responseOption, error) {
config := s.Config()
if config.ResponseOption == nil {
return nil, noResponseOptionConfiguredError
}
return config.ResponseOption, nil
}
5 changes: 3 additions & 2 deletions weaver.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ private_url_subs = [
"url4"
]
private_sub_token = "gopher"


["raycat/subConfigureProvider".response_option]
update_interval_hours = 8
profile_web_page = "https://x.com"


[single]
Expand Down
177 changes: 160 additions & 17 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 f0fad8d

Please sign in to comment.