micw/server/server.go
2024-11-24 15:04:52 +01:00

52 lines
950 B
Go

package server
import (
"context"
"github.com/jackc/pgx/v5/pgxpool"
"mic-wallet/server/config"
"mic-wallet/server/core"
"mic-wallet/server/repository"
"net"
"net/http"
)
type serverTask func() error
type Server struct {
Cfg *config.Processed
DB *pgxpool.Pool
Repo *repository.Repository
BlocksCommiter *core.BlocksCommiter
BlocksCreator *core.BlocksCreator
HttpListener net.Listener
HttpsListener net.Listener
GrpcListener net.Listener
Mux *http.ServeMux
ErrChan chan error
}
func New(cfg *config.Processed) *Server {
return &Server{
Cfg: cfg,
ErrChan: make(chan error),
}
}
func (s *Server) execTasks(ctx context.Context, tasks []serverTask) error {
for _, task := range tasks {
go func() {
s.ErrChan <- task()
}()
}
for {
select {
case err := <-s.ErrChan:
if err != nil {
return err
}
case <-ctx.Done():
return ctx.Err()
}
}
}