mirror of
https://github.com/maputnik/editor.git
synced 2026-02-09 14:10:00 +00:00
Support saving file
This commit is contained in:
39
api.go
39
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 {
|
||||
|
||||
@@ -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") {
|
||||
|
||||
Reference in New Issue
Block a user