Add support for serving files when API doesn't return good result.
Some checks failed
/ basic (push) Failing after 19s
Some checks failed
/ basic (push) Failing after 19s
This commit is contained in:
parent
857eee205c
commit
486cbfb6b4
6 changed files with 352 additions and 18 deletions
|
|
@ -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)
|
||||
|
|
@ -55,6 +59,8 @@ cache/protection layer like Cloudflare in front of the server.
|
|||
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 ")
|
||||
}
|
||||
|
|
|
|||
25
default-pages/404.html
Normal file
25
default-pages/404.html
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en"><head>
|
||||
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||||
<meta charset="UTF-8">
|
||||
<title>Inspiran</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link href="main.css" rel="stylesheet">
|
||||
</head>
|
||||
<body>
|
||||
<div id="everythingHelper">
|
||||
<header>
|
||||
<div>
|
||||
<h1>Forgejo Pages</h1>
|
||||
<a id="headerSubtitle"><i>Make any branch available to the internet!</i></a>
|
||||
</div>
|
||||
</header>
|
||||
<div id="mainHelper">
|
||||
<main>
|
||||
<h1>404 Not Found</h1>
|
||||
<p><b>The URL does not match any repository available in this instance of Forgejo Pages.</b><br>If you think this is a mistake, please contact the system administrator.</p>
|
||||
</main>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
26
default-pages/index.html
Normal file
26
default-pages/index.html
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en"><head>
|
||||
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||||
<meta charset="UTF-8">
|
||||
<title>Inspiran</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link href="main.css" rel="stylesheet">
|
||||
</head>
|
||||
<body>
|
||||
<div id="everythingHelper">
|
||||
<header>
|
||||
<img id="logo" src="./logo.svg" alt="Forgejo Pages logo">
|
||||
<div>
|
||||
<h1>Forgejo Pages</h1>
|
||||
<a id="headerSubtitle"><i>Make any branch available to the internet!</i></a>
|
||||
</div>
|
||||
</header>
|
||||
<div id="mainHelper">
|
||||
<main>
|
||||
<h1>Hello, there!</h1>
|
||||
<p>You've hit the root of this instance of Forgejo Pages. To access further content, please access <i class="noWrap"><b>./username/repo</b></i>.</p>
|
||||
</main>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
51
default-pages/logo.svg
Normal file
51
default-pages/logo.svg
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<!-- Uploaded to: SVG Repo, www.svgrepo.com, Generator: SVG Repo Mixer Tools -->
|
||||
|
||||
<svg
|
||||
width="800px"
|
||||
height="800px"
|
||||
viewBox="0 0 16 16"
|
||||
version="1.1"
|
||||
id="svg1"
|
||||
sodipodi:docname="pages-svgrepo-com.svg"
|
||||
inkscape:version="1.4.4 (dcaf3e7d9e, 2026-05-05)"
|
||||
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
|
||||
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
xmlns:svg="http://www.w3.org/2000/svg">
|
||||
<defs
|
||||
id="defs1" />
|
||||
<sodipodi:namedview
|
||||
id="namedview1"
|
||||
pagecolor="#505050"
|
||||
bordercolor="#eeeeee"
|
||||
borderopacity="1"
|
||||
inkscape:showpageshadow="0"
|
||||
inkscape:pageopacity="0"
|
||||
inkscape:pagecheckerboard="0"
|
||||
inkscape:deskcolor="#505050"
|
||||
inkscape:zoom="0.33460581"
|
||||
inkscape:cx="318.28497"
|
||||
inkscape:cy="469.20883"
|
||||
inkscape:window-width="1912"
|
||||
inkscape:window-height="1007"
|
||||
inkscape:window-x="0"
|
||||
inkscape:window-y="0"
|
||||
inkscape:window-maximized="0"
|
||||
inkscape:current-layer="svg1" />
|
||||
<rect
|
||||
width="16"
|
||||
height="16"
|
||||
id="icon-bound"
|
||||
fill="none" />
|
||||
<path
|
||||
d="M 12,0 H 4 V 13 H 15 V 3 Z m 1,11 H 6 V 2 h 5 v 2 h 2 z"
|
||||
id="path1"
|
||||
style="fill:#00ffff"
|
||||
sodipodi:nodetypes="ccccccccccccc" />
|
||||
<path
|
||||
d="m 2.0090712,2.9970218 h -2.11999996 l 0.12,12.9999992 H 11.009072 v -2 H 2.0090712 Z"
|
||||
style="fill:#008080"
|
||||
id="path1-5"
|
||||
sodipodi:nodetypes="ccccccc" />
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 1.5 KiB |
171
default-pages/main.css
Normal file
171
default-pages/main.css
Normal file
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
67
lib/web.go
67
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
|
||||
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()
|
||||
|
|
@ -46,11 +57,11 @@ 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
|
||||
|
|
@ -122,6 +170,11 @@ 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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue