Luke Granger-Brown
08098fb666
This adds both redirect-to-signed-URL and proxy fileserving. The proxy fileserving is somewhat limited: we don't support the Range header, and it isn't easy to reuse the net/http ServeContent implementation because that requires a SeekCloser. I think it might be possible to "bodge" a SeekCloser on top of dynamically opening files, but it'll be a bit wonky and will be slower than strictly necessary.
52 lines
1.4 KiB
Go
52 lines
1.4 KiB
Go
// SPDX-FileCopyrightText: 2021 Luke Granger-Brown <depot@lukegb.com>
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/google/safehtml"
|
|
"github.com/spf13/cobra"
|
|
"hg.lukegb.com/lukegb/depot/web/fup/fuphttp"
|
|
"hg.lukegb.com/lukegb/depot/web/fup/fupstatic"
|
|
)
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(serveCmd)
|
|
|
|
serveCmd.Flags().StringVarP(&serveBind, "listen", "l", ":8191", "Bind address for HTTP server.")
|
|
serveCmd.Flags().BoolVar(&serveDirectOnly, "direct-only", false, "If set, all file serving will be proxied, even if the backend supports signed URLs.")
|
|
}
|
|
|
|
var (
|
|
serveBind string
|
|
serveDirectOnly bool
|
|
|
|
serveCmd = &cobra.Command{
|
|
Use: "serve",
|
|
Short: "Serve HTTP",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
ctx := context.Background()
|
|
cfg := &fuphttp.Config{
|
|
Templates: fupstatic.Templates,
|
|
Static: fupstatic.Static,
|
|
StaticRoot: safehtml.TrustedResourceURLFromConstant("/static/"),
|
|
StorageURL: bucketURL,
|
|
RedirectToBlobstore: !serveDirectOnly,
|
|
}
|
|
a, err := fuphttp.New(ctx, cfg)
|
|
if err != nil {
|
|
return fmt.Errorf("constructing application: %w", err)
|
|
}
|
|
http.Handle("/", a.Handler())
|
|
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(fupstatic.Static))))
|
|
log.Printf("Serving on %s", serveBind)
|
|
return http.ListenAndServe(serveBind, nil)
|
|
},
|
|
}
|
|
)
|