From 486cbfb6b4a9c98afc35f3f7351931285e415721 Mon Sep 17 00:00:00 2001 From: Adrian Victor Date: Fri, 15 May 2026 18:00:59 -0300 Subject: [PATCH] Add support for serving files when API doesn't return good result. --- cmd/serve.go | 14 +++- default-pages/404.html | 25 ++++++ default-pages/index.html | 26 ++++++ default-pages/logo.svg | 51 ++++++++++++ default-pages/main.css | 171 +++++++++++++++++++++++++++++++++++++++ lib/web.go | 83 +++++++++++++++---- 6 files changed, 352 insertions(+), 18 deletions(-) create mode 100644 default-pages/404.html create mode 100644 default-pages/index.html create mode 100644 default-pages/logo.svg create mode 100644 default-pages/main.css diff --git a/cmd/serve.go b/cmd/serve.go index c400476..2cbd727 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -37,6 +37,8 @@ cache/protection layer like Cloudflare in front of the server. server := viper.GetString("server") token := viper.GetString("token") branch := viper.GetString("branch") + notFound := viper.GetString("notfound") + files := viper.GetString("files") if bind == "" || server == "" || token == "" || branch == "" { fmt.Println("bind, server token and branch are required") fmt.Println("dumping flags:") @@ -44,6 +46,8 @@ cache/protection layer like Cloudflare in front of the server. fmt.Println(" server: ", server) fmt.Println(" token: ", token) fmt.Println(" branch: ", branch) + fmt.Println(" notfound: ", notFound) + fmt.Println(" files: ", files) return } serverUrl, err := url.Parse(server) @@ -52,9 +56,11 @@ cache/protection layer like Cloudflare in front of the server. return } f := &lib.Forgejo{ - Server: *serverUrl, - Token: token, - Branch: branch, + Server: *serverUrl, + Token: token, + Branch: branch, + NotFound: notFound, + Files: files, } s, err := lib.UseAPI(bind, viper.GetString("well-known"), f) @@ -85,4 +91,6 @@ func init() { f.StringP("token", "k", "", "Forgejo api token") f.StringP("branch", "b", "static-pages", "branch to use") f.StringP("well-known", "w", "", "well-known path, used by LetsEncrypt") + f.StringP("notfound", "n", "", "path to custom 404.html file") + f.StringP("files", "f", "", "path for files to be served ") } diff --git a/default-pages/404.html b/default-pages/404.html new file mode 100644 index 0000000..08b6bb9 --- /dev/null +++ b/default-pages/404.html @@ -0,0 +1,25 @@ + + + + + Inspiran + + + + +
+
+
+

Forgejo Pages

+ Make any branch available to the internet! +
+
+
+
+

404 Not Found

+

The URL does not match any repository available in this instance of Forgejo Pages.
If you think this is a mistake, please contact the system administrator.

+
+
+
+ + \ No newline at end of file diff --git a/default-pages/index.html b/default-pages/index.html new file mode 100644 index 0000000..e4cc57c --- /dev/null +++ b/default-pages/index.html @@ -0,0 +1,26 @@ + + + + + Inspiran + + + + +
+
+ +
+

Forgejo Pages

+ Make any branch available to the internet! +
+
+
+
+

Hello, there!

+

You've hit the root of this instance of Forgejo Pages. To access further content, please access ./username/repo.

