aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorMitchell Riedstra <mitch@riedstra.dev>2023-01-02 01:14:10 -0500
committerMitchell Riedstra <mitch@riedstra.dev>2023-01-02 01:14:10 -0500
commit5f1642deee2d9a717d1a56232254e80db5c32031 (patch)
tree4ad68d1cbcd835b8d5e1b97a35ea1a79bc3a058c /main.go
parentb24748777294b3646e67c4b7e599e032ee1dfcf9 (diff)
downloadpaste-5f1642deee2d9a717d1a56232254e80db5c32031.tar.gz
paste-5f1642deee2d9a717d1a56232254e80db5c32031.tar.xz
Switch to new UI for embedding. Add some additional debugging functions.
Diffstat (limited to 'main.go')
-rw-r--r--main.go79
1 files changed, 60 insertions, 19 deletions
diff --git a/main.go b/main.go
index dbbc888..e9c6020 100644
--- a/main.go
+++ b/main.go
@@ -34,7 +34,8 @@ var (
logger = log.New(os.Stderr, "", 0)
ID_BYTES = 8
- //go:embed paste-ui/build/*
+ embeddedPrefix = "ui/build" // Keep in sync with staticEmbedded below
+ //go:embed all:ui/build/*
staticEmbedded embed.FS
)
@@ -65,7 +66,8 @@ func main() {
var (
listen = ":6130"
idBytes = "8"
- debug = "false"
+ debugF = "false"
+ debug = false
genhash = false
storage = ""
fsdir = ""
@@ -74,18 +76,20 @@ func main() {
err error
)
- static, err = fs.Sub(staticEmbedded, "paste-ui/build/*")
+ dumpFStree(staticEmbedded)
+ static, err = fs.Sub(staticEmbedded, embeddedPrefix)
if err != nil {
logger.Fatal("Embedding failed no static directory")
}
+ dumpFStree(static)
fl := flag.NewFlagSet("Simple pastebin", flag.ExitOnError)
EnvFlagString(fl, &listen, "listen", "LISTEN_ADDR",
"Address to bind to")
EnvFlagString(fl, &idBytes, "b", "ID_BYTES",
"How many bytes long should the IDs be")
- EnvFlagString(fl, &debug, "d", "DEBUG",
- "Additional logger flags for debugging")
+ EnvFlagString(fl, &debugF, "d", "DEBUG",
+ "Additoinal debugging if set to true")
EnvFlagString(fl, &storage, "s", "STORAGE_DIR",
"Path of directory to serve")
EnvFlagString(fl, &fsdir, "fs", "FS_DIR",
@@ -108,11 +112,17 @@ func main() {
os.Exit(0)
}
- if d, err := strconv.ParseBool(debug); err != nil && d {
+ debug, err = strconv.ParseBool(debugF)
+ if err != nil {
+ logger.Println("Warning invalid value for debug: ", debugF)
+ }
+
+ if debug {
logger.SetFlags(log.LstdFlags | log.Llongfile)
}
if fsdir != "" {
+ logger.Println("Using filesystem directory for assets: ", fsdir)
static = os.DirFS(fsdir)
}
@@ -125,23 +135,28 @@ func main() {
"`-s` flag or STORAGE_DIR environment variable")
}
+ err = os.MkdirAll(storage, 0777)
+ if err != nil {
+ logger.Fatal("Failed to create storage directory: ", err)
+ }
+
+ if debug {
+ dumpFStree(static)
+ }
+
app := &App{
static: static,
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)
+ rp, err := getProxyHandler(proxyURL)
+ if proxyURL != "" && err == nil {
app.staticHandler = rp
- logger.Println("Proxying static requests to: ", pu)
+ logger.Println("Proxying static requests to: ", rp)
+ } else if err != nil {
+ logger.Printf("Warning, invalid url: '%s': %s", proxyURL, err)
}
-skipProxy:
logger.Println("listening on: ", listen)
@@ -154,6 +169,33 @@ skipProxy:
logger.Fatal(srv.ListenAndServe())
}
+func dumpFStree(f fs.FS) {
+ logger.Println("dumping fs tree....")
+ fs.WalkDir(f, ".", func(path string, d fs.DirEntry, err error) error {
+ if err != nil {
+ return err
+ }
+
+ if d.IsDir() {
+ return nil
+ }
+
+ logger.Println(path)
+ return nil
+ })
+}
+
+// getProxyHandler returns a httputil.NewSingleHostReverseProxy for the
+// url provided, and errors parsing the URL, if any.
+func getProxyHandler(proxyURL string) (http.Handler, error) {
+ pu, err := url.Parse(proxyURL)
+ if err != nil {
+ return nil, err
+ }
+ rp := httputil.NewSingleHostReverseProxy(pu)
+ return rp, nil
+}
+
func logRequests(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logger.Printf("%s %s %s \"%s\" \"%s\"\n",
@@ -411,7 +453,6 @@ func (a *App) HandleViewPlain() http.Handler {
})
}
-
func (a *App) HandleList() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
pl, err := LoadPastes(a.storage)
@@ -475,9 +516,9 @@ func LoadPastes(pth string) (PasteListing, error) {
}
out = append(out, &Paste{
- Id: e.Name(),
+ Id: e.Name(),
Created: info.ModTime(),
- Size: info.Size(),
+ Size: info.Size(),
})
}
@@ -527,7 +568,7 @@ func skipSlice[T any](s []T, skip int) []T {
}
func handleSkipLimitSort(pl PasteListing, URL *url.URL) PasteListing {
- if _, ok := URL.Query()["reverse"] ; ok {
+ if _, ok := URL.Query()["reverse"]; ok {
pl.SortDateReverse()
}