doodle/wasm/server.go
Noah Petherbridge 3d08291bc5 Demo Running Level as Title Screen Wallpaper
* Load SDL2 fonts from go-bindata storage so we don't have to ship
  external font files on disk.
* Dedupe names of doodads so we don't show double on the front-end
  (go-bindata bundled doodads + those on local filesystem)
* Use go-bindata for accessing wallpaper images.
* Better flashed messages walking you through the Link Tool.
* Stylize the title screen (MainScene) by rendering a live example level
  as the background wallpaper, with mobile doodads in motion.
2019-06-27 22:59:36 -07:00

128 lines
2.3 KiB
Go

// +build disabled
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os/exec"
"path/filepath"
"github.com/fsnotify/fsnotify"
)
func main() {
const wasm = "/doodle.wasm"
// Watch the dev directory for changes.
go watchChanges()
http.Handle("/", http.FileServer(http.Dir(".")))
http.HandleFunc(wasm, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/wasm")
http.ServeFile(w, r, "."+wasm)
})
fmt.Println("Listening at http://localhost:8080/")
log.Fatal(http.ListenAndServe(":8080", nil))
}
// onChange handler to rebuild the wasm file automatically.
func onChange() {
out, err := exec.Command("make").Output()
if err != nil {
log.Printf("error: %s", err)
}
log.Printf("%s", out)
log.Printf("Doodle WASM file rebuilt")
}
// Watch the Doodle source tree for changes to Go files and rebuild
// the wasm binary automatically.
func watchChanges() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Printf("error: %s", err)
return
}
defer watcher.Close()
done := make(chan bool)
go func() {
log.Println("Starting watch files loop")
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
log.Printf("event: %s", event)
if event.Op&fsnotify.Write == fsnotify.Write {
log.Printf("modified file: %s", event.Name)
onChange()
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Printf("error: %s", err)
}
}
}()
log.Println("Adding source directory to watcher")
dirs := crawlDirectory("../")
// Watch all these folders.
for _, dir := range dirs {
err = watcher.Add(dir)
if err != nil {
log.Printf("error: %s", err)
}
}
<-done
}
// Crawl the filesystem and return paths with Go files.
func crawlDirectory(root string) []string {
var (
ext = ".go"
result []string
has bool
)
files, err := ioutil.ReadDir(root)
if err != nil {
log.Fatalln(err)
}
for _, file := range files {
if file.Name() == ".git" {
continue
}
// Recursively scan subdirectories.
if file.IsDir() {
result = append(result,
crawlDirectory(filepath.Join(root, file.Name()))...,
)
continue
}
// This root has a file we want?
if filepath.Ext(file.Name()) == ext {
has = true
}
}
if has {
result = append(result, root)
}
return result
}