wisski-cloud-distillery/cmd/dis_server.go
Tom Wiesing 4b357476a3
Add 'dis' component
This commit adds a new 'dis' component to the distillery that serves a
list of all known instances for the moment.
2022-09-09 17:10:24 +02:00

43 lines
1.1 KiB
Go

package cmd
import (
"net/http"
wisski_distillery "github.com/FAU-CDI/wisski-distillery"
"github.com/FAU-CDI/wisski-distillery/core"
"github.com/tkw1536/goprogram/exit"
)
// DisServer is the 'dis_server' command
var DisServer wisski_distillery.Command = disServer{}
type disServer struct {
Prefix string `short:"p" long:"prefix" description:"prefix to listen under"`
Bind string `short:"b" long:"bind" description:"address to listen on" default:"127.0.0.1:8888"`
}
func (disServer) Description() wisski_distillery.Description {
return wisski_distillery.Description{
Requirements: core.Requirements{
NeedsDistillery: true,
},
Command: "dis_server",
Description: "Starts a server with information about this distillery",
}
}
var errServerListen = exit.Error{
ExitCode: exit.ExitGeneric,
Message: "Unable to listen",
}
func (s disServer) Run(context wisski_distillery.Context) error {
server := context.Environment.Server()
context.Printf("Listening on %s\n", s.Bind)
err := http.ListenAndServe(s.Bind, http.StripPrefix(s.Prefix, server))
if err == nil {
return nil
}
return errServerListen.Wrap(err)
}