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

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
},
}
}

View File

@ -5,9 +5,6 @@ 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"`
}
func (c *Config) GetHostname() string {

43
app/proxy/server.go Normal file
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)
}
}

View File

@ -1,3 +1,49 @@
# Nebula
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)

0
nebula/assets/js/htmx.min.js vendored Normal file
View File

View File

@ -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"
]
}
}
}

View File

@ -1 +1,31 @@
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.