fup: fix config file handling

Whoops! Before, config files were being read but all the contents were basically
being discarded.

Now, we both load and actually use the config file, leading to a much more positive
experience for everyone involved :)
This commit is contained in:
Luke Granger-Brown 2021-03-23 01:21:39 +00:00
parent a99e0309c5
commit affe252f73
2 changed files with 40 additions and 37 deletions

View file

@ -12,8 +12,7 @@ import (
) )
var ( var (
cfgFile string cfgFile string
bucketURL string
rootCmd = &cobra.Command{ rootCmd = &cobra.Command{
Use: "fup", Use: "fup",
@ -22,6 +21,8 @@ var (
} }
) )
func bucketURL() string { return viper.GetString("storage.bucketURL") }
func Execute() error { func Execute() error {
return rootCmd.Execute() return rootCmd.Execute()
} }
@ -30,8 +31,9 @@ func init() {
cobra.OnInitialize(initConfig) cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file") rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file")
rootCmd.PersistentFlags().StringVar(&bucketURL, "bucket-url", "mem://", "gocloud.dev-compatible URL to use for file storage") rootCmd.PersistentFlags().String("bucket-url", "mem://", "gocloud.dev-compatible URL to use for file storage")
viper.BindPFlag("storage.bucketURL", rootCmd.PersistentFlags().Lookup("bucket-url")) viper.BindPFlag("storage.bucketURL", rootCmd.PersistentFlags().Lookup("bucket-url"))
viper.SetDefault("storage.bucketURL", "mem://")
} }
func initConfig() { func initConfig() {

View file

@ -24,45 +24,46 @@ import (
func init() { func init() {
rootCmd.AddCommand(serveCmd) rootCmd.AddCommand(serveCmd)
serveCmd.Flags().StringVar(&serveRoot, "root", "http://localhost:8191/", "Application root address.") serveCmd.Flags().String("root", "http://localhost:8191/", "Application root address.")
viper.BindPFlag("serve.app-root", serveCmd.Flags().Lookup("root")) viper.BindPFlag("serve.app-root", serveCmd.Flags().Lookup("root"))
serveCmd.Flags().StringVar(&serveStaticRoot, "static-root", "/static/", "Root address from which static assets should be referenced.") viper.SetDefault("serve.app-root", "http://localhost:8191/")
viper.BindPFlag("serve.static-root", serveCmd.Flags().Lookup("static-root"))
serveCmd.Flags().StringVarP(&serveBind, "listen", "l", ":8191", "Bind address for HTTP server.")
viper.BindPFlag("serve.listen", serveCmd.Flags().Lookup("listen"))
serveCmd.Flags().BoolVar(&serveDirectOnly, "direct-only", false, "If set, all file serving will be proxied, even if the backend supports signed URLs.")
viper.BindPFlag("serve.direct-only", serveCmd.Flags().Lookup("direct-only"))
serveCmd.Flags().StringVar(&serveCheddarPath, "cheddar-path", "cheddar", "Path to 'cheddar' binary to use for syntax highlighting. If it cannot be found, syntax highlighting and markdown rendering will be disabled.") serveCmd.Flags().String("static-root", "/static/", "Root address from which static assets should be referenced.")
viper.BindPFlag("serve.static-root", serveCmd.Flags().Lookup("static-root"))
viper.SetDefault("serve.static-root", "/static/")
serveCmd.Flags().StringP("listen", "l", ":8191", "Bind address for HTTP server.")
viper.BindPFlag("serve.listen", serveCmd.Flags().Lookup("listen"))
viper.SetDefault("serve.listen", ":8191")
serveCmd.Flags().Bool("direct-only", false, "If set, all file serving will be proxied, even if the backend supports signed URLs.")
viper.BindPFlag("serve.direct-only", serveCmd.Flags().Lookup("direct-only"))
viper.SetDefault("serve.direct-only", false)
serveCmd.Flags().String("cheddar-path", "cheddar", "Path to 'cheddar' binary to use for syntax highlighting. If it cannot be found, syntax highlighting and markdown rendering will be disabled.")
viper.BindPFlag("serve.cheddar.path", serveCmd.Flags().Lookup("cheddar-path")) viper.BindPFlag("serve.cheddar.path", serveCmd.Flags().Lookup("cheddar-path"))
serveCmd.Flags().StringVar(&serveCheddarAddr, "cheddar-address", "", "If non-empty, will be used instead of attempting to spawn a copy of cheddar.") viper.SetDefault("serve.cheddar.path", "cheddar")
serveCmd.Flags().String("cheddar-address", "", "If non-empty, will be used instead of attempting to spawn a copy of cheddar.")
viper.BindPFlag("serve.cheddar.address", serveCmd.Flags().Lookup("cheddar-address")) viper.BindPFlag("serve.cheddar.address", serveCmd.Flags().Lookup("cheddar-address"))
serveCmd.Flags().StringVar(&serveAuthToken, "auth-token", "", "If non-empty, this auth token will be required as the Basic Auth password.") serveCmd.Flags().String("auth-token", "", "If non-empty, this auth token will be required as the Basic Auth password.")
viper.BindPFlag("serve.auth.token", serveCmd.Flags().Lookup("auth-token")) viper.BindPFlag("serve.auth.token", serveCmd.Flags().Lookup("auth-token"))
serveCmd.Flags().StringVar(&serveAuthRealm, "auth-realm", "fup", "Will be used as the realm for Basic Auth.") serveCmd.Flags().String("auth-realm", "fup", "Will be used as the realm for Basic Auth.")
viper.BindPFlag("serve.auth.realm", serveCmd.Flags().Lookup("auth-realm")) viper.BindPFlag("serve.auth.realm", serveCmd.Flags().Lookup("auth-realm"))
viper.SetDefault("serve.auth.realm", "fup")
} }
var ( var (
serveBind string
serveRoot string
serveStaticRoot string
serveDirectOnly bool
serveCheddarPath string
serveCheddarAddr string
serveAuthToken string
serveAuthRealm string
serveCmd = &cobra.Command{ serveCmd = &cobra.Command{
Use: "serve", Use: "serve",
Short: "Serve HTTP", Short: "Serve HTTP",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
if !strings.HasSuffix(serveRoot, "/") { if !strings.HasSuffix(viper.GetString("serve.app-root"), "/") {
return fmt.Errorf("--root flag should end in / (value is %q)", serveRoot) return fmt.Errorf("--root flag (serve.app-root) should end in / (value is %q)", viper.GetString("serve.app-root"))
} }
if !strings.HasSuffix(serveStaticRoot, "/") { if !strings.HasSuffix(viper.GetString("serve.static-root"), "/") {
return fmt.Errorf("--static-root flag should end in / (value is %q)", serveStaticRoot) return fmt.Errorf("--static-root flag (serve.static-root) should end in / (value is %q)", viper.GetString("serve.static-root"))
} }
ctx := context.Background() ctx := context.Background()
@ -75,11 +76,11 @@ var (
Templates: fupstatic.Templates, Templates: fupstatic.Templates,
Static: fupstatic.Static, Static: fupstatic.Static,
StaticRoot: safehtml.TrustedResourceURLFromFlag(cmd.Flag("static-root").Value), StaticRoot: safehtml.TrustedResourceURLFromFlag(cmd.Flag("static-root").Value),
AppRoot: serveRoot, AppRoot: viper.GetString("serve.app-root"),
StorageURL: bucketURL, StorageURL: bucketURL(),
RedirectToBlobstore: !serveDirectOnly, RedirectToBlobstore: !viper.GetBool("serve.direct-only"),
Highlighter: highlighter, Highlighter: highlighter,
AuthMiddleware: fuphttp.TokenAuthMiddleware(serveAuthToken, serveAuthRealm), AuthMiddleware: fuphttp.TokenAuthMiddleware(viper.GetString("serve.auth.token"), viper.GetString("serve.auth.realm")),
} }
a, err := fuphttp.New(ctx, cfg) a, err := fuphttp.New(ctx, cfg)
if err != nil { if err != nil {
@ -87,8 +88,8 @@ var (
} }
http.Handle("/", a.Handler()) http.Handle("/", a.Handler())
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(fupstatic.Static)))) http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(fupstatic.Static))))
log.Printf("Serving on %s", serveBind) log.Printf("Serving on %s", viper.GetString("serve.listen"))
if serveBind == "systemd" { if viper.GetString("serve.listen") == "systemd" {
listeners, err := activation.Listeners() listeners, err := activation.Listeners()
if err != nil { if err != nil {
return fmt.Errorf("getting systemd socket-activated listeners: %v", err) return fmt.Errorf("getting systemd socket-activated listeners: %v", err)
@ -98,19 +99,19 @@ var (
} }
return http.Serve(listeners[0], nil) return http.Serve(listeners[0], nil)
} }
return http.ListenAndServe(serveBind, nil) return http.ListenAndServe(viper.GetString("serve.listen"), nil)
}, },
} }
) )
func serveCheddar(ctx context.Context) (*minicheddar.Cheddar, error) { func serveCheddar(ctx context.Context) (*minicheddar.Cheddar, error) {
if serveCheddarAddr != "" { if serveCheddarAddr := viper.GetString("serve.cheddar.addr"); serveCheddarAddr != "" {
return minicheddar.Remote(serveCheddarAddr), nil return minicheddar.Remote(serveCheddarAddr), nil
} }
cpath, err := exec.LookPath(serveCheddarPath) cpath, err := exec.LookPath(viper.GetString("serve.cheddar.path"))
if err != nil { if err != nil {
log.Printf("couldn't find cheddar at %q; disabling syntax highlighting", serveCheddarPath) log.Printf("couldn't find cheddar at %q; disabling syntax highlighting", viper.GetString("serve.cheddar.path"))
return nil, nil return nil, nil
} }