mirror of
https://github.com/sonr-io/sonr.git
synced 2026-08-02 17:31:39 +00:00
refactor: move DWN proxy server logic to separate file
This commit is contained in:
+7
-39
@@ -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
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user