-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #235 from systemli/Switch-to-command-pattern
Switch to command pattern
- Loading branch information
Showing
7 changed files
with
183 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"github.com/systemli/ticker/internal/bridge" | ||
"github.com/systemli/ticker/internal/config" | ||
) | ||
|
||
var ( | ||
GitCommit string | ||
GitVersion string | ||
|
||
configPath string | ||
cfg config.Config | ||
|
||
rootCmd = &cobra.Command{ | ||
Use: "ticker", | ||
Short: "Service to distribute short messages", | ||
Long: "Service to distribute short messages in support of events, demonstrations, or other time-sensitive events.", | ||
} | ||
) | ||
|
||
func init() { | ||
cobra.OnInitialize(initConfig) | ||
rootCmd.PersistentFlags().StringVar(&configPath, "config", "config.yml", "path to config.yml") | ||
} | ||
|
||
func initConfig() { | ||
cfg = config.LoadConfig(configPath) | ||
//TODO: Improve startup routine | ||
if cfg.TelegramEnabled() { | ||
user, err := bridge.BotUser(cfg.TelegramBotToken) | ||
if err != nil { | ||
log.WithError(err).Error("Unable to retrieve the user information for the Telegram Bot") | ||
} else { | ||
cfg.TelegramBotUser = user | ||
} | ||
} | ||
|
||
lvl, err := log.ParseLevel(cfg.LogLevel) | ||
if err != nil { | ||
panic(err) | ||
} | ||
if cfg.LogFormat == "json" { | ||
log.SetFormatter(&log.JSONFormatter{}) | ||
} else { | ||
log.SetFormatter(&log.TextFormatter{FullTimestamp: true}) | ||
} | ||
log.SetLevel(lvl) | ||
} | ||
|
||
func Execute() { | ||
rootCmd.AddCommand(runCmd) | ||
|
||
if err := rootCmd.Execute(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"github.com/sethvargo/go-password/password" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"github.com/systemli/ticker/internal/api" | ||
"github.com/systemli/ticker/internal/config" | ||
"github.com/systemli/ticker/internal/storage" | ||
) | ||
|
||
var ( | ||
runCmd = &cobra.Command{ | ||
Use: "run", | ||
Short: "Run the ticker", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
log.Infof("starting ticker api on %s", cfg.Listen) | ||
if GitCommit != "" && GitVersion != "" { | ||
log.Infof("build info: %s (commit: %s)", GitVersion, GitCommit) | ||
} | ||
|
||
go func() { | ||
http.Handle("/metrics", promhttp.Handler()) | ||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
_, _ = w.Write([]byte(`<html> | ||
<head><title>Ticker Metrics Exporter</title></head> | ||
<body> | ||
<h1>Ticker Metrics Exporter</h1> | ||
<p><a href="/metrics">Metrics</a></p> | ||
</body> | ||
</html>`)) | ||
}) | ||
log.Fatal(http.ListenAndServe(cfg.MetricsListen, nil)) | ||
}() | ||
|
||
store := storage.NewStorage(cfg.Database, cfg.UploadPath) | ||
router := api.API(cfg, store) | ||
server := &http.Server{ | ||
Addr: cfg.Listen, | ||
Handler: router, | ||
} | ||
|
||
firstRun(store, cfg) | ||
|
||
go func() { | ||
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { | ||
log.Fatal(err) | ||
} | ||
}() | ||
|
||
// Wait for interrupt signal to gracefully shutdown the server with a timeout of 5 seconds. | ||
quit := make(chan os.Signal, 1) | ||
signal.Notify(quit, os.Interrupt) | ||
<-quit | ||
|
||
log.Infoln("Shutdown Ticker") | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
if err := server.Shutdown(ctx); err != nil { | ||
log.Fatal(err) | ||
} | ||
}, | ||
} | ||
) | ||
|
||
func firstRun(store storage.TickerStorage, config config.Config) { | ||
count, err := store.CountUser() | ||
if err != nil { | ||
log.Fatal("error using database") | ||
} | ||
|
||
if count == 0 { | ||
pw, err := password.Generate(24, 3, 3, false, false) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
user, err := storage.NewAdminUser(config.Initiator, pw) | ||
if err != nil { | ||
log.Fatal("could not create first user") | ||
} | ||
|
||
err = store.SaveUser(&user) | ||
if err != nil { | ||
log.Fatal("could not persist first user") | ||
} | ||
|
||
log.WithField("email", user.Email).WithField("password", pw).Info("admin user created (change password now!)") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,15 +13,17 @@ the [Systemli Ticker Project](https://www.systemli.org/en/service/ticker.html). | |
## First run | ||
|
||
- make sure you have pulled the repository | ||
```shell | ||
git clone [email protected]:systemli/ticker.git | ||
``` | ||
|
||
```shell | ||
git clone [email protected]:systemli/ticker.git | ||
``` | ||
|
||
- start the ticker | ||
```shell | ||
cp config.yml.dist config.yml | ||
go run . | ||
``` | ||
|
||
```shell | ||
cp config.yml.dist config.yml | ||
go run . run | ||
``` | ||
|
||
Now you have a running ticker API! | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,127 +1,7 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"github.com/systemli/ticker/internal/api" | ||
"github.com/systemli/ticker/internal/bridge" | ||
"github.com/systemli/ticker/internal/config" | ||
"github.com/systemli/ticker/internal/storage" | ||
|
||
"github.com/sethvargo/go-password/password" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
var ( | ||
GitCommit string | ||
GitVersion string | ||
) | ||
import "github.com/systemli/ticker/cmd" | ||
|
||
func main() { | ||
var configPath string | ||
flag.StringVar(&configPath, "config", "config.yml", "path to config.yml") | ||
flag.Parse() | ||
|
||
config := config.LoadConfig(configPath) | ||
//TODO: Improve startup routine | ||
if config.TelegramEnabled() { | ||
user, err := bridge.BotUser(config.TelegramBotToken) | ||
if err != nil { | ||
log.WithError(err).Error("Unable to retrieve the user information for the Telegram Bot") | ||
} else { | ||
config.TelegramBotUser = user | ||
} | ||
} | ||
|
||
lvl, err := log.ParseLevel(config.LogLevel) | ||
if err != nil { | ||
panic(err) | ||
} | ||
if config.LogFormat == "json" { | ||
log.SetFormatter(&log.JSONFormatter{}) | ||
} else { | ||
log.SetFormatter(&log.TextFormatter{FullTimestamp: true}) | ||
} | ||
log.SetLevel(lvl) | ||
|
||
log.Infof("starting ticker api on %s", config.Listen) | ||
if GitCommit != "" && GitVersion != "" { | ||
log.Infof("build info: %s (commit: %s)", GitVersion, GitCommit) | ||
} | ||
|
||
go func() { | ||
http.Handle("/metrics", promhttp.Handler()) | ||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
_, _ = w.Write([]byte(`<html> | ||
<head><title>Ticker Metrics Exporter</title></head> | ||
<body> | ||
<h1>Ticker Metrics Exporter</h1> | ||
<p><a href="/metrics">Metrics</a></p> | ||
</body> | ||
</html>`)) | ||
}) | ||
log.Fatal(http.ListenAndServe(config.MetricsListen, nil)) | ||
}() | ||
|
||
store := storage.NewStorage(config.Database, config.UploadPath) | ||
router := api.API(config, store) | ||
server := &http.Server{ | ||
Addr: config.Listen, | ||
Handler: router, | ||
} | ||
|
||
firstRun(store, config) | ||
|
||
go func() { | ||
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { | ||
log.Fatal(err) | ||
} | ||
}() | ||
|
||
// Wait for interrupt signal to gracefully shutdown the server with a timeout of 5 seconds. | ||
quit := make(chan os.Signal, 1) | ||
signal.Notify(quit, os.Interrupt) | ||
<-quit | ||
|
||
log.Infoln("Shutdown Ticker") | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
if err := server.Shutdown(ctx); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func firstRun(store storage.TickerStorage, config config.Config) { | ||
count, err := store.CountUser() | ||
if err != nil { | ||
log.Fatal("error using database") | ||
} | ||
|
||
if count == 0 { | ||
pw, err := password.Generate(24, 3, 3, false, false) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
user, err := storage.NewAdminUser(config.Initiator, pw) | ||
if err != nil { | ||
log.Fatal("could not create first user") | ||
} | ||
|
||
err = store.SaveUser(&user) | ||
if err != nil { | ||
log.Fatal("could not persist first user") | ||
} | ||
|
||
log.WithField("email", user.Email).WithField("password", pw).Info("admin user created (change password now!)") | ||
} | ||
cmd.Execute() | ||
} |