miniflux/main.go

146 lines
3.6 KiB
Go
Raw Normal View History

2017-11-20 06:10:04 +01:00
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package main
//go:generate go run generate.go
2017-11-20 22:46:22 +01:00
//go:generate gofmt -s -w sql/sql.go
//go:generate gofmt -s -w server/static/css.go
//go:generate gofmt -s -w server/static/bin.go
//go:generate gofmt -s -w server/static/js.go
//go:generate gofmt -s -w server/template/views.go
//go:generate gofmt -s -w server/template/common.go
//go:generate gofmt -s -w locale/translations.go
2017-11-20 06:10:04 +01:00
import (
"bufio"
"context"
"flag"
"fmt"
2017-11-20 22:46:22 +01:00
"os"
"os/signal"
"runtime"
"strings"
"time"
2017-12-13 06:48:13 +01:00
"github.com/miniflux/miniflux/config"
2017-12-16 03:55:57 +01:00
"github.com/miniflux/miniflux/logger"
2017-12-13 06:48:13 +01:00
"github.com/miniflux/miniflux/model"
"github.com/miniflux/miniflux/reader/feed"
"github.com/miniflux/miniflux/scheduler"
"github.com/miniflux/miniflux/server"
"github.com/miniflux/miniflux/storage"
"github.com/miniflux/miniflux/version"
2017-11-20 06:10:04 +01:00
_ "github.com/lib/pq"
"golang.org/x/crypto/ssh/terminal"
)
func run(cfg *config.Config, store *storage.Storage) {
2017-12-16 03:55:57 +01:00
logger.Info("Starting Miniflux...")
2017-11-20 06:10:04 +01:00
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
feedHandler := feed.NewFeedHandler(store)
pool := scheduler.NewWorkerPool(feedHandler, cfg.GetInt("WORKER_POOL_SIZE", config.DefaultWorkerPoolSize))
server := server.NewServer(cfg, store, pool, feedHandler)
2017-12-17 03:48:17 +01:00
scheduler.NewFeedScheduler(
store,
pool,
cfg.GetInt("POLLING_FREQUENCY", config.DefaultPollingFrequency),
cfg.GetInt("BATCH_SIZE", config.DefaultBatchSize),
)
2017-11-20 06:10:04 +01:00
2017-12-17 03:48:17 +01:00
scheduler.NewSessionScheduler(store, config.DefaultSessionCleanupFrequency)
2017-11-20 06:10:04 +01:00
<-stop
2017-12-16 03:55:57 +01:00
logger.Info("Shutting down the server...")
2017-11-20 06:10:04 +01:00
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
server.Shutdown(ctx)
store.Close()
2017-12-16 03:55:57 +01:00
logger.Info("Server gracefully stopped")
2017-11-20 06:10:04 +01:00
}
func askCredentials() (string, string) {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter Username: ")
username, _ := reader.ReadString('\n')
fmt.Print("Enter Password: ")
bytePassword, _ := terminal.ReadPassword(0)
fmt.Printf("\n")
return strings.TrimSpace(username), strings.TrimSpace(string(bytePassword))
}
func main() {
flagInfo := flag.Bool("info", false, "Show application information")
flagVersion := flag.Bool("version", false, "Show application version")
flagMigrate := flag.Bool("migrate", false, "Migrate database schema")
flagFlushSessions := flag.Bool("flush-sessions", false, "Flush all sessions (disconnect users)")
flagCreateAdmin := flag.Bool("create-admin", false, "Create admin user")
flag.Parse()
cfg := config.NewConfig()
store := storage.NewStorage(
cfg.Get("DATABASE_URL", config.DefaultDatabaseURL),
cfg.GetInt("DATABASE_MAX_CONNS", config.DefaultDatabaseMaxConns),
2017-11-20 06:10:04 +01:00
)
if *flagInfo {
fmt.Println("Version:", version.Version)
fmt.Println("Build Date:", version.BuildDate)
fmt.Println("Go Version:", runtime.Version())
return
}
if *flagVersion {
fmt.Println(version.Version)
return
}
if *flagMigrate {
store.Migrate()
return
}
if *flagFlushSessions {
fmt.Println("Flushing all sessions (disconnect users)")
if err := store.FlushAllSessions(); err != nil {
fmt.Println(err)
os.Exit(1)
}
return
}
if *flagCreateAdmin {
2017-11-25 19:40:23 +01:00
user := &model.User{
Username: os.Getenv("ADMIN_USERNAME"),
Password: os.Getenv("ADMIN_PASSWORD"),
IsAdmin: true,
}
if user.Username == "" || user.Password == "" {
user.Username, user.Password = askCredentials()
}
2017-11-20 06:10:04 +01:00
if err := user.ValidateUserCreation(); err != nil {
fmt.Println(err)
os.Exit(1)
}
if err := store.CreateUser(user); err != nil {
fmt.Println(err)
os.Exit(1)
}
return
}
run(cfg, store)
}