+
+
+
+ + \ No newline at end of file diff --git a/default-pages/logo.svg b/default-pages/logo.svg new file mode 100644 index 0000000..b831cab --- /dev/null +++ b/default-pages/logo.svg @@ -0,0 +1,51 @@ + + + + + + + + + + diff --git a/default-pages/main.css b/default-pages/main.css new file mode 100644 index 0000000..dd3ed71 --- /dev/null +++ b/default-pages/main.css @@ -0,0 +1,171 @@ +:root { + --theme-color:#6b6e4c; + --theme-color-lighter:#bbb584; + --theme-color-variation:#ede622 +} + +* { + margin:0; + padding:0; + box-sizing:border-box +} + +body { + background-color:#000; + font-family:"Segoe UI",Tahoma,Geneva,Verdana,sans-serif; + font-size:1em; + color:#fff +} + +a { + transition:.2s; + color:#d3d3d3; + text-decoration:none +} + +a:hover { + color:var(--theme-color-lighter) +} + +header { + display:flex; + border-bottom:thick solid rgba(255,255,255,.1); + width:100%; + box-shadow:2px 7px 5px rgba(0,0,0,.3),0 -4px 10px rgba(0,0,0,.3); + background-color:rgba(0,0,0,.15) +} + +header div { + padding:1rem +} + +main { + margin-bottom:2em +} + +h1,h2,h3 { + text-shadow:2px 7px 5px rgba(0,0,0,.3),0 -4px 10px rgba(0,0,0,.3) +} + +main p { + font-size:x-large; + margin-bottom:1em +} + +main h1,main h2 { + margin-bottom: .6em +} + +main h2 { + margin-top:1em +} + +b { + font-weight:600 +} + +h2 { + font-weight:400; + color:var(--theme-color-lighter) +} + +#everythingHelper { + position:relative +} + +#mainHelper { + padding-top:3rem; + max-width:50vw; + margin:auto; + display:flex +} + +#headerSubtitle { + color:#fff; + opacity:.6 +} + +#logo { + height: 2em; + margin: auto 0 auto 1em; +} + +.noWrap { + text-wrap-mode: nowrap; +} + +@keyframes ellipsis-loader { + 0%,25% { + transform:translateX(0) + } + + 100% { + transform:translate(20px) + } +} + +@keyframes ellipsis-loader__dot { + 0% { + animation-timing-function:cubic-bezier(.23,1,.32,1); + transform:translateX(0) + } + + 55% { + animation-timing-function:cubic-bezier(.785,.135,.15,.86); + opacity:1; + transform:translateX(98px) + } + + 100%,75% { + transform:translateX(212px) + } + + 90% { + opacity:.2 + } +} + +@keyframes fadeIn { + to { + opacity:1 + } +} + +@keyframes fadeOut { + to { + opacity:0 + } +} + +@media screen and (max-width:1280px) { + body { + font-size:2vh + } + + header { + padding-top:1.4rem; + } + + header div { + margin-right: auto; + } + + main { + max-width:90vw + } + + main h1 { + font-size:3vh + } + + #mainHelper { + flex-direction:column; + max-width:90vw + } + + #logo { + margin: auto 0 auto auto; + padding-left: 2em; + } +} + diff --git a/lib/web.go b/lib/web.go index ab0da19..1f151c5 100644 --- a/lib/web.go +++ b/lib/web.go @@ -9,17 +9,21 @@ import ( "errors" "fmt" "io" + "mime" "net/http" "net/url" - "strings" - "mime" + "os" + "path" "path/filepath" + "strings" ) type Forgejo struct { - Server url.URL - Token string - Branch string + Server url.URL + Token string + Branch string + NotFound string + Files string } var ErrNotFound = errors.New("404 not found") @@ -27,7 +31,14 @@ var ErrNotFound = errors.New("404 not found") // headers must ot be nil func (f *Forgejo) GetFile(ctx context.Context, headers map[string]string, user, repo, branch, file string) (ret *http.Response, err error) { u := f.Server - u.Path = "/api/v1/repos/" + user + "/" + repo + "/raw/" + file + u.Path = path.Join( + u.Path, + "api/v1/repos", + user, + repo, + "raw", + file, + ) p := u.Query() p.Set("ref", branch) u.RawQuery = p.Encode() @@ -36,7 +47,7 @@ func (f *Forgejo) GetFile(ctx context.Context, headers map[string]string, user, if err != nil { return } - + for k, v := range headers { if v == "" { continue @@ -46,13 +57,13 @@ func (f *Forgejo) GetFile(ctx context.Context, headers map[string]string, user, req.Header.Set("Authorization", "token "+f.Token) resp, err := http.DefaultClient.Do(req) if err != nil { - return + return nil, err } if resp.StatusCode == http.StatusNotFound { - err = ErrNotFound - return + resp.Body.Close() + return nil, ErrNotFound } - + return resp, nil } @@ -87,12 +98,49 @@ func trySet(h http.Header, key string, src http.Header) { h.Set(key, value) } +func (f *Forgejo) serveLocalFile(w http.ResponseWriter, r *http.Request, status int) { + path := strings.TrimPrefix(r.URL.Path, "/") + if path == "" { + path = "index.html" + } + + if f.Files != "" { + path = filepath.Join(f.Files, path) + } + + fmt.Println("Serving local file:", path) + + data, err := os.ReadFile(path) + if err != nil { + path := f.NotFound + + data, readErr := os.ReadFile(path) + if readErr != nil { + http.Error(w, "404 not found", http.StatusNotFound) + return + } + + w.Header().Set("Content-Type", "text/html; charset=utf-8") + w.WriteHeader(http.StatusNotFound) + w.Write(data) + return + } + + if ct := mime.TypeByExtension(filepath.Ext(path)); ct != "" { + w.Header().Set("Content-Type", ct) + } + + w.WriteHeader(status) + w.Write(data) +} + func (f *Forgejo) handle(w http.ResponseWriter, r *http.Request) { arr := strings.Split(r.URL.Path[1:], "/") if len(arr) < 2 { - w.WriteHeader(http.StatusBadRequest) + f.serveLocalFile(w, r, http.StatusOK) return } + user, repo := arr[0], arr[1] if user == "u" { user, repo = repo, f.Branch @@ -104,7 +152,7 @@ func (f *Forgejo) handle(w http.ResponseWriter, r *http.Request) { } file = strings.Join(arr[2:], "/") } - + headers := map[string]string{} headers["If-None-Match"] = r.Header.Get("If-None-Match") headers["If-Modified-Since"] = r.Header.Get("If-Modified-Since") @@ -122,11 +170,16 @@ func (f *Forgejo) handle(w http.ResponseWriter, r *http.Request) { headers["CF-Ray"] = r.Header.Get("CF-Ray") resp, err := f.GetFile(r.Context(), headers, user, repo, f.Branch, file) if err != nil { + if errors.Is(err, ErrNotFound) { + f.serveLocalFile(w, r, http.StatusNotFound) + return + } + w.WriteHeader(http.StatusNotFound) return } defer resp.Body.Close() - + trySet(w.Header(), "Etag", resp.Header) trySet(w.Header(), "Last-Modified", resp.Header) trySet(w.Header(), "Content-Length", resp.Header) @@ -140,7 +193,7 @@ func (f *Forgejo) handle(w http.ResponseWriter, r *http.Request) { if contentType != "" { w.Header().Set("Content-Type", contentType) } - + w.WriteHeader(resp.StatusCode) io.Copy(w, resp.Body) }