mirror of
https://github.com/maputnik/editor.git
synced 2026-08-24 22:17:25 +00:00
Integrate file access into API
This commit is contained in:
@@ -4,7 +4,7 @@ BINARY=maputnik
|
|||||||
|
|
||||||
all: $(BINARY)
|
all: $(BINARY)
|
||||||
|
|
||||||
$(BINARY): $(SOURCES)
|
$(BINARY): $(SOURCES) bindata_assetfs.go
|
||||||
go build -o ${BINARY}
|
go build -o ${BINARY}
|
||||||
|
|
||||||
editor/node_modules:
|
editor/node_modules:
|
||||||
@@ -14,8 +14,8 @@ editor/public: editor/node_modules
|
|||||||
cd editor && npm run build
|
cd editor && npm run build
|
||||||
|
|
||||||
bindata_assetfs.go: editor/public
|
bindata_assetfs.go: editor/public
|
||||||
go-bindata-assetfs editor/public/
|
go-bindata-assetfs --prefix "editor/" editor/public
|
||||||
|
|
||||||
.PHONY: clean
|
.PHONY: clean
|
||||||
clean:
|
clean:
|
||||||
rm -rf editor/public && rm -rf maputnik
|
rm -rf editor/public && rm -f bindata_assetfs.go && rm -f maputnik
|
||||||
|
|||||||
@@ -2,14 +2,51 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Return all current deployments
|
func StyleFileAccessor(filename string) styleFileAccessor {
|
||||||
func foo(w http.ResponseWriter, r *http.Request) {
|
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")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
encoder := json.NewEncoder(w)
|
encoder := json.NewEncoder(w)
|
||||||
encoder.Encode(map[string]string{
|
encoder.Encode([]string{filepath.Base(fa.file.Name())})
|
||||||
"foo": "bar",
|
}
|
||||||
})
|
|
||||||
|
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())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-8
@@ -1,8 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/gorilla/handlers"
|
"github.com/gorilla/handlers"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
@@ -31,26 +33,28 @@ func main() {
|
|||||||
|
|
||||||
router := mux.NewRouter().StrictSlash(true)
|
router := mux.NewRouter().StrictSlash(true)
|
||||||
|
|
||||||
// Register websocket to notify we clients about file changes
|
|
||||||
filename := c.String("file")
|
filename := c.String("file")
|
||||||
|
|
||||||
if filename != "" {
|
if filename != "" {
|
||||||
/*
|
fmt.Printf("%s is accessible via Maputnik\n", filename)
|
||||||
router.Path("/files").Methods("GET").HandlerFunc(listFiles)
|
// Allow access to reading and writing file on the local system
|
||||||
router.Path("/files/{filename}").Methods("PUT").HandlerFunc(saveFile)
|
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") {
|
if c.Bool("watch") {
|
||||||
router.Path("/ws").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
router.Path("/ws").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
filewatch.ServeWebsocketFileWatcher(filename, w, r)
|
filewatch.ServeWebsocketFileWatcher(filename, w, r)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
router.PathPrefix("/").Handler(http.StripPrefix("/", gui))
|
router.PathPrefix("/").Handler(http.StripPrefix("/", gui))
|
||||||
|
|
||||||
loggedRouter := handlers.LoggingHandler(os.Stdout, router)
|
loggedRouter := handlers.LoggingHandler(os.Stdout, router)
|
||||||
|
|
||||||
|
fmt.Println("Exposing Maputnik on http://localhost:8000")
|
||||||
return http.ListenAndServe(":8000", loggedRouter)
|
return http.ListenAndServe(":8000", loggedRouter)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user