refactor: move DWN proxy server logic to separate file

This commit is contained in:
Prad Nukala
2024-09-25 20:27:00 -04:00
parent 0df3762f13
commit de05777261
8 changed files with 128 additions and 68 deletions
+7 -39
View File
@@ -1,15 +1,8 @@
package proxy
import (
"context"
"net/http"
"os"
"os/signal"
"time"
"log"
"github.com/labstack/echo/v4"
"github.com/labstack/gommon/log"
"github.com/onsonr/sonr/nebula/pages"
"github.com/spf13/cobra"
)
@@ -17,40 +10,15 @@ func NewProxyCmd() *cobra.Command {
return &cobra.Command{
Use: "dwn-proxy",
Short: "Starts the DWN proxy server for the local IPFS node",
Run: func(cmd *cobra.Command, args []string) {
// Echo instance
e := echo.New()
e.Logger.SetLevel(log.INFO)
RunE: func(cmd *cobra.Command, args []string) error {
// Load config
_, err := LoadConfig("./")
c, err := LoadConfig(".")
if err != nil {
e.Logger.Error(err)
}
// Configure the server
e.GET("/", pages.Home)
e.GET("/allocate", pages.Profile)
// Start server
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
// Start server
go func() {
if err := e.Start(":1323"); err != nil && err != http.ErrServerClosed {
e.Logger.Fatal("shutting down the server")
}
}()
// Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds.
<-ctx.Done()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Shutdown the server with 10 seconds timeout.
if err := e.Shutdown(ctx); err != nil {
e.Logger.Fatal(err)
return err
}
log.Printf("Config: %+v", c)
startServer()
return nil
},
}
}
+2 -5
View File
@@ -3,11 +3,8 @@ package proxy
import "github.com/spf13/viper"
type Config struct {
Host string `mapstructure:"HOST"`
Port string `mapstructure:"PORT"`
SSL bool `mapstructure:"SSL"`
CertFile string `mapstructure:"CERT_FILE"`
KeyFile string `mapstructure:"KEY_FILE"`
Host string `mapstructure:"HOST"`
Port string `mapstructure:"PORT"`
}
func (c *Config) GetHostname() string {
+43
View File
@@ -0,0 +1,43 @@
package proxy
import (
"context"
"net/http"
"os"
"os/signal"
"time"
"github.com/labstack/echo/v4"
"github.com/labstack/gommon/log"
"github.com/onsonr/sonr/nebula/pages"
)
func startServer() {
// Echo instance
e := echo.New()
e.Logger.SetLevel(log.INFO)
// Configure the server
e.GET("/", pages.Home)
e.GET("/allocate", pages.Profile)
// Start server
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
// Start server
go func() {
if err := e.Start(":1323"); err != nil && err != http.ErrServerClosed {
e.Logger.Fatal("shutting down the server")
}
}()
// Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds.
<-ctx.Done()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Shutdown the server with 10 seconds timeout.
if err := e.Shutdown(ctx); err != nil {
e.Logger.Fatal(err)
}
}