Skip to content

Commit

Permalink
portrd option to choose b/w admin, tunnel or both servers to run
Browse files Browse the repository at this point in the history
  • Loading branch information
amalshaji committed Feb 13, 2024
1 parent 99d4b3f commit 5e1774b
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 77 deletions.
2 changes: 1 addition & 1 deletion .air.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ tmp_dir = "tmp"

[build]
args_bin = []
bin = "./tmp/main start -c configs/server.yaml"
bin = "./tmp/main -c configs/server.yaml start all"
cmd = "go build -o ./tmp/main cmd/portrd/main.go"
delay = 1000
exclude_dir = [
Expand Down
88 changes: 64 additions & 24 deletions cmd/portrd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"log"
"os"
"os/signal"
"syscall"

"github.com/amalshaji/portr/internal/server/admin"
"github.com/amalshaji/portr/internal/server/admin/service"
Expand All @@ -19,39 +21,61 @@ import (

const VERSION = "0.0.1-beta"

type ServiceToRun int

const (
ADMIN_SERVER ServiceToRun = iota + 1
TUNNEL_SERVER = iota + 1
ALL_SERVERS = iota + 1
)

func main() {
app := &cli.App{
Name: "portrd",
Usage: "portr server",
Version: VERSION,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "config file",
Value: "config.yaml",
},
},
Commands: []*cli.Command{
{
Name: "start",
Usage: "Start the portr server",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "config file",
Value: "config.yaml",
Usage: "Specify the server to start",
Subcommands: []*cli.Command{
{
Name: "admin",
Usage: "Start the admin server",
Action: func(c *cli.Context) error {
start(c.String("config"), ADMIN_SERVER)
return nil
},
},
{
Name: "tunnel",
Usage: "Start the tunnel server",
Action: func(c *cli.Context) error {
start(c.String("config"), TUNNEL_SERVER)
return nil
},
},
{
Name: "all",
Usage: "Start both admin and tunnel servers",
Action: func(c *cli.Context) error {
start(c.String("config"), ALL_SERVERS)
return nil
},
},
},
Action: func(c *cli.Context) error {
start(c.String("config"))
return nil
},
},
{
Name: "migrate",
Usage: "Run database migrations",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "config file",
Value: "config.yaml",
},
},
Action: func(c *cli.Context) error {
migrate(c.String("config"))
return nil
Expand All @@ -65,7 +89,7 @@ func main() {
}
}

func start(configFilePath string) {
func start(configFilePath string, toRun ServiceToRun) {
config, err := config.Load(configFilePath)
if err != nil {
log.Fatal(err)
Expand All @@ -91,10 +115,26 @@ func start(configFilePath string) {
adminServer := admin.New(config, service)
cron := cron.New(_db, config)

go proxyServer.Start()
go sshServer.Start()
go cron.Start()
adminServer.Start()
if toRun == TUNNEL_SERVER || toRun == ALL_SERVERS {
go proxyServer.Start()
defer proxyServer.Shutdown(context.TODO())

go sshServer.Start()
defer sshServer.Shutdown(context.TODO())
}

if toRun == ADMIN_SERVER || toRun == ALL_SERVERS {
go adminServer.Start()
defer adminServer.Shutdown()

go cron.Start()
defer cron.Shutdown()
}

done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

<-done
}

func migrate(configFilePath string) {
Expand Down
4 changes: 2 additions & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ COPY --from=frontend-builder /app/dist/ ./internal/server/admin/web/dist/
VOLUME [ "/app/configs" ]
VOLUME [ "/app/data" ]

ENTRYPOINT ["./portrd", "start"]
CMD [ "--config", "/app/config.yaml" ]
ENTRYPOINT ["./portrd", "--config", "/app/config.yaml"]

1 change: 1 addition & 0 deletions docker/docker-compose.dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ services:
build:
context: ..
dockerfile: docker/Dockerfile
command: ["start", "all"]
volumes:
- ../configs/server.yaml:/app/config.yaml
- ../data:/app/data/
Expand Down
1 change: 1 addition & 0 deletions docker/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ services:
# use an image
context: .
dockerfile: docker/Dockerfile
command: ["start", "all"]
volumes:
- ./server.yaml:/app/config.yaml
- ./data:/app/data/
Expand Down
22 changes: 9 additions & 13 deletions internal/server/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import (
"context"
"errors"
"fmt"
"log"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"github.com/amalshaji/portr/internal/server/admin/handler"
Expand Down Expand Up @@ -141,20 +139,18 @@ func New(config *config.Config, service *service.Service) *AdminServer {
func (s *AdminServer) Start() {
s.log.Info("starting admin server", "port", s.config.ListenAddress())

done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

go func() {
if err := s.app.Listen(s.config.ListenAddress()); err != nil && !errors.Is(err, http.ErrServerClosed) {
s.log.Error("failed to start admin server", "error", err)
done <- nil
}
}()
if err := s.app.Listen(s.config.ListenAddress()); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("failed to start admin server: %v", err)
}
}

<-done
func (s *AdminServer) Shutdown() {
s.log.Info("stopping admin server")

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)

defer func() { cancel() }()

if err := s.app.ShutdownWithContext(ctx); err != nil {
s.log.Error("failed to stop proxy server", "error", err)
}
Expand Down
19 changes: 8 additions & 11 deletions internal/server/cron/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"fmt"
"log/slog"
"os"
"os/signal"
"time"

"github.com/amalshaji/portr/internal/server/config"
Expand All @@ -14,9 +12,10 @@ import (
)

type Cron struct {
db *db.Db
logger *slog.Logger
config *config.Config
db *db.Db
logger *slog.Logger
config *config.Config
cancelFunc context.CancelFunc
}

func New(db *db.Db, config *config.Config) *Cron {
Expand All @@ -25,6 +24,7 @@ func New(db *db.Db, config *config.Config) *Cron {

func (c *Cron) Start() {
ctx, cancel := context.WithCancel(context.Background())
c.cancelFunc = cancel

c.logger.Info(fmt.Sprintf("Starting %d cron jobs", len(crons)))
for _, job := range crons {
Expand All @@ -42,11 +42,8 @@ func (c *Cron) Start() {
}
}(job)
}
}

sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt)

<-sigCh

cancel()
func (c *Cron) Shutdown() {
c.cancelFunc()
}
25 changes: 11 additions & 14 deletions internal/server/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import (
"context"
"errors"
"fmt"
"log"
"log/slog"
"net/http"
"net/http/httputil"
"net/url"
"os"
"os/signal"
"sync"
"syscall"
"time"

"github.com/amalshaji/portr/internal/server/config"
Expand Down Expand Up @@ -111,21 +109,20 @@ func (p *Proxy) handleRequest(w http.ResponseWriter, r *http.Request) {
func (p *Proxy) Start() {
p.log.Info("starting proxy server", "port", p.GetServerAddr())

done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

http.HandleFunc("/", p.handleRequest)
go func() {
if err := p.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
p.log.Error("failed to start proxy server", "error", err)
done <- nil
}
}()

<-done

if err := p.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("failed to start proxy server: %v", err)
}
}

func (p *Proxy) Shutdown(_ context.Context) {
p.log.Info("stopping proxy server")

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)

defer func() { cancel() }()

if err := p.server.Shutdown(ctx); err != nil {
p.log.Error("failed to stop proxy server", "error", err)
}
Expand Down
23 changes: 11 additions & 12 deletions internal/server/ssh/sshd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"log"
"log/slog"
"os"
"os/signal"
"syscall"
"time"

"github.com/amalshaji/portr/internal/constants"
Expand All @@ -24,6 +22,7 @@ type SshServer struct {
log *slog.Logger
proxy *proxy.Proxy
service *service.Service
server *ssh.Server
}

func New(config *config.SshConfig, proxy *proxy.Proxy, service *service.Service) *SshServer {
Expand Down Expand Up @@ -117,23 +116,23 @@ func (s *SshServer) Start() {
},
}

done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
s.server = &server

s.log.Info("starting SSH server", "port", s.GetServerAddr())

go func() {
if err := server.ListenAndServe(); err != nil && !errors.Is(err, ssh.ErrServerClosed) {
s.log.Error("failed to start SSH server", "error", err)
done <- nil
}
}()
if err := server.ListenAndServe(); err != nil && !errors.Is(err, ssh.ErrServerClosed) {
log.Fatalf("failed to start SSH server: %v", err)
}
}

<-done
func (s *SshServer) Shutdown(_ context.Context) {
s.log.Info("stopping SSH server")

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)

defer func() { cancel() }()
if err := server.Shutdown(ctx); err != nil {

if err := s.server.Shutdown(ctx); err != nil {
s.log.Error("failed to stop SSH server", "error", err)
}
}

0 comments on commit 5e1774b

Please sign in to comment.