refactor to go-restful

This commit is contained in:
Mikaël Cluseau
2019-02-04 13:56:43 +11:00
parent 4d07db7b2c
commit e93b84b60c
390 changed files with 72017 additions and 233 deletions

View File

@ -2,7 +2,6 @@ package main
import (
"log"
"net/http"
"path"
restful "github.com/emicklei/go-restful"
@ -13,39 +12,53 @@ type wsHost struct {
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",
func (ws *wsHost) register(rws *restful.WebService, alterRB func(*restful.RouteBuilder)) {
b := func(what string) *restful.RouteBuilder {
return rws.GET(ws.prefix + "/" + what).To(ws.render)
}
for _, rb := range []*restful.RouteBuilder{
// raw configuration
b("config").Doc("Get the host's configuration"),
// metal/local HDD install
b("boot.img").Doc("Get the host's boot disk image"),
b("boot.img.gz").Doc("Get the host's boot disk image (gzip compressed)"),
b("boot.img.lz4").Doc("Get the host's boot disk image (lz4 compressed)"),
// metal/local HDD upgrades
b("boot.tar").Doc("Get the host's /boot archive (ie: for metal upgrades)"),
// read-only ISO support
b("boot.iso").Doc("Get the host's boot CD-ROM image"),
// netboot support
b("ipxe").Doc("Get the host's IPXE code (for netboot)"),
b("kernel").Doc("Get the host's kernel (ie: for netboot)"),
b("initrd").Doc("Get the host's initial RAM disk (ie: for netboot)"),
} {
rws.Route(rws.GET(ws.prefix + "/" + what).To(ws.render))
alterRB(rb)
rws.Route(rb)
}
}
func (ws *wsHost) render(req *restful.Request, resp *restful.Response) {
hostname := ws.getHost(req)
if hostname == "" {
http.NotFound(resp.ResponseWriter, req.Request)
wsNotFound(req, resp)
return
}
cfg, err := readConfig()
if err != nil {
writeError(resp.ResponseWriter, err)
wsError(resp, err)
return
}
host := cfg.Host(hostname)
if host == nil {
log.Print("no host named ", hostname)
http.NotFound(resp.ResponseWriter, req.Request)
wsNotFound(req, resp)
return
}