Skip to content

Commit

Permalink
Add healthcheck subcommand; close #60
Browse files Browse the repository at this point in the history
  • Loading branch information
louisroyer committed Oct 18, 2024
1 parent 06cc7c7 commit 054de34
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 3 deletions.
3 changes: 2 additions & 1 deletion internal/ctrl/rules-registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
"fmt"
"net/http"

"github.com/nextmn/srv6/internal/database"

"github.com/gin-gonic/gin"
"github.com/gofrs/uuid"
"github.com/nextmn/json-api/jsonapi"
"github.com/nextmn/srv6/internal/database"
"github.com/sirupsen/logrus"
)

Expand Down
77 changes: 77 additions & 0 deletions internal/healthcheck/healthcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2024 Louis Royer and the NextMN-SRv6 contributors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
// SPDX-License-Identifier: MIT
package healthcheck

import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"

"github.com/sirupsen/logrus"

"github.com/nextmn/srv6/internal/config"
)

type Healthcheck struct {
uri string
}

// TODO: move this in json-api
type Status struct {
Ready bool `json:"ready"`
}

func NewHealthcheck(conf *config.SRv6Config) *Healthcheck {
httpPort := "80" // default http port
if conf.HTTPPort != nil {
httpPort = *conf.HTTPPort
}
httpURI := "http://"
if conf.HTTPAddress.Is6() {
httpURI = httpURI + "[" + conf.HTTPAddress.String() + "]:" + httpPort
} else {
httpURI = httpURI + conf.HTTPAddress.String() + ":" + httpPort
}
return &Healthcheck{
uri: httpURI,
}
}
func (h *Healthcheck) Run(ctx context.Context) error {
client := http.Client{
Timeout: 100 * time.Millisecond,
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, h.uri+"/status", nil)
if err != nil {
logrus.WithError(err).Error("Error while creating http get request")
return err
}
req.Header.Add("User-Agent", "go-github-nextmn-srv6")
req.Header.Set("Accept", "application/json")
req.Header.Set("Accept-Charset", "utf-8")
resp, err := client.Do(req)
if err != nil {
logrus.WithFields(logrus.Fields{"remote-server": h.uri}).WithError(err).Info("No http response")
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
logrus.WithFields(logrus.Fields{"remote-server": h.uri}).WithError(err).Info("Http response is not 200 OK")
return err
}
decoder := json.NewDecoder(resp.Body)
var status Status
if err := decoder.Decode(&status); err != nil {
logrus.WithFields(logrus.Fields{"remote-server": h.uri}).WithError(err).Info("Could not decode json response")
return err
}
if !status.Ready {
err := fmt.Errorf("Server is not ready")
logrus.WithFields(logrus.Fields{"remote-server": h.uri}).WithError(err).Info("Server is not ready")
return err
}
return nil
}
26 changes: 24 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/nextmn/srv6/internal/app"
"github.com/nextmn/srv6/internal/config"
"github.com/nextmn/srv6/internal/healthcheck"

"github.com/adrg/xdg"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -41,7 +42,7 @@ func main() {
EnvVars: []string{"CONFIG_FILE"},
},
},
Action: func(ctx *cli.Context) error {
Before: func(ctx *cli.Context) error {
if ctx.Path("config") == "" {
if xdgPath, err := xdg.SearchConfigFile("nextmn-srv6/config.yaml"); err != nil {
cli.ShowAppHelp(ctx)
Expand All @@ -50,19 +51,40 @@ func main() {
ctx.Set("config", xdgPath)
}
}
return nil
},
Action: func(ctx *cli.Context) error {
conf, err := config.ParseConf(ctx.Path("config"))
if err != nil {
logrus.WithContext(ctx.Context).WithError(err).Fatal("Error loading config, exiting…")
}
if conf.Logger != nil {
logrus.SetLevel(conf.Logger.Level)
}

if err := app.NewSetup(conf).Run(ctx.Context); err != nil {
logrus.WithError(err).Fatal("Error while running, exiting…")
}
return nil
},
Commands: []*cli.Command{
{
Name: "healthcheck",
Usage: "check status of the node",
Action: func(ctx *cli.Context) error {
conf, err := config.ParseConf(ctx.Path("config"))
if err != nil {
logrus.WithContext(ctx.Context).WithError(err).Fatal("Error loading config, exiting…")
}
if conf.Logger != nil {
logrus.SetLevel(conf.Logger.Level)
}
if err := healthcheck.NewHealthcheck(conf).Run(ctx.Context); err != nil {
os.Exit(1)
}
return nil
},
},
},
}
if err := app.RunContext(ctx, os.Args); err != nil {
logrus.Fatal(err)
Expand Down

0 comments on commit 054de34

Please sign in to comment.