Initial commit.

This commit is contained in:
2024-10-12 13:04:09 +05:00
commit 57937a5845
30 changed files with 816 additions and 0 deletions

18
server/Dockerfile.CMS Normal file
View File

@@ -0,0 +1,18 @@
FROM code.pztrn.name/containers/go-toolbox:v4 AS build
WORKDIR /featurer
COPY . /featurer
RUN --mount=type=cache,target="$GOCACHE" task server:cmd:cms:build
FROM debian:12.6-slim
RUN apt-get update && \
apt-get install -y ca-certificates iputils-ping && \
rm -rf /var/lib/apt/* /var/cache/apt/*
COPY --from=build /featurer/_build/featurer-cms /featurer-cms
COPY --from=build /usr/local/bin/dlv /dlv
COPY server/entrypoint-cms.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]

View File

@@ -0,0 +1,18 @@
FROM code.pztrn.name/containers/go-toolbox:v4 AS build
WORKDIR /featurer
COPY . /featurer
RUN --mount=type=cache,target="$GOCACHE" task server:cmd:featurer:build
FROM debian:12.6-slim
RUN apt-get update && \
apt-get install -y ca-certificates iputils-ping && \
rm -rf /var/lib/apt/* /var/cache/apt/*
COPY --from=build /featurer/_build/featurer /featurer
COPY --from=build /usr/local/bin/dlv /dlv
COPY server/entrypoint-featurer.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]

6
server/Taskfile.yml Normal file
View File

@@ -0,0 +1,6 @@
---
version: "3"
includes:
cmd: ./cmd
localdev: ./localdev

6
server/cmd/Taskfile.yml Normal file
View File

@@ -0,0 +1,6 @@
---
version: "3"
includes:
cms: ./cms
featurer: ./featurer

View File

@@ -0,0 +1,21 @@
---
version: "3"
tasks:
build:
desc: "Builds Featurer's CMS binary."
cmds:
- task: cleanup
- go build -ldflags="{{ .LDFLAGS }}" -tags netgo -o _build/featurer-cms{{exeExt}} ./server/cmd/cms/main.go
sources:
- ./**/*.go
- ./Taskfile.yml
- ./go.mod
generates:
- ./_build/featurer-cms{{exeExt}}
method: none
cleanup:
desc: "Deletes Featurer's CMS binary from local build cache."
cmds:
- rm -f _build/featurer-cms{{exeExt}}

42
server/cmd/cms/main.go Normal file
View File

@@ -0,0 +1,42 @@
package main
import (
"log/slog"
"go.dev.pztrn.name/featurer/server/internal/application"
"go.dev.pztrn.name/featurer/server/internal/services/core/datastore"
)
func main() {
_ = slog.SetLogLoggerLevel(slog.LevelDebug)
app := application.New()
slog.Info(
"Launching Featurer's CMS...",
"version", application.Version,
"build", application.Build,
"branch", application.Branch,
"commit", application.Commit,
"build date", application.BuildDate,
)
// Initializing core services first.
checkError(datastore.Initialize(app))
// Then - features services.
// Start application.
checkError(app.Start())
<-app.GetShutdownDoneChannel()
slog.Info("Featurer's CMS.")
}
func checkError(err error) {
if err == nil {
return
}
panic(err)
}

View File

@@ -0,0 +1,21 @@
---
version: "3"
tasks:
build:
desc: "Builds Featurer main binary."
cmds:
- task: cleanup
- go build -ldflags="{{ .LDFLAGS }}" -tags netgo -o _build/featurer{{exeExt}} ./server/cmd/featurer/main.go
sources:
- ./**/*.go
- ./Taskfile.yml
- ./go.mod
generates:
- ./_build/featurer{{exeExt}}
method: none
cleanup:
desc: "Deletes Featurer main binary from local build cache."
cmds:
- rm -f _build/featurer{{exeExt}}

View File

@@ -0,0 +1,42 @@
package main
import (
"log/slog"
"go.dev.pztrn.name/featurer/server/internal/application"
"go.dev.pztrn.name/featurer/server/internal/services/core/datastore"
)
func main() {
_ = slog.SetLogLoggerLevel(slog.LevelDebug)
app := application.New()
slog.Info(
"Launching Featurer server...",
"version", application.Version,
"build", application.Build,
"branch", application.Branch,
"commit", application.Commit,
"build date", application.BuildDate,
)
// Initializing core services first.
checkError(datastore.Initialize(app))
// Then - features services.
// Start application.
checkError(app.Start())
<-app.GetShutdownDoneChannel()
slog.Info("Featurer stopped.")
}
func checkError(err error) {
if err == nil {
return
}
panic(err)
}

8
server/entrypoint-cms.sh Executable file
View File

