mirror of
https://github.com/maputnik/editor.git
synced 2026-07-13 17:37:41 +00:00
Integrate file access into API
This commit is contained in:
@@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user