Skip to content

Commit 4e9d0e0

Browse files
committed
Database connection details come from the environment.
The README.md file has been updated appropriately.
1 parent 659f1eb commit 4e9d0e0

File tree

6 files changed

+40
-36
lines changed

6 files changed

+40
-36
lines changed

README.md

+16-6
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,31 @@ If that host were to suffer a crash then five minutes after the last submission
2121

2222
To install the software run:
2323

24-
~ $ go get github.com/skx/purppura
24+
~ $ go get -u github.com/skx/purppura
25+
~ $ go install github.com/skx/purppura
2526

26-
Once installed you'll be able to launch the server like so:
27+
Once installed you'll be ready to launc the server, but first of all you
28+
must create the (MySQL) database and save the connection-details in the
29+
environment.
2730

31+
Something like this:
32+
33+
~ $ export PURPLE_DSN=user:pass@tcp(localhost:3306)/purple?timeout=5s
2834
~ $ purppura serve
2935
Listening on http://localhost:8080/
3036

31-
**NOTE** however that the server presents a web interface which requires a login, so you'll need to add at least one user to the system. This can be done while the server is running, or before you launch it:
37+
The server presents a web interface which requires a login, so you'll also want to add at least one use - this can be done while the server is running, or before you launch it:
3238

3339
~ $ purppura add-user
3440
Enter Username: moi
3541
Enter Password: kissa
3642
~ $
3743

38-
You should now be able to login to the web interface with username `steve` and password `secret`. You can use the `del-user` sub-command to remove the user in the future, or the "list-users" sub-command to see the users.
44+
> **NOTE**: Adding the user will also require the `$PURPLE_DSN` variable to be set.
45+
46+
Once the user has been added you should be able to login to the web interface with username `moi` and password `kissa`.
47+
48+
You can use the `del-user` sub-command to remove the user in the future, or the `list-users` sub-command to see the users which are present.
3949

4050

4151
# Alerts
@@ -76,13 +86,13 @@ Further details are available in the [alert guide](ALERTS.md) - and you can see
7686

7787
The web-based user-interface lists alerts which are pending, raised, or acknowledges. While this is useful it isn't going to wake anybody up if something fails overnight, so we have to allow notification via SMS, WhatsApp, etc.
7888

79-
There is no built-in facility for sending text-messages, sending pushover notifications, or similar. Instead the default alerting behaviour is to simply pipe any alert which is in the raised state into an external binary:
89+
There is no built-in facility for sending notifications, instead the default alerting behaviour is to simply pipe any alert which is in the raised state into an external binary:
8090

8191
* `purppura-notify`
8292
* Executed when an alert is raised, or re-raised.
8393
* Will receive all the details of the alert as a JSON-object on STDIN
8494

8595
By moving the notification into an external process you gain the flexibility
86-
to route alerts to humans in whichever way seems best to you. You can find sample notification-scripts which push to pushover beneath [notifiers/](notifiers/).
96+
to route alerts to humans in whichever way seems best to you. You can find a sample notification-script [notifiers/](notifiers/) which alerts a human via pushover.
8797

8898
**NOTE**: Remember that you need to add this script somewhere upon your `PATH`.

alerts/alerts.go

+7-26
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ package alerts
88

99
import (
1010
"database/sql"
11+
"errors"
1112
"fmt"
13+
"os"
1214
"time"
1315

14-
"github.com/go-sql-driver/mysql"
1516
"github.com/microcosm-cc/bluemonday"
1617
"github.com/skx/purppura/alert"
1718
"github.com/skx/purppura/util"
@@ -34,32 +35,12 @@ func New() (*Alerts, error) {
3435
m := new(Alerts)
3536

3637
//
37-
// Create a default configuration structure for MySQL.
38+
// Get our DSN from the environment
3839
//
39-
config := mysql.NewConfig()
40-
41-
//
42-
// Populate the username & password fields.
43-
//
44-
// This all needs to be configurable in the future.
45-
//
46-
config.User = "purple"
47-
config.Passwd = "purple"
48-
config.DBName = "purple"
49-
config.Net = "tcp"
50-
config.Addr = "www.vpn:3306"
51-
config.Timeout = 5 * time.Second
52-
53-
//
54-
// Now convert the connection-string to a DSN, which
55-
// is used to connect to the database.
56-
//
57-
dsn := config.FormatDSN()
58-
59-
//
60-
// Show the DSN, if appropriate.
61-
//
62-
// fmt.Printf("\tMySQL DSN is %s\n", dsn)
40+
dsn := os.Getenv("PURPLE_DSN")
41+
if dsn == "" {
42+
return m, errors.New("You must specify the environmental variable 'PURPLE_DSN' with your DB details")
43+
}
6344

6445
//
6546
// Connect to the database

cmd_add_user.go

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func (p *addUserCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{
5454
storage, err := alerts.New()
5555
if err != nil {
5656
fmt.Printf("Creating database-handle failed: %s\n", err.Error())
57+
os.Exit(1)
5758
}
5859
err = storage.AddUser(user, pass)
5960
if err != nil {

cmd_del_user.go

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func (p *delUserCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{
5050
storage, err := alerts.New()
5151
if err != nil {
5252
fmt.Printf("Creating database-handle failed: %s\n", err.Error())
53+
os.Exit(1)
5354
}
5455
err = storage.DelUser(user)
5556
if err != nil {

cmd_list_users.go

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"flag"
1010
"fmt"
11+
"os"
1112

1213
"github.com/google/subcommands"
1314
"github.com/skx/purppura/alerts"
@@ -41,6 +42,7 @@ func (p *listUsersCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interfac
4142
storage, err := alerts.New()
4243
if err != nil {
4344
fmt.Printf("Creating database-handle failed: %s\n", err.Error())
45+
os.Exit(1)
4446
}
4547

4648
var usernames []string

cmd_serve.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,13 @@ func LoadCookie() {
103103
}
104104

105105
// LoadEvents sets up a new (MySQL) database-connection.
106-
func LoadEvents() {
107-
storage, _ = alerts.New()
106+
func LoadEvents() error {
107+
var err error
108+
storage, err = alerts.New()
109+
if err != nil {
110+
return err
111+
}
112+
return nil
108113
}
109114

110115
// AddContext updates our HTTP-handlers to be username-aware.
@@ -587,7 +592,11 @@ func serve(settings serveCmd) {
587592
//
588593
// Ensure we have a database for our HTTP-handler
589594
//
590-
LoadEvents()
595+
err := LoadEvents()
596+
if err != nil {
597+
fmt.Printf("Error with DB setup: %s\n", err.Error())
598+
os.Exit(1)
599+
}
591600

592601
//
593602
// Create a scheduler to process our events frequently enough
@@ -652,7 +661,7 @@ func serve(settings serveCmd) {
652661
//
653662
// Launch the server.
654663
//
655-
err := srv.ListenAndServe()
664+
err = srv.ListenAndServe()
656665
if err != nil {
657666
fmt.Printf("\nError starting HTTP server: %s\n", err.Error())
658667
}

0 commit comments

Comments
 (0)