Skip to content

Commit

Permalink
feat: moved from yaml to using of command flags services mgt
Browse files Browse the repository at this point in the history
  • Loading branch information
DeeStarks committed Aug 12, 2022
1 parent 534e036 commit d5f8684
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 154 deletions.
54 changes: 33 additions & 21 deletions app/cli/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (c *ServiceCommand) ListRunning() {
t := table.New(os.Stdout)
t.SetDividers(table.UnicodeDividers)

t.SetHeaders("NAME", "TYPE", "SERVERS", "ROOT", "REMOTE SERVER", "TUNNELLED", "CREATED")
t.SetHeaders("NAME", "TYPE", "LISTENING ON", "ROOT", "REMOTE ADDRESS", "TUNNELLED", "CREATED")
for _, p := range processes {
created_at := utils.TimeAgo(p.CreatedAt, time.Now().Unix())
listeners := strings.Join(p.Listeners, ", ")
Expand Down Expand Up @@ -83,7 +83,7 @@ func (c *ServiceCommand) ListAll() {
t := table.New(os.Stdout)
t.SetDividers(table.UnicodeDividers)

t.SetHeaders("NAME", "STATUS", "TYPE", "SERVERS", "ROOT", "REMOTE SERVER", "TUNNELLED", "CREATED")
t.SetHeaders("NAME", "STATUS", "TYPE", "LISTENING ON", "ROOT", "REMOTE ADDRESS", "TUNNELLED", "CREATED")
for _, p := range processes {
created_at := utils.TimeAgo(p.CreatedAt, time.Now().Unix())
listeners := strings.Join(p.Listeners, ", ")
Expand All @@ -108,25 +108,11 @@ func (c *ServiceCommand) ListAll() {
}

// Add new service
func (c *ServiceCommand) Add(filepath string, update bool) {
// Ensure the file exists
f, err := os.Open(filepath)
if err != nil {
fmt.Println("No file named: \"conoid.yml\"")
return
}
defer f.Close()

conf, err := utils.DeserializeConf(f)
if err != nil {
fmt.Println("Error deserializing configuration file:", err)
return
}

func (c *ServiceCommand) Add(conf utils.AppConf, update bool) {
// Vallidate configuration
validatedConf, err := utils.ValidateConf(conf)
if err != nil {
fmt.Println("Invalid configuration file:", err)
fmt.Println(err)
return
}

Expand All @@ -143,7 +129,7 @@ func (c *ServiceCommand) Add(filepath string, update bool) {
for _, p := range processes {
if p.Name == validatedConf.Name {
if !update {
fmt.Printf("A service already exists with the name \"%s\"; Use the \"--update\" flag to modify service\n", validatedConf.Name)
fmt.Printf("A service already exists with the name \"%s\"; Use \"conoid update --name %s [flags]\" to modify service\n", validatedConf.Name, validatedConf.Name)
return
}

Expand Down Expand Up @@ -197,7 +183,7 @@ func (c *ServiceCommand) Add(filepath string, update bool) {
fmt.Println(err)
return
}
fmt.Println("Your service was successfully added. Restart conoid to start accepting request")
fmt.Println("Your service was successfully added. Restart conoid to start accepting requests")
} else {
// Updating service
_, err := domainPort.ServiceProcesses().Update(serviceToUpdate, mapConf)
Expand Down Expand Up @@ -239,7 +225,7 @@ func (c *ServiceCommand) Get(name string) {
if service.Type == "static" {
t.AddRow("DOCUMENT ROOT", service.RootDirectory)
}
t.AddRow("REMOTE SERVER", service.RemoteServer)
t.AddRow("REMOTE ADDRESS", service.RemoteServer)
t.AddRow("TUNNELLED", tunnelled)
t.AddRow("CREATED", utils.TimeAgo(service.CreatedAt, time.Now().Unix()))
t.Render()
Expand Down Expand Up @@ -270,3 +256,29 @@ func (c *ServiceCommand) Start(name string) {
}
fmt.Printf("%s: service restarted. Restart conoid server to synchronize update\n", name)
}

// Stop a running servive
func (c *ServiceCommand) Stop(name string) {
// Retrieve from db
domainPort := port.NewDomainPort(c.defaultDB)
service, err := domainPort.ServiceProcesses().Get(name)
if err != nil {
fmt.Printf("No such service: \"%s\"\n", name)
return
}

// Check if service is already running
if !service.Status {
fmt.Println("Service stopped")
return
}

// Change status
_, err = domainPort.ServiceProcesses().Update(name, map[string]interface{}{
"status": 0,
})
if err != nil {
panic(err)
}
fmt.Println("Service stopped")
}
6 changes: 3 additions & 3 deletions app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (s *Server) process(conn net.Conn) {

// TODO:
// The idea for the load balancer isn't fully formed yet.
// For now, it's always going to select the first server for every connection
// For now, it's always going to select the first server
lb := tools.NewLoadBalancer(addrs)
addr := lb.GetNextServer()

Expand Down Expand Up @@ -99,10 +99,10 @@ func (s *Server) Serve() {
connsCh := make(chan int, config.MAX_CONN_COUNT)

for {
// Block if connections count it full
// Block if connections count is full
connsCh <- 1

// Establish a point-to-point connection between the client and server
// Accept connections
conn, err := listener.Accept()
if err != nil {
log.Println("Connection failed:", err)
Expand Down
7 changes: 3 additions & 4 deletions app/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
)

type (
// A map to store running apps, the key represents incoming network address,
// and the value as the server's address
// A map to store running services, the key represents local server,
// and the value as the remote server's address
RunningServices map[string][]string

Services struct {
Expand Down Expand Up @@ -108,8 +108,7 @@ func (s *Services) ServeServices(conoidHost, conoidPort string, connCh chan<- ne
"remote_server": host.FullURL(),
})
if err != nil {
log.Println("Error updating tunnel state:", err)
continue
panic(err)
}
}
}
Expand Down
15 changes: 3 additions & 12 deletions app/tools/tunnel_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tools_test

import (
"log"
"net"
"testing"
"time"
Expand All @@ -22,10 +21,9 @@ func TestTunnel(t *testing.T) {
openConns := make(chan net.Conn, len(tests)*2)

// Create main server
mSrv, err := net.Listen("tcp", "30000")
mSrv, err := net.Listen("tcp", ":30000")
if err != nil {
log.Println(err)
return
t.Error(err)
}

for _, tc := range tests {
Expand All @@ -38,17 +36,11 @@ func TestTunnel(t *testing.T) {
// Open tunnel
svr, err := net.Listen("tcp", tc.svr)
if err != nil {
log.Println(err)
break
t.Error(err)
}
h.OpenTunnel(mSrv.Addr().String(), []string{svr.Addr().String()})
}

a := true
if a {
t.Error("All conns connected")
}

L:
for {
select {
Expand All @@ -57,7 +49,6 @@ L:
if err != nil {
t.Error("Error closing connection", err)
}
log.Printf("%s stopping", conn.LocalAddr().String())
case <-time.After(time.Second * 5):
break L
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ var (
}

mltcc := 10 // Maximum localtunnel connections count
mltcc = mltcc * 2 // Incomming and outgoing connections
mltcc = mltcc * 2 // Incoming and outgoing connections
tunnelConnCount := tunnelled * mltcc
// Record all open connections
// This will be used to close all on shutdown
openConnsCh := make(chan net.Conn, config.MAX_CONN_COUNT+mltcc)
openConnsCh := make(chan net.Conn, config.MAX_CONN_COUNT+tunnelConnCount)

// Start server if no argumentss were passed or the first argument is "up"
if len(args) <= 0 || args[0] == "start" {
Expand Down Expand Up @@ -83,7 +84,7 @@ var (

versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number of Conoid",
Short: "Current conoid version",
Long: `All software has versions. This is Conoid's`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(config.VERSION)
Expand Down
Loading

0 comments on commit d5f8684

Please sign in to comment.