Skip to content

Commit

Permalink
Improve and enhance the web service (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanio authored Aug 6, 2024
2 parents c06ff4c + 48a1b49 commit d70f651
Show file tree
Hide file tree
Showing 14 changed files with 238 additions and 156 deletions.
19 changes: 13 additions & 6 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ tasks:
- task: lint
- task: test
- task: build
- task: security

lint:
desc: Check linting of Go, JSON, YAML, and Markdown files
Expand All @@ -126,16 +127,12 @@ tasks:
- task: markdownlint

test:
desc: Run the unit tests and static code analysis
desc: Run the unit tests
summary: |
Run the unit and integration tests for the Go application, as well as any
static code analysis and security analysis of the application with tools
such as Trivy and CodeQL.
Run the unit and integration tests for the Go application.
silent: true
cmds:
- task: go:test
- task: go:trivy
- task: go:codeql

build:
desc: Locally build the application binary
Expand All @@ -147,6 +144,16 @@ tasks:
cmds:
- task: go:build

security:
desc: Run security and static code analysis checks against the application
summary: |
Run static code analysis and security analysis of the application with
tools such as Trivy and CodeQL.
silent: true
cmds:
- task: go:trivy
- task: go:codeql

run:
desc: Run the dashboard service
summary: |
Expand Down
3 changes: 0 additions & 3 deletions config/send.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
---
endpoint-uri: http://localhost:8080
api-key: d54813f8-9a23-470b-be06-d35b150f9fc1 # gitleaks:allow

logging:
json: false
8 changes: 4 additions & 4 deletions config/serve.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
web:
bind:
address: localhost
address: 0.0.0.0
port: 8080
proxies:
- '::1'
- '172.27.4.188'
proxies:
- '::1'
- '172.27.4.188'

logging:
json: true
Expand Down
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ go 1.22.4
require (
github.com/MakeNowJust/heredoc/v2 v2.0.1
github.com/gin-gonic/gin v1.10.0
github.com/prometheus/client_golang v1.19.1
github.com/samber/slog-gin v1.13.3
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.9.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
Expand All @@ -37,6 +40,9 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
Expand Down
20 changes: 16 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
github.com/MakeNowJust/heredoc/v2 v2.0.1 h1:rlCHh70XXXv7toz95ajQWOWQnN4WNLt0TdpZYIR/J6A=
github.com/MakeNowJust/heredoc/v2 v2.0.1/go.mod h1:6/2Abh5s+hc3g9nbWLe9ObDIOhaRrqsyY9MWy+4JdRM=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
Expand Down Expand Up @@ -70,8 +74,16 @@ github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/prometheus/client_golang v1.19.1 h1:wZWJDwK+NameRJuPGDhlnFgx8e8HN3XHQeLaYJFJBOE=
github.com/prometheus/client_golang v1.19.1/go.mod h1:mP78NwGzrVks5S2H6ab8+ZZGJLZUq1hoULYBAYBw1Ho=
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI=
github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSzKKE=
github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc=
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
Expand Down Expand Up @@ -135,8 +147,8 @@ golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
36 changes: 24 additions & 12 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,25 @@ import (
"github.com/spf13/viper"
)

// rootCmd represents the base command when called without any subcommands and
// provides the point from which all other subcommands are attached.
var rootCmd = &cobra.Command{
Use: fmt.Sprintf("%s [options] command [options]", Name),
Short: "A server and client for running a web-based dashboard",
Long: heredoc.Doc(`
var (

// configFile is a place holder for Cobra to store the alternate
// configuration file, if set, from the command-line.
configFile = ""

// rootCmd represents the base command when called without any subcommands and
// provides the point from which all other subcommands are attached.
rootCmd = &cobra.Command{
Use: fmt.Sprintf("%s [options] command [options]", Name),
Short: "A server and client for running a web-based dashboard",
Long: heredoc.Doc(`
dashboard is a web-based application which provides a high-level dashboard
for receiving and displaying events and related information about any
system. For example, when a deployment has been triggered and what is it's
final status, or a function has been triggered, or a job has been run.
`),

Example: strings.TrimRight(heredoc.Doc(`
Example: strings.TrimRight(heredoc.Doc(`
$ dashboard serve \
--endpoint-uri https://development.dashboard.n3t.uk
Expand All @@ -32,19 +38,25 @@ var rootCmd = &cobra.Command{
--status pass \
--message 'This is a test message for the dashboard'
`), "\n"),
}
}
)

// init will initialise the command-line settings for `rootCmd` command, as well
// as set up the general application configuration (such as the processing of
// environment variables) and settings.
func init() {
flags := rootCmd.PersistentFlags()
flags.StringVarP(&configFile, "config", "c", "", "Path to the configuration file")

// Provide configuration for the logger, including setting JSON, structured
// output, and the level of logging output by default
viper.SetDefault("logging.level", "info")
flags.StringP("log-level", "l", "info", "Set the logging level (debug, info, warning, error)")
_ = viper.BindPFlag("logging.level", flags.Lookup("log-level"))

err := viper.BindPFlag("logging.level", flags.Lookup("log-level"))
if err != nil {
panic(err)
}
viper.SetDefault("logging.json", false)
flags.BoolP("log-json", "j", false, "Output logs in JSON format")
_ = viper.BindPFlag("logging.json", flags.Lookup("log-json"))
}

// Execute executes `rootCmd` and therefore starts the application, with all the
Expand Down
75 changes: 27 additions & 48 deletions internal/cmd/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,53 +19,35 @@ const (
sendConfigName = "send.yaml"
)

var (
// sendConfigFile is a place holder for Cobra to store the alternate
// configuration file, if set, from the command-line.
sendConfigFile string

// sendCmd represents the send command for the dashboard application, and will
// provide the setup and arguments needed for the application to build an
// event and send it to the dashboard endpoint for processing.
sendCmd = &cobra.Command{
Use: "send [options]",
Short: "Send events and updates to the dashboard web service",
Long: heredoc.Doc(`
dashboard send provides a mechanism to construct and send an event to the
web service using either an input file or command-line arguments to build
and/or override the events.
`),

// Add blank line at the top for enforced extra spacing in the output
Example: strings.TrimRight(heredoc.Doc(`
$ dashboard send \
--endpoint-uri https://development.dashboard.n3t.uk \
--event-id this-is-a-test-message \
--status pass \
--message 'This is a test message for the dashboard'
`), "\n"),

RunE: runSend,
}
)
// sendCmd represents the send command for the dashboard application, and will
// provide the setup and arguments needed for the application to build an
// event and send it to the dashboard endpoint for processing.
var sendCmd = &cobra.Command{
Use: "send [options]",
Short: "Send events and updates to the dashboard web service",
Long: heredoc.Doc(`
dashboard send provides a mechanism to construct and send an event to the
web service using either an input file or command-line arguments to build
and/or override the events.
`),

// Add blank line at the top for enforced extra spacing in the output
Example: strings.TrimRight(heredoc.Doc(`
$ dashboard send \
--endpoint-uri https://development.dashboard.n3t.uk \
--event-id this-is-a-test-message \
--status pass \
--message 'This is a test message for the dashboard'
`), "\n"),

RunE: runSend,
}

// init will initialise the command-line settings for `sendCmd` command,
// including any command-specific flags.
func init() {
var err error

flags := sendCmd.PersistentFlags()

flags.StringVarP(&sendConfigFile, "config", "c", "", "Path to the configuration file")

flags.BoolP("log-json", "j", true, "Output logs in JSON format")

err = viper.BindPFlag("logging.json", flags.Lookup("log-json"))
if err != nil {
panic(err)
}

viper.SetDefault("endpoint-uri", "http://localhost:8080")
rootCmd.AddCommand(sendCmd)
}

Expand All @@ -74,16 +56,13 @@ func init() {
// endpoint. If there was an error processing the configuration or the event, an
// `error` will be returned.
func runSend(_ *cobra.Command, _ []string) error {
err := config.Load(sendConfigName, sendConfigFile)
err := config.Load(sendConfigName, configFile)
if err != nil {
//nolint:revive,stylecheck // new-line is required to break error and usage
return fmt.Errorf("\n %w\n", err)
}

send.Prepare()
logger.Start(nil)

err = send.Run()

return err
return send.Run()
}
Loading

0 comments on commit d70f651

Please sign in to comment.