diff --git a/Makefile b/Makefile index 691be69a..be8a84d3 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ BINARY=maputnik all: $(BINARY) -$(BINARY): $(SOURCES) +$(BINARY): $(SOURCES) bindata_assetfs.go go build -o ${BINARY} editor/node_modules: @@ -14,8 +14,8 @@ editor/public: editor/node_modules cd editor && npm run build bindata_assetfs.go: editor/public - go-bindata-assetfs editor/public/ + go-bindata-assetfs --prefix "editor/" editor/public .PHONY: clean clean: - rm -rf editor/public && rm -rf maputnik + rm -rf editor/public && rm -f bindata_assetfs.go && rm -f maputnik diff --git a/api.go b/api.go index 5ecc68ba..20c6690e 100644 --- a/api.go +++ b/api.go @@ -2,14 +2,51 @@ package main import ( "encoding/json" + "io" + "log" "net/http" + "os" + "path/filepath" + + "github.com/gorilla/mux" ) -// Return all current deployments -func foo(w http.ResponseWriter, r *http.Request) { +func StyleFileAccessor(filename string) styleFileAccessor { + file, err := os.Open(filename) + if err != nil { + log.Fatalf("Can not access style file: %s", err.Error()) + } + + return styleFileAccessor{file} +} + +// Allows access to a single style file +type styleFileAccessor struct { + file *os.File +} + +func (fa styleFileAccessor) ListFiles(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") encoder := json.NewEncoder(w) - encoder.Encode(map[string]string{ - "foo": "bar", - }) + encoder.Encode([]string{filepath.Base(fa.file.Name())}) +} + +func (fa styleFileAccessor) ReadFile(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + _ = vars["filename"] + + //TODO: Choose right file + // right now we just return the single file we know of + w.Header().Set("Content-Type", "application/json") + if _, err := io.Copy(w, fa.file); err != nil { + log.Fatalf("Can not copy from file to request: %s", err.Error()) + } +} + +func (fa styleFileAccessor) SaveFile(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + + if _, err := io.Copy(fa.file, r.Body); err != nil { + log.Fatalf("Can not copy from request to file: %s", err.Error()) + } } diff --git a/maputnik.go b/maputnik.go index 4428a011..4db6396c 100644 --- a/maputnik.go +++ b/maputnik.go @@ -1,8 +1,10 @@ package main import ( + "fmt" "net/http" "os" + "path/filepath" "github.com/gorilla/handlers" "github.com/gorilla/mux" @@ -31,26 +33,28 @@ func main() { router := mux.NewRouter().StrictSlash(true) - // Register websocket to notify we clients about file changes filename := c.String("file") - if filename != "" { - /* - router.Path("/files").Methods("GET").HandlerFunc(listFiles) - router.Path("/files/{filename}").Methods("PUT").HandlerFunc(saveFile) - */ + fmt.Printf("%s is accessible via Maputnik\n", filename) + // Allow access to reading and writing file on the local system + path, _ := filepath.Abs(filename) + accessor := StyleFileAccessor(path) + router.Path("/files").Methods("GET").HandlerFunc(accessor.ListFiles) + router.Path("/files/{filename}").Methods("GET").HandlerFunc(accessor.ReadFile) + router.Path("/files/{filename}").Methods("PUT").HandlerFunc(accessor.SaveFile) + // Register websocket to notify we clients about file changes if c.Bool("watch") { router.Path("/ws").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { filewatch.ServeWebsocketFileWatcher(filename, w, r) }) } - } router.PathPrefix("/").Handler(http.StripPrefix("/", gui)) - loggedRouter := handlers.LoggingHandler(os.Stdout, router) + + fmt.Println("Exposing Maputnik on http://localhost:8000") return http.ListenAndServe(":8000", loggedRouter) }