begin move to go-restful
This commit is contained in:
@ -13,7 +13,6 @@ import (
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"novit.nc/direktil/pkg/localconfig"
|
||||
)
|
||||
@ -273,7 +272,7 @@ func uploadConfig(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
err = func() (err error) {
|
||||
backupPath := filepath.Join(archivesPath, "config."+time.Now().Format(time.RFC3339)+".yaml.gz")
|
||||
backupPath := filepath.Join(archivesPath, "config."+ulid()+".yaml.gz")
|
||||
|
||||
bck, err := os.Create(backupPath)
|
||||
if err != nil {
|
||||
|
@ -6,7 +6,10 @@ import (
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
restful "github.com/emicklei/go-restful"
|
||||
"novit.nc/direktil/pkg/cas"
|
||||
|
||||
"novit.nc/direktil/local-server/pkg/apiutils"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -30,16 +33,19 @@ func main() {
|
||||
}
|
||||
|
||||
casStore = cas.NewDir(filepath.Join(*dataDir, "cache"))
|
||||
|
||||
go casCleaner()
|
||||
|
||||
apiutils.Setup(func() {
|
||||
restful.Add(buildWS())
|
||||
})
|
||||
|
||||
// by default, serve a host resource by its IP
|
||||
http.HandleFunc("/", serveHostByIP)
|
||||
//http.HandleFunc("/", serveHostByIP)
|
||||
|
||||
http.HandleFunc("/configs", uploadConfig)
|
||||
|
||||
http.HandleFunc("/hosts", serveHosts)
|
||||
http.HandleFunc("/hosts/", serveHost)
|
||||
//http.HandleFunc("/hosts/", serveHost)
|
||||
|
||||
http.HandleFunc("/clusters", serveClusters)
|
||||
http.HandleFunc("/clusters/", serveCluster)
|
||||
|
22
cmd/dkl-local-server/ulid.go
Normal file
22
cmd/dkl-local-server/ulid.go
Normal file
@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
ulidp "github.com/oklog/ulid"
|
||||
)
|
||||
|
||||
var (
|
||||
ulidCtx struct{ entropy io.Reader }
|
||||
)
|
||||
|
||||
func initUlid() {
|
||||
entropy := ulidp.Monotonic(rand.New(rand.NewSource(time.Now().UnixNano())), 0)
|
||||
ulidCtx.entropy = entropy
|
||||
}
|
||||
|
||||
func ulid() string {
|
||||
return ulidp.MustNew(ulidp.Now(), ulidCtx.entropy).String()
|
||||
}
|
55
cmd/dkl-local-server/ws-host.go
Normal file
55
cmd/dkl-local-server/ws-host.go
Normal file
@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"path"
|
||||
|
||||
restful "github.com/emicklei/go-restful"
|
||||
)
|
||||
|
||||
type wsHost struct {
|
||||
prefix string
|
||||
getHost func(req *restful.Request) string
|
||||
}
|
||||
|
||||
func (ws *wsHost) register(rws *restful.WebService) {
|
||||
for _, what := range []string{
|
||||
"boot.img",
|
||||
"boot.img.gz",
|
||||
"boot.img.lz4",
|
||||
"boot.iso",
|
||||
"boot.tar",
|
||||
"config",
|
||||
"initrd",
|
||||
"ipxe",
|
||||
"kernel",
|
||||
} {
|
||||
rws.Route(rws.GET(ws.prefix + "/" + what).To(ws.render))
|
||||
}
|
||||
}
|
||||
|
||||
func (ws *wsHost) render(req *restful.Request, resp *restful.Response) {
|
||||
hostname := ws.getHost(req)
|
||||
if hostname == "" {
|
||||
http.NotFound(resp.ResponseWriter, req.Request)
|
||||
return
|
||||
}
|
||||
|
||||
cfg, err := readConfig()
|
||||
if err != nil {
|
||||
writeError(resp.ResponseWriter, err)
|
||||
return
|
||||
}
|
||||
|
||||
host := cfg.Host(hostname)
|
||||
if host == nil {
|
||||
log.Print("no host named ", hostname)
|
||||
http.NotFound(resp.ResponseWriter, req.Request)
|
||||
return
|
||||
}
|
||||
|
||||
what := path.Base(req.Request.URL.Path)
|
||||
|
||||
renderHost(resp.ResponseWriter, req.Request, what, host, cfg)
|
||||
}
|
64
cmd/dkl-local-server/ws.go
Normal file
64
cmd/dkl-local-server/ws.go
Normal file
@ -0,0 +1,64 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/emicklei/go-restful"
|
||||
)
|
||||
|
||||
func buildWS() *restful.WebService {
|
||||
ws := &restful.WebService{}
|
||||
|
||||
ws.Route(ws.POST("/configs").To(wsUploadConfig))
|
||||
|
||||
(&wsHost{
|
||||
prefix: "",
|
||||
getHost: detectHost,
|
||||
}).register(ws)
|
||||
|
||||
(&wsHost{
|
||||
prefix: "/hosts/{hostname}",
|
||||
getHost: func(req *restful.Request) string {
|
||||
return req.PathParameter("hostname")
|
||||
},
|
||||
}).register(ws)
|
||||
|
||||
return ws
|
||||
}
|
||||
|
||||
func detectHost(req *restful.Request) string {
|
||||
r := req.Request
|
||||
remoteAddr := r.RemoteAddr
|
||||
|
||||
if *trustXFF {
|
||||
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
|
||||
remoteAddr = strings.Split(xff, ",")[0]
|
||||
}
|
||||
}
|
||||
|
||||
hostIP, _, err := net.SplitHostPort(remoteAddr)
|
||||
|
||||
if err != nil {
|
||||
hostIP = remoteAddr
|
||||
}
|
||||
|
||||
cfg, err := readConfig()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
host := cfg.HostByIP(hostIP)
|
||||
|
||||
if host == nil {
|
||||
log.Print("no host found for IP ", hostIP)
|
||||
return ""
|
||||
}
|
||||
|
||||
return host.Name
|
||||
}
|
||||
|
||||
func wsUploadConfig(req *restful.Request, res *restful.Response) {
|
||||
// TODO
|
||||
}
|
Reference in New Issue
Block a user