From 0d77518a02b3fe5445e3e3d3c28bf01270722854 Mon Sep 17 00:00:00 2001 From: Lukas Martinelli Date: Sat, 3 Dec 2016 16:51:47 +0100 Subject: [PATCH] Support saving file --- api.go | 39 ++++++++++++++++++++++++++++++++++----- maputnik.go | 6 +++--- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/api.go b/api.go index 20c6690e..ac149fef 100644 --- a/api.go +++ b/api.go @@ -2,48 +2,77 @@ package main import ( "encoding/json" + "fmt" "io" + "io/ioutil" "log" "net/http" "os" - "path/filepath" "github.com/gorilla/mux" ) func StyleFileAccessor(filename string) styleFileAccessor { - file, err := os.Open(filename) + file, err := os.OpenFile(filename, os.O_RDWR, 0666) if err != nil { log.Fatalf("Can not access style file: %s", err.Error()) } - return styleFileAccessor{file} + return styleFileAccessor{file, styleId(file)} +} + +func styleId(file *os.File) string { + raw, err := ioutil.ReadAll(file) + if err != nil { + log.Panicln(err) + } + + var spec styleSpec + err = json.Unmarshal(raw, &spec) + if err != nil { + log.Panicln(err) + } + + if spec.Id == "" { + fmt.Println("No id in style") + } + return spec.Id +} + +type styleSpec struct { + Id string `json:"id"` } // Allows access to a single style file type styleFileAccessor struct { file *os.File + id string } func (fa styleFileAccessor) ListFiles(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") encoder := json.NewEncoder(w) - encoder.Encode([]string{filepath.Base(fa.file.Name())}) + encoder.Encode([]string{fa.id}) } func (fa styleFileAccessor) ReadFile(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - _ = vars["filename"] + _ = vars["styleId"] //TODO: Choose right file // right now we just return the single file we know of w.Header().Set("Content-Type", "application/json") + fa.file.Seek(0, 0) 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) { + vars := mux.Vars(r) + _ = vars["styleId"] + + //TODO: Save to right file w.Header().Set("Content-Type", "application/json") if _, err := io.Copy(fa.file, r.Body); err != nil { diff --git a/maputnik.go b/maputnik.go index 4db6396c..a86a417a 100644 --- a/maputnik.go +++ b/maputnik.go @@ -39,9 +39,9 @@ func main() { // 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) + router.Path("/styles").Methods("GET").HandlerFunc(accessor.ListFiles) + router.Path("/styles/{styleId}").Methods("GET").HandlerFunc(accessor.ReadFile) + router.Path("/styles/{styleId}").Methods("PUT").HandlerFunc(accessor.SaveFile) // Register websocket to notify we clients about file changes if c.Bool("watch") {