Skip to content

Add static HTTP file server for /.well-known/ endpoint #25892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,12 @@ func registerRoutes(m *web.Route) {
ctx.Redirect(setting.AppSubURL + "/user/settings/account")
})
})
wellKnownDir := wellKnownWebDir()
m.Get("/.well-known/*", func(ctx *context.Context) {
fsHandler := http.FileServer(wellKnownDir)
fs := http.StripPrefix("/.well-known/", fsHandler)
fs.ServeHTTP(ctx.Resp, ctx.Req)
})

m.Group("/explore", func() {
m.Get("", func(ctx *context.Context) {
Expand Down
37 changes: 37 additions & 0 deletions routers/web/well_known.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package web

import (
"net/http"
"os"
"path/filepath"

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
)

// create or check .well-known directory exist
func wellKnownWebDir() http.Dir {
wellKnownDir := http.Dir(filepath.Join(setting.CustomPath, "web-well-known"))
makeDirExist(string(wellKnownDir))

return wellKnownDir
}

// check directory exist, create if not
func makeDirExist(dirName string) {
_, err := os.Stat(dirName)
if err == nil {
return
}
if os.IsNotExist(err) {
err := os.Mkdir(dirName, os.FileMode(0o755))
if err != nil {
log.Fatal(".well-known directory (%s) mkdir error %v", dirName, err)
}
} else {
log.Warn(".well-known directory (%s) check error %v", dirName, err)
}
}