aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorMitchell Riedstra <mitch@riedstra.dev>2022-12-26 00:11:58 -0500
committerMitchell Riedstra <mitch@riedstra.dev>2022-12-26 00:11:58 -0500
commitfd3e3280a2590be9ca074a172c535990a5035649 (patch)
tree232f23010449a5aea88052c6e860a68ef9bdeb34 /main.go
parent0704674ba408db54855c33bcb8ca71a7ae1e74b7 (diff)
downloadpaste-fd3e3280a2590be9ca074a172c535990a5035649.tar.gz
paste-fd3e3280a2590be9ca074a172c535990a5035649.tar.xz
Add a proxy option for static assets. Fix the paste view handlers.
Diffstat (limited to 'main.go')
-rw-r--r--main.go54
1 files changed, 38 insertions, 16 deletions
diff --git a/main.go b/main.go
index c3f5613..e8e1f02 100644
--- a/main.go
+++ b/main.go
@@ -4,7 +4,6 @@ import (
"bytes"
"crypto/rand"
"embed"
- _ "embed"
"encoding/base64"
"encoding/json"
"errors"
@@ -14,6 +13,8 @@ import (
"io/fs"
"log"
"net/http"
+ "net/http/httputil"
+ "net/url"
"os"
"path/filepath"
"strconv"
@@ -32,14 +33,15 @@ var (
logger = log.New(os.Stderr, "", 0)
ID_BYTES = 8
- //go:embed static/*
+ //go:embed paste-ui/build/*
staticEmbedded embed.FS
)
type App struct {
- static fs.FS
- users map[string]string
- storage string // path to where the paste files are actually stored
+ static fs.FS
+ users map[string]string
+ storage string // path to where the paste files are actually stored
+ staticHandler http.Handler
}
// EnvFlagString is a convent way to set value from environment variable,
@@ -60,17 +62,18 @@ type errResp struct {
func main() {
var (
- listen = ":6130"
- idBytes = "8"
- debug = "false"
- genhash = false
- storage = ""
- fsdir = ""
- static fs.FS
- err error
+ listen = ":6130"
+ idBytes = "8"
+ debug = "false"
+ genhash = false
+ storage = ""
+ fsdir = ""
+ proxyURL = ""
+ static fs.FS
+ err error
)
- static, err = fs.Sub(staticEmbedded, "static")
+ static, err = fs.Sub(staticEmbedded, "paste-ui/build/*")
if err != nil {
logger.Fatal("Embedding failed no static directory")
}
@@ -86,6 +89,8 @@ func main() {
"Path of directory to serve")
EnvFlagString(fl, &fsdir, "fs", "FS_DIR",
"Path which static assets are located, empty to use embedded")
+ EnvFlagString(fl, &proxyURL, "sp", "STATIC_PROXY",
+ "What server do we proxy to for static content?")
fl.BoolVar(&genhash, "genhash", genhash,
"Interactively prompt for a password and spit out a hash\n")
version := fl.Bool("v", false, "Print version then exit")
@@ -124,6 +129,19 @@ func main() {
storage: storage,
users: getUsersFromEnviron(),
}
+
+ if proxyURL != "" {
+ pu, err := url.Parse(proxyURL)
+ if err != nil {
+ logger.Printf("Warning, invalid url: '%s': %s", proxyURL, err)
+ goto skipProxy
+ }
+ rp := httputil.NewSingleHostReverseProxy(pu)
+ app.staticHandler = rp
+ logger.Println("Proxying static requests to: ", pu)
+ }
+skipProxy:
+
logger.Println("listening on: ", listen)
srv := &http.Server{
@@ -181,17 +199,21 @@ func (a *App) Handler() http.Handler {
}
handlers := map[string]http.Handler{
- "/api/v1/view": http.StripPrefix(
+ "/api/v1/view/": http.StripPrefix(
"/api/v1/view/",
a.HandleViewJSON()),
- "/view": http.StripPrefix(
+ "/view/": http.StripPrefix(
"/view/",
a.HandleViewPlain()),
"/": http.FileServer(http.FS(a.static)),
}
+ if a.staticHandler != nil {
+ handlers["/"] = a.staticHandler
+ }
+
if len(a.users) > 0 {
for user := range a.users {
logger.Println("Found user:", user)