Skip to content

Commit

Permalink
feat: add embedded-fs support
Browse files Browse the repository at this point in the history
  • Loading branch information
spy16 committed Jul 27, 2022
1 parent a7c67a0 commit 77fd849
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 9 deletions.
12 changes: 7 additions & 5 deletions moonshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package moonshot
import (
"context"
"fmt"
"io/fs"

"github.com/go-chi/chi"
"github.com/spf13/cobra"
Expand All @@ -12,11 +13,12 @@ import (
// App represents an instance of app command. Invoke App.Launch()
// in `main()`.
type App struct {
Name string
Short string
Long string
CfgPtr interface{}
Routes func(r *chi.Mux)
Name string
Short string
Long string
CfgPtr interface{}
Routes func(r *chi.Mux)
StaticFS fs.FS
}

func (app *App) Launch(ctx context.Context, cmds ...*cobra.Command) int {
Expand Down
58 changes: 54 additions & 4 deletions moonshot_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ package moonshot

import (
"context"
"fmt"
"io"
"io/fs"
"mime"
"net/http"
"os"
"path/filepath"
"strings"
"time"

"github.com/go-chi/chi"
Expand Down Expand Up @@ -31,12 +38,14 @@ func (cli *App) cmdServe(ctx context.Context) *cobra.Command {
router.Get("/health", pingHandler(map[string]interface{}{
"status": "ok",
}))
if staticRoute != "" {
cli.Routes(router)

if cli.StaticFS != nil {
router.Mount(staticRoute, staticHandler(cli.StaticFS))
} else if staticDir != "" {
router.Mount(staticRoute, http.StripPrefix(staticRoute, http.FileServer(http.Dir(staticDir))))
}

cli.Routes(router)

log.Infof(ctx, "starting server at '%s'...", addr)
if err := httputils.GracefulServe(ctx, graceDur, addr, router); err != nil {
log.Fatalf(ctx, "server exited with error: %v", err)
Expand All @@ -45,7 +54,7 @@ func (cli *App) cmdServe(ctx context.Context) *cobra.Command {
}

cmd.Flags().StringVarP(&addr, "addr", "a", ":8080", "Bind address for HTTP server")
cmd.Flags().StringVarP(&staticDir, "static-dir", "D", "./app", "Directory to serve static files from")
cmd.Flags().StringVarP(&staticDir, "static-dir", "D", "", "Directory to serve static files from")
cmd.Flags().StringVarP(&staticRoute, "static-route", "R", "/", "Route to serve static files under")
cmd.Flags().DurationVarP(&graceDur, "grace-period", "G", 5*time.Second, "Grace period for shutdown")
return cmd
Expand All @@ -70,3 +79,44 @@ func pingHandler(info map[string]interface{}) http.HandlerFunc {
httputils.Respond(wr, req, http.StatusOK, info)
}
}

func staticHandler(staticFS fs.FS) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}

path := filepath.Clean(r.URL.Path)
if path == "/" { // Add other paths that you route on the UI side here
path = "index.html"
}
path = strings.TrimPrefix(path, "/")

file, err := staticFS.Open(path)
if err != nil {
if os.IsNotExist(err) {
log.Warnf(r.Context(), "file '%s' not found: %v", path, err)
http.NotFound(w, r)
return
}
log.Warnf(r.Context(), "failed to open '%s': %v", path, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}

contentType := mime.TypeByExtension(filepath.Ext(path))
w.Header().Set("Content-Type", contentType)
if strings.HasPrefix(path, "static/") {
w.Header().Set("Cache-Control", "public, max-age=31536000")
}

stat, err := file.Stat()
if err == nil && stat.Size() > 0 {
w.Header().Set("Content-Length", fmt.Sprintf("%d", stat.Size()))
}

n, _ := io.Copy(w, file)
log.Debugf(r.Context(), "%d bytes copied to client from file '%s'", n, path)
}
}

0 comments on commit 77fd849

Please sign in to comment.