Initial commit.
Linting and tests / Linting (push) Failing after 30s
Linting and tests / Tests (push) Successful in 25s

This commit is contained in:
2026-06-10 10:23:00 +05:00
commit 3d43b8a84e
34 changed files with 1325 additions and 0 deletions
+203
View File
@@ -0,0 +1,203 @@
package application
import (
"context"
"errors"
"fmt"
"log/slog"
"os"
"strings"
"time"
)
var errApplication = errors.New("application")
// Application is a lifecycle controlling structure for application.
type Application struct {
shutdownChan chan struct{}
ctx context.Context
cancelFunc context.CancelFunc
baseLogger *slog.Logger
appLogger *slog.Logger
services []Service
}
// New creates new instance of lifecycle controlling structure.
func New() *Application {
appl := &Application{}
appl.initialize()
return appl
}
func (a *Application) configure() error {
// First iteration - core services.
for _, service := range a.services {
if !strings.Contains(service.Name(), "core/") {
continue
}
a.appLogger.Debug("Launching configuration procedure for service", "service", service.Name())
if err := service.Configure(); err != nil {
return fmt.Errorf("configure service '%s': %w", service.Name(), err)
}
}
// Second iteration - rest of the services.
for _, service := range a.services {
if strings.Contains(service.Name(), "core/") {
continue
}
a.appLogger.Debug("Launching configuration procedure for service", "service", service.Name())
if err := service.Configure(); err != nil {
return fmt.Errorf("configure service '%s': %w", service.Name(), err)
}
}
return nil
}
func (a *Application) connectDependencies() error {
// First iteration - core services.
for _, service := range a.services {
if !strings.Contains(service.Name(), "core/") {
continue
}
a.appLogger.Debug("Connecting dependencies for service.", "service", service.Name())
if err := service.ConnectDependencies(); err != nil {
return fmt.Errorf("connect dependencies for service '%s': %w", service.Name(), err)
}
}
// Second iteration - rest of the services.
for _, service := range a.services {
if strings.Contains(service.Name(), "core/") {
continue
}
a.appLogger.Debug("Connecting dependencies for service.", "service", service.Name())
if err := service.ConnectDependencies(); err != nil {
return fmt.Errorf("connect dependencies for service '%s': %w", service.Name(), err)
}
}
return nil
}
// Context returns application-wide context.
func (a *Application) Context() context.Context {
return a.ctx
}
// ContextWithTimeout returns context.Context with requested timeout.
func (a *Application) ContextWithTimeout(timeout time.Duration) context.Context {
ctx, cancelFunc := context.WithTimeout(a.ctx, timeout)
// As we do not need to call cancelFunc - make linter happy.
// This probably will lead to context leak, so it should be investigated.
go func(_ context.CancelFunc) {}(cancelFunc)
return ctx
}
func (a *Application) initialize() {
a.ctx, a.cancelFunc = context.WithCancel(context.Background())
a.initializeLogger()
a.services = make([]Service, 0)
}
func (a *Application) launchStartupTasks() error {
for _, service := range a.services {
if strings.Contains(service.Name(), "mainwindow") {
continue
}
if !strings.Contains(service.Name(), "core/") {
continue
}
a.appLogger.Debug("Launching startup tasks for service.", "service", service.Name())
if err := service.LaunchStartupTasks(); err != nil {
return fmt.Errorf("launch startup tasks for core/%s: %w", service.Name(), err)
}
}
for _, service := range a.services {
if strings.Contains(service.Name(), "core/") {
continue
}
a.appLogger.Debug("Launching startup tasks for service.", "service", service.Name())
if err := service.LaunchStartupTasks(); err != nil {
return fmt.Errorf("launch startup tasks for core/%s: %w", service.Name(), err)
}
}
return nil
}
// Shutdown stops application.
func (a *Application) Shutdown() error {
a.appLogger.Info("Stopping vikunja-notifier...")
a.cancelFunc()
for _, service := range a.services {
if !strings.Contains(service.Name(), "features/") {
continue
}
a.appLogger.Debug("Shutting down service.", "service", service.Name())
if err := service.Shutdown(); err != nil {
return fmt.Errorf("shutting down service '%s': %w", service.Name(), err)
}
}
for _, service := range a.services {
if !strings.Contains(service.Name(), "core/") {
continue
}
a.appLogger.Debug("Shutting down service.", "service", service.Name())
if err := service.Shutdown(); err != nil {
return fmt.Errorf("shutting down service '%s': %w", service.Name(), err)
}
}
os.Exit(0)
return nil
}
// Start starts application.
// Server application will start a goroutine that monitors SIGTERM and sends empty struct to channel.
func (a *Application) Start() error {
if err := a.connectDependencies(); err != nil {
return fmt.Errorf("%w: %w", errApplication, err)
}
if err := a.configure(); err != nil {
return fmt.Errorf("%w: %w", errApplication, err)
}
if err := a.launchStartupTasks(); err != nil {
return fmt.Errorf("%w: %w", errApplication, err)
}
a.startServer()
return nil
}
+45
View File
@@ -0,0 +1,45 @@
package application
import (
"log/slog"
"os"
"strings"
)
const (
defaultLogLevel slog.Level = slog.LevelInfo
logLevelEnvVar = "VN_LOG_LEVEL"
)
func (a *Application) initializeLogger() {
logLevel := defaultLogLevel
logLevelAsString, found := os.LookupEnv(logLevelEnvVar)
if found {
switch strings.ToLower(logLevelAsString) {
case "debug":
logLevel = slog.LevelDebug
case "info":
logLevel = slog.LevelInfo
case "warn":
logLevel = slog.LevelWarn
case "error":
logLevel = slog.LevelError
}
}
slog.Warn("Setting log level.", "level", logLevel.String())
a.baseLogger = slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
AddSource: true,
Level: logLevel,
}))
a.appLogger = a.baseLogger.With("module", "application")
}
// NewLogger creates new sub-instance of base logger and adds some additional data to it for persistent output.
func (a *Application) NewLogger(withs ...any) *slog.Logger {
return a.baseLogger.With(withs...)
}
+26
View File
@@ -0,0 +1,26 @@
package application
import (
"os"
"os/signal"
"syscall"
)
// ShutdownChan returns shutdown channel for main function.
func (a *Application) ShutdownChan() chan struct{} {
return a.shutdownChan
}
func (a *Application) startServer() {
a.shutdownChan = make(chan struct{})
go func() {
listener := make(chan os.Signal, 1)
signal.Notify(listener, syscall.SIGTERM, os.Interrupt)
<-listener
a.shutdownChan <- struct{}{}
}()
}
+80
View File
@@ -0,0 +1,80 @@
package application
import (
"errors"
"fmt"
)
var (
// ErrServiceAlreadyRegistered returns if trying to register a service with name already taken by other service.
ErrServiceAlreadyRegistered = errors.New("service with such name already registered")
// ErrServiceNotFound returns if trying to gather service with unknown name.
ErrServiceNotFound = errors.New("service with such name wasn't found")
)
// Service is an interface every service should conform to. Specific services will have own interface for
// cross-service interation.
type Service interface {
// Configure configures service. Called after ConnectDependencies and before LaunchStartupTasks.
Configure() error
// ConnectDependencies gets necessary dependencies.
ConnectDependencies() error
// Initialize initializes service's internal state. Called while registering service with Application
// lifecycle controller.
Initialize() error
// Name returns service name.
Name() string
// LaunchStartupTasks launches tasks on application start. Called after ConnectDependencies and Configure.
LaunchStartupTasks() error
// Shutdown stops service.
Shutdown() error
}
// RegisterService registering service with lifecycle controller for later use in any other service.
func (a *Application) RegisterService(srv Service) error {
var found bool
for _, knownService := range a.services {
if srv.Name() == knownService.Name() {
found = true
break
}
}
if found {
return fmt.Errorf(
"%w: RegisterService: check for service '%s' registration: %w",
errApplication,
srv.Name(),
ErrServiceAlreadyRegistered,
)
}
if err := srv.Initialize(); err != nil {
return fmt.Errorf("%w: RegisterService: initialize service '%s': %w", errApplication, srv.Name(), err)
}
a.services = append(a.services, srv)
return nil
}
// Service returns requested service.
func (a *Application) Service(name string) Service {
var srv Service
for _, knownService := range a.services {
if knownService.Name() == name {
srv = knownService
break
}
}
if srv == nil {
return nil
}
return srv
}
+14
View File
@@ -0,0 +1,14 @@
package commons
var (
// Branch это ветка, из которой собрано приложение.
Branch string
// Build это порядковый номер сборки.
Build string
// BuildDate это таймштамп сборки приложения.
BuildDate string
// Commit это коммит, из которого собрано приложение.
Commit string
// Version это версия, из которой собрано приложение.
Version string
)
+23
View File
@@ -0,0 +1,23 @@
package core
import (
"errors"
"net/http"
)
// ServiceNameHTTPServer is a name for HTTP server service.
const ServiceNameHTTPServer = "core/http_server"
// ErrHTTPServerIsInvalid appears when HTTP server service implementation is invalid.
var ErrHTTPServerIsInvalid = errors.New("HTTP server service implementation is invalid")
// HTTPServer is an interface for HTTP server service.
type HTTPServer interface {
// RegisterHandler registers HTTP handler.
RegisterHandler(method, path string, handler http.HandlerFunc)
// RegisterMiddleware registers HTTP server middlewares.
RegisterMiddleware(middleware HTTPMiddlewareFunc)
}
// HTTPMiddlewareFunc is a function that acts as middleware for HTTP requests.
type HTTPMiddlewareFunc func(fn http.HandlerFunc) http.HandlerFunc
@@ -0,0 +1,8 @@
package httpserver
import "net/http"
func (h *httpServer) defaultHandler(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte("Unknown path."))
}
@@ -0,0 +1,84 @@
package httpserver
import (
"errors"
"fmt"
"log/slog"
"net/http"
"go.dev.pztrn.name/vikunja-notifier/internal/application"
"go.dev.pztrn.name/vikunja-notifier/internal/services/core"
)
var (
_ = core.HTTPServer(&httpServer{})
errHTTPServer = errors.New("HTTP server core service")
)
type httpServer struct {
app *application.Application
logger *slog.Logger
httpSrv *http.Server
httpMux *http.ServeMux
middlewares []core.HTTPMiddlewareFunc
}
// Initialize initializes service.
func Initialize(app *application.Application) error {
httpSrv := &httpServer{
app: app,
}
if err := app.RegisterService(httpSrv); err != nil {
return fmt.Errorf("%w: %w", errHTTPServer, err)
}
return nil
}
func (h *httpServer) Configure() error {
h.logger.Debug("Configuring service...")
if err := h.configureHTTPServer(); err != nil {
return fmt.Errorf("configure: %w", err)
}
return nil
}
func (h *httpServer) ConnectDependencies() error {
return nil
}
func (h *httpServer) Initialize() error {
h.logger = h.app.NewLogger("service", core.ServiceNameHTTPServer)
h.logger.Info("Initializing...")
h.middlewares = make([]core.HTTPMiddlewareFunc, 0)
h.RegisterMiddleware(h.requestLoggingMiddleware)
return nil
}
func (h *httpServer) Name() string {
return core.ServiceNameHTTPServer
}
func (h *httpServer) LaunchStartupTasks() error {
h.logger.Debug("Launching startup tasks...")
go h.startHTTPServer()
return nil
}
func (h *httpServer) Shutdown() error {
if err := h.stopHTTPServer(); err != nil {
return fmt.Errorf("%w: Shutdown: %w", errHTTPServer, err)
}
return nil
}
@@ -0,0 +1,24 @@
package httpserver
import (
"fmt"
"net/http"
"time"
)
func (h *httpServer) requestLoggingMiddleware(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
startTime := time.Now()
fn(w, r)
h.logger.Info(
"HTTP request.",
"remote_addr", r.RemoteAddr,
"user_agent", r.UserAgent(),
"host", r.Host,
"path", fmt.Sprintf("%s %s", r.Method, r.RequestURI),
"duration", time.Since(startTime),
)
}
}
@@ -0,0 +1,81 @@
package httpserver
import (
"errors"
"fmt"
"net"
"net/http"
"os"
"time"
"go.dev.pztrn.name/vikunja-notifier/internal/services/core"
)
const httpServerAddrEnvVar = "VN_HTTP_ADDRESS"
var (
errHTTPServerAddrInvalid = errors.New("VN_HTTP_ADDRESS environment variable contains invalid address to " +
"listen, should be 'host:port'")
errHTTPServerAddrNotFound = errors.New("VN_HTTP_ADDRESS environment variable empty")
)
func (h *httpServer) configureHTTPServer() error {
httpSrvAddr, found := os.LookupEnv(httpServerAddrEnvVar)
if !found {
return fmt.Errorf("configure HTTP server: get address from environment variable: %w", errHTTPServerAddrNotFound)
}
host, port, err := net.SplitHostPort(httpSrvAddr)
if err != nil {
return fmt.Errorf("configure HTTP server: validate HTTP server address: %w", err)
}
if httpSrvAddr != host+":"+port {
return fmt.Errorf("configure HTTP server: validate HTTP server address: %w", errHTTPServerAddrInvalid)
}
h.httpMux = new(http.ServeMux)
// Default catch-all handler.
h.RegisterHandler("", "/", h.defaultHandler)
h.httpSrv = &http.Server{
Addr: httpSrvAddr,
Handler: h.httpMux,
ReadHeaderTimeout: time.Second * 3,
}
return nil
}
func (h *httpServer) RegisterHandler(method, path string, handler http.HandlerFunc) {
h.httpMux.HandleFunc(fmt.Sprintf("%s %s", method, path), func(w http.ResponseWriter, r *http.Request) {
for i := len(h.middlewares) - 1; i >= 0; i-- {
handler = h.middlewares[i](handler)
}
handler(w, r)
})
}
func (h *httpServer) RegisterMiddleware(middleware core.HTTPMiddlewareFunc) {
h.middlewares = append(h.middlewares, middleware)
}
func (h *httpServer) startHTTPServer() {
h.logger.Info("Starting listening for HTTP requests.", "address", h.httpSrv.Addr)
if err := h.httpSrv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
h.logger.Warn("Error when listening to ", "error", err.Error())
}
}
func (h *httpServer) stopHTTPServer() error {
h.logger.Info("Stopping HTTP server...")
if err := h.httpSrv.Shutdown(h.app.ContextWithTimeout(time.Second * 3)); err != nil {
return fmt.Errorf("stopping HTTP server: %w", err)
}
return nil
}