Skip to content

cmd/present: embed static and template files #264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
15 changes: 6 additions & 9 deletions cmd/present/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package main
import (
"html/template"
"io"
"io/fs"
"log"
"net"
"net/http"
Expand All @@ -18,10 +19,6 @@ import (
"golang.org/x/tools/present"
)

func init() {
http.HandleFunc("/", dirHandler)
}

// dirHandler serves a directory listing for the requested path, rooted at *contentPath.
func dirHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/favicon.ico" {
Expand Down Expand Up @@ -65,29 +62,29 @@ var (
contentTemplate map[string]*template.Template
)

func initTemplates(base string) error {
func initTemplates(fs fs.FS) error {
// Locate the template file.
actionTmpl := filepath.Join(base, "templates/action.tmpl")
actionTmpl := "templates/action.tmpl"

contentTemplate = make(map[string]*template.Template)

for ext, contentTmpl := range map[string]string{
".slide": "slides.tmpl",
".article": "article.tmpl",
} {
contentTmpl = filepath.Join(base, "templates", contentTmpl)
contentTmpl = filepath.Join("templates", contentTmpl)

// Read and parse the input.
tmpl := present.Template()
tmpl = tmpl.Funcs(template.FuncMap{"playable": playable})
if _, err := tmpl.ParseFiles(actionTmpl, contentTmpl); err != nil {
if _, err := tmpl.ParseFS(fs, actionTmpl, contentTmpl); err != nil {
return err
}
contentTemplate[ext] = tmpl
}

var err error
dirListTemplate, err = template.ParseFiles(filepath.Join(base, "templates/dir.tmpl"))
dirListTemplate, err = template.ParseFS(fs, "templates/dir.tmpl")
return err
}

Expand Down
32 changes: 21 additions & 11 deletions cmd/present/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
package main

import (
"embed"
"flag"
"fmt"
"go/build"
"io/fs"
"log"
"net"
"net/http"
Expand Down Expand Up @@ -50,16 +51,25 @@ func main() {
*contentPath = "./content/"
}

if *basePath == "" {
p, err := build.Default.Import(basePkg, "", build.FindOnly)
baseFS := func() fs.FS {
if *basePath != "" {
return os.DirFS(*basePath)
}
//go:embed base
var staticFS embed.FS

// by default, lets use the embedded files.
fs, err := fs.Sub(staticFS, "base")
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't find gopresent files: %v\n", err)
fmt.Fprintf(os.Stderr, basePathMessage, basePkg)
os.Exit(1)
panic(err)
}
*basePath = p.Dir
}
err := initTemplates(*basePath)

return fs
}()

http.HandleFunc("/", dirHandler)

err := initTemplates(baseFS)
if err != nil {
log.Fatalf("Failed to parse templates: %v", err)
}
Expand Down Expand Up @@ -98,8 +108,8 @@ func main() {
}
}

initPlayground(*basePath, origin)
http.Handle("/static/", http.FileServer(http.Dir(*basePath)))
initPlayground(baseFS, origin)
http.Handle("/static/", http.FileServer(http.FS(baseFS)))

if !ln.Addr().(*net.TCPAddr).IP.IsLoopback() &&
present.PlayEnabled && !*nativeClient && !*usePlayground {
Expand Down
21 changes: 16 additions & 5 deletions cmd/present/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package main
import (
"bytes"
"fmt"
"io/fs"
"io/ioutil"
"net/http"
"net/url"
Expand All @@ -31,18 +32,27 @@ var scripts = []string{"jquery.js", "jquery-ui.js", "playground.js", "play.js"}
// playScript registers an HTTP handler at /play.js that serves all the
// scripts specified by the variable above, and appends a line that
// initializes the playground with the specified transport.
func playScript(root, transport string) {
// func playScript(root, transport string) {
func playScript(fs fs.FS, transport string) {
modTime := time.Now()
var buf bytes.Buffer
for _, p := range scripts {
if s, ok := static.Files[p]; ok {
buf.WriteString(s)
continue
}
b, err := ioutil.ReadFile(filepath.Join(root, "static", p))

f, err := fs.Open(filepath.Join("static", p))
if err != nil {
panic(err)
}
defer f.Close()

b, err := ioutil.ReadAll(f)
if err != nil {
panic(err)
}

buf.Write(b)
}
fmt.Fprintf(&buf, "\ninitPlayground(new %v());\n", transport)
Expand All @@ -53,12 +63,13 @@ func playScript(root, transport string) {
})
}

func initPlayground(basepath string, origin *url.URL) {
// func initPlayground(basepath string, origin *url.URL) {
func initPlayground(fs fs.FS, origin *url.URL) {
if !present.PlayEnabled {
return
}
if *usePlayground {
playScript(basepath, "HTTPTransport")
playScript(fs, "HTTPTransport")
return
}

Expand All @@ -73,7 +84,7 @@ func initPlayground(basepath string, origin *url.URL) {
return environ("GOOS=nacl")
}
}
playScript(basepath, "SocketTransport")
playScript(fs, "SocketTransport")
http.Handle("/socket", socket.NewHandler(origin))
}

Expand Down
Loading