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
|
package proxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"log"
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"os/signal"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
|
||||||
"github.com/labstack/gommon/log"
|
|
||||||
"github.com/onsonr/sonr/nebula/pages"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -17,40 +10,15 @@ func NewProxyCmd() *cobra.Command {
|
|||||||
return &cobra.Command{
|
return &cobra.Command{
|
||||||
Use: "dwn-proxy",
|
Use: "dwn-proxy",
|
||||||
Short: "Starts the DWN proxy server for the local IPFS node",
|
Short: "Starts the DWN proxy server for the local IPFS node",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
// Echo instance
|
|
||||||
e := echo.New()
|
|
||||||
e.Logger.SetLevel(log.INFO)
|
|
||||||
|
|
||||||
// Load config
|
// Load config
|
||||||
_, err := LoadConfig("./")
|
c, err := LoadConfig(".")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
e.Logger.Error(err)
|
return 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)
|
|
||||||
}
|
}
|
||||||
|
log.Printf("Config: %+v", c)
|
||||||
|
startServer()
|
||||||
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-5
@@ -3,11 +3,8 @@ package proxy
|
|||||||
import "github.com/spf13/viper"
|
import "github.com/spf13/viper"
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Host string `mapstructure:"HOST"`
|
Host string `mapstructure:"HOST"`
|
||||||
Port string `mapstructure:"PORT"`
|
Port string `mapstructure:"PORT"`
|
||||||
SSL bool `mapstructure:"SSL"`
|
|
||||||
CertFile string `mapstructure:"CERT_FILE"`
|
|
||||||
KeyFile string `mapstructure:"KEY_FILE"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) GetHostname() string {
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,49 @@
|
|||||||
# Nebula
|
# Nebula
|
||||||
|
|
||||||
A Templ component library for the Sonr DWN (Decentralized Web Node) client.
|
A Templ component library for the Sonr DWN (Decentralized Web Node) client.
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
### 3rd Party
|
||||||
|
|
||||||
|
- [htmx](https://htmx.org/)
|
||||||
|
- [tailwindcss](https://tailwindcss.com/)
|
||||||
|
- [templ](https://templ.dev/)
|
||||||
|
- [alpinejs](https://alpinejs.dev/)
|
||||||
|
|
||||||
|
### Components
|
||||||
|
|
||||||
|
- Navbar
|
||||||
|
- Footer
|
||||||
|
- Login
|
||||||
|
- Register
|
||||||
|
- Profile
|
||||||
|
- Authorize
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/onsonr/sonr/nebula"
|
||||||
|
"github.com/onsonr/sonr/nebula/components"
|
||||||
|
"github.com/onsonr/sonr/nebula/pages"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
e := echo.New()
|
||||||
|
e.Use(nebula.UseAssets)
|
||||||
|
e.GET("/", pages.Home)
|
||||||
|
e.GET("/login", pages.Login)
|
||||||
|
e.GET("/register", pages.Register)
|
||||||
|
e.GET("/profile", pages.Profile)
|
||||||
|
e.GET("/authorize", pages.Authorize)
|
||||||
|
e.GET("/components", components.Home)
|
||||||
|
e.Logger.Fatal(e.Start(":1323"))
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
[MIT](LICENSE)
|
||||||
|
|||||||
Vendored
@@ -1,24 +0,0 @@
|
|||||||
{
|
|
||||||
"$schema": "https://raw.githubusercontent.com/jetify-com/devbox/0.12.0/.schema/devbox.schema.json",
|
|
||||||
"packages": [
|
|
||||||
"bun@latest"
|
|
||||||
],
|
|
||||||
"env": {
|
|
||||||
"CLOUDFLARE_API_TOKEN": "$CLOUDFLARE_API_TOKEN",
|
|
||||||
"GOPATH": "$HOME/go",
|
|
||||||
"PATH": "$HOME/go/bin:$PATH",
|
|
||||||
"TEMPL_EXPERIMENT": "rawgo",
|
|
||||||
"CHAIN_ID": "sonr-testnet-1",
|
|
||||||
"DENOM": "usnr",
|
|
||||||
"KEYRING": "test",
|
|
||||||
"MONIKER": "florence",
|
|
||||||
"ENV": "$ENVIRONMENT"
|
|
||||||
},
|
|
||||||
"shell": {
|
|
||||||
"scripts": {
|
|
||||||
"build": [
|
|
||||||
"make build"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1 +1,31 @@
|
|||||||
package nebula
|
package nebula
|
||||||
|
|
||||||
|
import (
|
||||||
|
"embed"
|
||||||
|
"io/fs"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:embed assets
|
||||||
|
var embeddedFiles embed.FS
|
||||||
|
|
||||||
|
func getFileSystem(useOS bool) http.FileSystem {
|
||||||
|
if useOS {
|
||||||
|
return http.FS(os.DirFS("assets"))
|
||||||
|
}
|
||||||
|
|
||||||
|
fsys, err := fs.Sub(embeddedFiles, "assets")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return http.FS(fsys)
|
||||||
|
}
|
||||||
|
|
||||||
|
func UseAssets(e *echo.Echo) echo.HandlerFunc {
|
||||||
|
assets := http.FileServer(getFileSystem(true))
|
||||||
|
return echo.WrapHandler(assets)
|
||||||
|
}
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user