@@ -0,0 +1,8 @@
#!/bin/sh
echo "* Starting Featurer's CMS..."
if [ -n "${FEATURER_DEBUG}" ]; then
exec /dlv --listen=:4000 --headless=true --log=true --accept-multiclient --api-version=2 exec /featurer-cms --continue
else
exec /featurer-cms
fi

8
server/entrypoint-featurer.sh Executable file
View File

@@ -0,0 +1,8 @@
#!/bin/sh
echo "* Starting Featurer..."
if [ -n "${FEATURER_DEBUG}" ]; then
exec /dlv --listen=:4000 --headless=true --log=true --accept-multiclient --api-version=2 exec /featurer --continue
else
exec /featurer
fi

View File

@@ -0,0 +1,114 @@
package application
import (
"context"
"errors"
"fmt"
"log/slog"
"os"
"os/signal"
"sync"
"syscall"
)
var (
// ErrApplication indicates that error appeared somewhere in application's lifecycle controller.
ErrApplication = errors.New("application")
// Version is application's version.
Version string
// Branch is a branch name from which application was built.
Branch string
// Commit is a commit hash from which application was built.
Commit string
// Build is a build number.
Build string
// BuildDate is a date on which application was built.
BuildDate string
)
// Application is an application's lifecycle controlling structure.
type Application struct {
ctx context.Context
shutdownDone chan struct{}
cancelFunc context.CancelFunc
services map[string]Service
dataPath string
servicesMutex sync.RWMutex
}
// New creates new application's lifecycle controlling structure.
func New() *Application {
appl := &Application{}
appl.initialize()
return appl
}
// GetContext returns application's global context.
func (a *Application) GetContext() context.Context {
return a.ctx
}
// GetShutdownDoneChannel returns channel on which lifecycle controller will tell about shutdown completion.
// Should be used only in main()!
func (a *Application) GetShutdownDoneChannel() chan struct{} {
slog.Debug("Returning shutdown completion channel.")
return a.shutdownDone
}
func (a *Application) initialize() {
a.ctx, a.cancelFunc = context.WithCancel(context.Background())
a.ctx, a.cancelFunc = signal.NotifyContext(a.ctx, os.Interrupt, syscall.SIGTERM)
a.services = make(map[string]Service)
a.shutdownDone = make(chan struct{}, 1)
}
// Shutdown stops application and all registered services.
func (a *Application) Shutdown() error {
a.cancelFunc()
a.servicesMutex.RLock()
defer a.servicesMutex.RUnlock()
for _, service := range a.services {
if err := service.Shutdown(); err != nil {
slog.Error("Error appeared when trying to shut down service", "service", service.GetName(), "error", err.Error())
}
}
a.shutdownDone <- struct{}{}
return nil
}
// Start starts application and all registered services.
func (a *Application) Start() error {
go a.signalsListener()
a.servicesMutex.RLock()
defer a.servicesMutex.RUnlock()
// First pass - connecting dependencies.
for _, service := range a.services {
if err := service.ConnectDependencies(); err != nil {
return fmt.Errorf("%w: connecting dependencies for service '%s': %w", ErrApplication, service.GetName(), err)
}
}
// Second pass - launching startup tasks.
for name, service := range a.services {
slog.Debug("Launching startup tasks for service", "service", name)
if err := service.LaunchStartupTasks(); err != nil {
return fmt.Errorf("%w: launching startup tasks for '%s': %w", ErrApplication, service.GetName(), err)
}
}
slog.Debug("Application started.")
return nil
}

View File

@@ -0,0 +1,54 @@
package application
import (
"errors"
"fmt"
)
var (
// ErrServiceAlreadyRegistered appears when trying to register a service with already taken name.
ErrServiceAlreadyRegistered = errors.New("service already registered")
// ErrServiceNotRegistered appears when trying to get a registered service with unknown name.
ErrServiceNotRegistered = errors.New("unknown service")
)
// Service это интерфейс, которому должны соответствовать все сервисы, используемые в приложении.
type Service interface {
ConnectDependencies() error
GetName() string
Initialize() error
LaunchStartupTasks() error
Shutdown() error
}
// GetService возвращает сервис по имени, или же ошибку, если сервис не был зарегистрирован ранее.
func (a *Application) GetService(name string) (Service, error) {
a.servicesMutex.RLock()
service, found := a.services[name]
a.servicesMutex.RUnlock()
if !found {
return nil, fmt.Errorf("%w: get service '%s': %w", ErrApplication, name, ErrServiceNotRegistered)
}
return service, nil
}
// RegisterService регистрирует сервис, или возвращает ошибку, если сервис с таким именем уже был
// зарегистрирован ранее.
func (a *Application) RegisterService(service Service) error {
_, found := a.services[service.GetName()]
if found {
return fmt.Errorf("%w: register service '%s': %w", ErrApplication, service.GetName(), ErrServiceAlreadyRegistered)
}
if err := service.Initialize(); err != nil {
return fmt.Errorf("%w: initializing service '%s': %w", ErrApplication, service.GetName(), err)
}
a.servicesMutex.Lock()
a.services[service.GetName()] = service
a.servicesMutex.Unlock()
return nil
}

