support .well-known dir

This commit is contained in:
Ronmi Ren 2025-01-08 05:59:50 +08:00
commit 07d12326f0
2 changed files with 15 additions and 3 deletions

View file

@ -48,7 +48,7 @@ var serveCmd = &cobra.Command{
Branch: branch, Branch: branch,
} }
s, err := lib.UseAPI(bind, f) s, err := lib.UseAPI(bind, viper.GetString("well-known"), f)
if err != nil { if err != nil {
fmt.Println("cannot create server: ", err) fmt.Println("cannot create server: ", err)
return return
@ -82,5 +82,6 @@ func init() {
f.StringP("server", "s", "", "Forgejo server address") f.StringP("server", "s", "", "Forgejo server address")
f.StringP("token", "k", "", "Forgejo api token") f.StringP("token", "k", "", "Forgejo api token")
f.StringP("branch", "b", "static-pages", "branch to use") f.StringP("branch", "b", "static-pages", "branch to use")
f.StringP("well-known", "w", "/.well-known", "well-known path, used by LetsEncrypt")
viper.BindPFlags(f) viper.BindPFlags(f)
} }

View file

@ -54,13 +54,24 @@ func (f *Forgejo) GetFile(ctx context.Context, headers map[string]string, user,
return resp, nil return resp, nil
} }
func UseAPI(bind string, f *Forgejo) (*http.Server, error) { func UseAPI(bind, wellKnown string, f *Forgejo) (*http.Server, error) {
if f == nil { if f == nil {
return nil, errors.New("forgejo is nil") return nil, errors.New("forgejo is nil")
} }
mux := http.NewServeMux()
if wellKnown != "" {
mux.Handle(
"/.well-known/",
http.StripPrefix(
"/.well-known/",
http.FileServer(http.Dir(wellKnown)),
),
)
}
mux.HandleFunc("/", f.handle)
s := &http.Server{ s := &http.Server{
Addr: bind, Addr: bind,
Handler: http.HandlerFunc(f.handle), Handler: mux,
} }
return s, nil return s, nil
} }