polycule-connect/polyculeconnect/server/server.go
Melora Hugues ce8bd4ee7c
Some checks failed
ci/woodpecker/push/test Pipeline failed
ci/woodpecker/push/deploy unknown status
feat: start replacing dex with zitadel (#48)
Start the process of replacing dex with zitadel, this commit is
absolutely not prod-ready, basically we just added zitatel, and the
necessary elements to make it work to at least getting a client from the
DB

- replace logrus with zap
- start our own storage for the users
- instanciate zitaled on start
- allow getting client using the ID from the DB
2024-08-15 18:25:15 +02:00

111 lines
2.9 KiB
Go

package server
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"os"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/config"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/controller/ui"
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/middlewares"
"github.com/zitadel/oidc/v3/pkg/op"
"go.uber.org/zap"
)
type Server struct {
ctx context.Context
cancel context.CancelFunc
httpSrv *http.Server
listener net.Listener
serverMode config.ListeningMode
address string
handler *http.ServeMux
controllers map[string]http.Handler
l *zap.SugaredLogger
}
func newUnixListener(sockPath string) (net.Listener, error) {
if err := os.Remove(sockPath); err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("failed to cleanup previously existing socket: %w", err)
}
sock, err := net.Listen("unix", sockPath)
if err != nil {
return nil, fmt.Errorf("failed to create unix socket: %w", err)
}
if err := os.Chmod(sockPath, 0o777); err != nil {
return nil, fmt.Errorf("failed to set permissions to unix socket: %w", err)
}
return sock, nil
}
func New(appConf *config.AppConfig, oidcHandler *op.Provider, logger *zap.SugaredLogger) (*Server, error) {
var listener net.Listener
var addr string
var err error
switch appConf.ServerMode {
case config.ModeNet:
addr = fmt.Sprintf("%s:%d", appConf.Host, appConf.Port)
listener, err = net.Listen("tcp", addr)
if err != nil {
return nil, fmt.Errorf("failed to init server in net mode: %w", err)
}
case config.ModeUnix:
addr = appConf.SockPath
listener, err = newUnixListener(appConf.SockPath)
if err != nil {
return nil, fmt.Errorf("failed to init server in unix mode: %w", err)
}
default:
panic(fmt.Errorf("unexpected listening mode %v", appConf.ServerMode))
}
controllers := map[string]http.Handler{
ui.StaticRoute: middlewares.WithLogger(ui.NewStaticController(appConf.StaticDir), logger),
"/": middlewares.WithLogger(ui.NewIndexController(logger, oidcHandler, appConf.StaticDir), logger),
}
m := http.NewServeMux()
return &Server{
handler: m,
httpSrv: &http.Server{
Handler: m,
},
listener: listener,
l: logger,
serverMode: appConf.ServerMode,
address: addr,
controllers: controllers,
ctx: context.TODO(),
}, nil
}
func (s *Server) initMux() {
for r, c := range s.controllers {
s.handler.Handle(r, c)
}
}
func (s *Server) Run(ctx context.Context) {
s.ctx, s.cancel = context.WithCancel(ctx)
s.initMux()
switch s.serverMode {
case config.ModeNet:
s.l.Infof("Server listening on host %q", s.address)
case config.ModeUnix:
s.l.Infof("Server listening on unix socket %q", s.address)
default:
}
if err := s.httpSrv.Serve(s.listener); err != nil {
s.l.Errorf("failed to serve HTTP server: %s", err.Error())
}
s.cancel()
}
func (s *Server) Done() <-chan struct{} {
return s.ctx.Done()
}