View File

@@ -0,0 +1,24 @@
package application
import (
"log/slog"
"os"
"os/signal"
"syscall"
)
func (a *Application) signalsListener() {
slog.Info("Starting listening for SIGTERM signal...")
listener := make(chan os.Signal, 1)
signal.Notify(listener, syscall.SIGTERM, os.Interrupt)
<-listener
slog.Info("Got SIGTERM, stopping Featurer...")
if err := a.Shutdown(); err != nil {
slog.Error("Something went wrong when trying to shutdown application", "error", err.Error())
slog.Error("!!! APPLICATION CANNOT BE STOPPED NORMALLY, KILL IT MANUALLY !!!")
}
}

View File

@@ -0,0 +1,16 @@
package core
import "errors"
// ServiceNameDatastore is a service name for data storage implementation.
const ServiceNameDatastore = "datastore"
var (
// ErrDatastore indicates that error appeared somewhere in data storage service.
ErrDatastore = errors.New("datastore")
// ErrDatastoreServiceIsInvalid appears when data storage implementation isn't conforming to interface.
ErrDatastoreServiceIsInvalid = errors.New("invalid datastore implementation")
)
// DataStore is an interface for data storage implementations.
type DataStore interface{}

View File

@@ -0,0 +1,48 @@
package datastore
import (
"fmt"
"log/slog"
"go.dev.pztrn.name/featurer/server/internal/application"
"go.dev.pztrn.name/featurer/server/internal/services/core"
)
type datastore struct {
app *application.Application
}
// Initialize initializes service.
func Initialize(app *application.Application) error {
plan := &datastore{
app: app,
}
if err := app.RegisterService(plan); err != nil {
return fmt.Errorf("%w: %w", core.ErrDatastore, err)
}
return nil
}
func (p *datastore) ConnectDependencies() error {
return nil
}
func (p *datastore) GetName() string {
return core.ServiceNameDatastore
}
func (p *datastore) Initialize() error {
slog.Info("Initializing data storage...")
return nil
}
func (p *datastore) LaunchStartupTasks() error {
return nil
}
func (p *datastore) Shutdown() error {
return nil
}

View File

@@ -0,0 +1,19 @@
---
version: 3
includes:
common: ./common
featurer: ./featurer
tasks:
down:
desc: "Removes development environment."
cmds:
- task: featurer:down
- task: common:network-down
up:
desc: "Creates development environment."
cmds:
- task: common:network-up
- task: featurer:up

View File

@@ -0,0 +1,15 @@
---
version: "3"
tasks:
network-down:
desc: "Deletes Docker network for development."
dir: "./server/localdev/common"
cmds:
- docker compose -p featurer-network -f network.yaml down
network-up:
desc: "Creates Docker network for development"
dir: "./server/localdev/common"
cmds:
- docker compose -p featurer-network -f network.yaml up -d

View File

@@ -0,0 +1,18 @@
---
services:
dummy:
image: busybox
container_name: featurer-dummy
hostname: dummy
networks:
featurer:
ipv4_address: 248.248.0.254
networks:
featurer:
driver: bridge
name: "featurer"
ipam:
config:
- subnet: 248.248.0.0/24
gateway: 248.248.0.1

View File

@@ -0,0 +1,47 @@
---
version: "3"
tasks:
build:
desc: "Builds Featurer's container."
dir: "./server/localdev/featurer"
cmds:
- docker compose -p featurer -f docker-compose.yaml build
down:
desc: "Deletes all Featurer's data (down)."
dir: "./server/localdev/featurer"
cmds:
- docker compose -p featurer -f docker-compose.yaml down --volumes
logs:
desc: "Show Featurer logs."
dir: "./server/localdev/featurer"
cmds:
- docker compose -p featurer -f docker-compose.yaml logs -f
restart:
desc: "Restart Featurer."
dir: "./server/localdev/featurer"
cmds:
- docker compose -p featurer -f docker-compose.yaml restart
start:
desc: "Start Featurer."
dir: "./server/localdev/featurer"
cmds:
- docker compose -p featurer -f docker-compose.yaml start
stop:
desc: "Stop Featurer without deleting it's data."
dir: "./server/localdev/featurer"
cmds:
- docker compose -p featurer -f docker-compose.yaml stop
up:
desc: "Start Featurer (up -d)."
dir: "./server/localdev/featurer"
cmds:
- task: :common:network-up
- task: build
- docker compose -p featurer -f docker-compose.yaml up -d

View File

@@ -0,0 +1,16 @@
---
services:
featurer:
container_name: "featurer"
build:
context: ../../../
dockerfile: server/Dockerfile.featurer
networks:
featurer:
ipv4_address: 248.248.0.2
cap_add:
- SYS_PTRACE
networks:
featurer:
external: true