Add version information

This commit is contained in:
Marc Di Luzio 2020-06-02 15:54:22 +01:00
parent 07c5b9cf5a
commit e3b065ff89
5 changed files with 44 additions and 6 deletions

View file

@ -5,6 +5,7 @@ WORKDIR /app
COPY . .
RUN go mod download
RUN go build -o rove-server .
RUN go build -o rove-server -ldflags="-X version.Version=$(git describe --always --long --dirty)" .
CMD [ "./rove-server" ]
CMD "./rove-server"

View file

@ -8,13 +8,20 @@ import (
"syscall"
"github.com/mdiluz/rove/pkg/server"
"github.com/mdiluz/rove/pkg/version"
)
var ver = flag.Bool("version", false, "Display version number")
var port = flag.Int("port", 8080, "The port to host on")
func main() {
flag.Parse()
if *ver {
fmt.Println(version.Version)
os.Exit(0)
}
s := server.NewServer(
server.OptionPort(*port),
server.OptionPersistentData())

View file

@ -6,18 +6,40 @@ import (
"net/http"
"github.com/mdiluz/rove/pkg/accounts"
"github.com/mdiluz/rove/pkg/version"
)
// Route defines the information for a single path->function route
type Route struct {
path string
handler func(http.ResponseWriter, *http.Request)
}
// NewRouter sets up the server mux
func (s *Server) SetUpRouter() {
// Array of all our routes
var routes = []Route{
{
path: "/status",
handler: s.HandleStatus,
},
{
path: "/register",
handler: s.HandleRegister,
},
}
// Set up the handlers
s.router.HandleFunc("/status", s.HandleStatus)
s.router.HandleFunc("/register", s.HandleRegister)
for _, route := range routes {
s.router.HandleFunc(route.path, route.handler)
}
}
// StatusResponse is a struct that contains information on the status of the server
type StatusResponse struct {
Ready bool `json:"ready"`
Ready bool `json:"ready"`
Version string `json:"version"`
}
// HandleStatus handles HTTP requests to the /status endpoint
@ -31,7 +53,8 @@ func (s *Server) HandleStatus(w http.ResponseWriter, r *http.Request) {
}
var response = StatusResponse{
Ready: true,
Ready: true,
Version: version.Version,
}
// Be a good citizen and set the header for the return

View file

@ -21,6 +21,10 @@ func TestHandleStatus(t *testing.T) {
if status.Ready != true {
t.Errorf("got false for /status")
}
if len(status.Version) == 0 {
t.Errorf("got empty version info")
}
}
func TestHandleRegister(t *testing.T) {

3
pkg/version/version.go Normal file
View file

@ -0,0 +1,3 @@
package version
var Version = "undefined"