restful /configs

This commit is contained in:
Mikaël Cluseau 2019-02-01 18:35:50 +11:00
parent f4f285d0dc
commit 4d07db7b2c
4 changed files with 82 additions and 81 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
*.sw[po] *.sw[po]
modd-local.conf

View File

@ -1,16 +1,11 @@
package main package main
import ( import (
"compress/gzip"
"encoding/json" "encoding/json"
"flag" "flag"
"io"
"io/ioutil"
"log" "log"
"net" "net"
"net/http" "net/http"
"os"
"path/filepath"
"regexp" "regexp"
"strings" "strings"
@ -236,79 +231,6 @@ func serveCluster(w http.ResponseWriter, r *http.Request) {
} }
} }
func uploadConfig(w http.ResponseWriter, r *http.Request) {
if !authorizeAdmin(r) {
forbidden(w, r)
return
}
if r.Method != "POST" {
http.NotFound(w, r)
return
}
out, err := ioutil.TempFile(*dataDir, ".config-upload")
if err != nil {
writeError(w, err)
return
}
defer os.Remove(out.Name())
_, err = io.Copy(out, r.Body)
out.Close()
if err != nil {
writeError(w, err)
return
}
archivesPath := filepath.Join(*dataDir, "archives")
cfgPath := configFilePath()
err = os.MkdirAll(archivesPath, 0700)
if err != nil {
writeError(w, err)
return
}
err = func() (err error) {
backupPath := filepath.Join(archivesPath, "config."+ulid()+".yaml.gz")
bck, err := os.Create(backupPath)
if err != nil {
return
}
defer bck.Close()
in, err := os.Open(cfgPath)
if err != nil {
return
}
gz, err := gzip.NewWriterLevel(bck, 2)
if err != nil {
return
}
_, err = io.Copy(gz, in)
gz.Close()
in.Close()
return
}()
if err != nil {
writeError(w, err)
return
}
err = os.Rename(out.Name(), cfgPath)
if err != nil {
writeError(w, err)
return
}
}
func writeError(w http.ResponseWriter, err error) { func writeError(w http.ResponseWriter, err error) {
log.Print("request failed: ", err) log.Print("request failed: ", err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)

View File

@ -42,7 +42,7 @@ func main() {
// by default, serve a host resource by its IP // by default, serve a host resource by its IP
//http.HandleFunc("/", serveHostByIP) //http.HandleFunc("/", serveHostByIP)
http.HandleFunc("/configs", uploadConfig) //http.HandleFunc("/configs", uploadConfig)
http.HandleFunc("/hosts", serveHosts) http.HandleFunc("/hosts", serveHosts)
//http.HandleFunc("/hosts/", serveHost) //http.HandleFunc("/hosts/", serveHost)

View File

@ -1,8 +1,14 @@
package main package main
import ( import (
"compress/gzip"
"io"
"io/ioutil"
"log" "log"
"net" "net"
"net/http"
"os"
"path/filepath"
"strings" "strings"
"github.com/emicklei/go-restful" "github.com/emicklei/go-restful"
@ -59,6 +65,78 @@ func detectHost(req *restful.Request) string {
return host.Name return host.Name
} }
func wsUploadConfig(req *restful.Request, res *restful.Response) { func wsUploadConfig(req *restful.Request, resp *restful.Response) {
// TODO r := req.Request
w := resp.ResponseWriter
if !authorizeAdmin(r) {
forbidden(w, r)
return
}
if r.Method != "POST" {
http.NotFound(w, r)
return
}
out, err := ioutil.TempFile(*dataDir, ".config-upload")
if err != nil {
writeError(w, err)
return
}
defer os.Remove(out.Name())
_, err = io.Copy(out, r.Body)
out.Close()
if err != nil {
writeError(w, err)
return
}
archivesPath := filepath.Join(*dataDir, "archives")
cfgPath := configFilePath()
err = os.MkdirAll(archivesPath, 0700)
if err != nil {
writeError(w, err)
return
}
err = func() (err error) {
backupPath := filepath.Join(archivesPath, "config."+ulid()+".yaml.gz")
bck, err := os.Create(backupPath)
if err != nil {
return
}
defer bck.Close()
in, err := os.Open(cfgPath)
if err != nil {
return
}
gz, err := gzip.NewWriterLevel(bck, 2)
if err != nil {
return
}
_, err = io.Copy(gz, in)
gz.Close()
in.Close()
return
}()
if err != nil {
writeError(w, err)
return
}
err = os.Rename(out.Name(), cfgPath)
if err != nil {
writeError(w, err)
return
}
} }