depot/web/fup/cmd/serve.go

45 lines
1 KiB
Go

// SPDX-FileCopyrightText: 2021 Luke Granger-Brown <depot@lukegb.com>
//
// SPDX-License-Identifier: Apache-2.0
package cmd
import (
"context"
"fmt"
"net/http"
"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.")
}
var (
serveBind string
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: "/static/",
}
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))))
return http.ListenAndServe(serveBind, nil)
},
}
)