2024-11-23 01:28:58 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
_ "embed"
|
2024-12-02 14:27:18 -05:00
|
|
|
"fmt"
|
2024-11-23 01:28:58 -05:00
|
|
|
"net/http"
|
|
|
|
|
|
2024-12-02 14:27:18 -05:00
|
|
|
"github.com/ipfs/kubo/client/rpc"
|
2024-11-23 01:28:58 -05:00
|
|
|
"github.com/labstack/echo/v4"
|
2024-12-02 14:27:18 -05:00
|
|
|
"github.com/labstack/echo/v4/middleware"
|
|
|
|
|
"github.com/onsonr/sonr/pkg/common/session"
|
|
|
|
|
"github.com/onsonr/sonr/pkg/gateway"
|
|
|
|
|
"github.com/onsonr/sonr/pkg/webui/landing"
|
|
|
|
|
|
|
|
|
|
gatewaymiddleware "github.com/onsonr/sonr/pkg/gateway/middleware"
|
|
|
|
|
// TODO: Integrate TigerBeetle
|
|
|
|
|
// _ "github.com/tigerbeetle/tigerbeetle-go"
|
|
|
|
|
// _ "github.com/tigerbeetle/tigerbeetle-go/pkg/types"
|
2024-11-23 01:28:58 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type (
|
|
|
|
|
Host struct {
|
|
|
|
|
Echo *echo.Echo
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
2024-12-02 14:27:18 -05:00
|
|
|
// Setup Echo
|
|
|
|
|
hosts := map[string]*Host{}
|
|
|
|
|
api, err := rpc.NewLocalApi()
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------
|
|
|
|
|
// Website
|
|
|
|
|
//---------
|
|
|
|
|
site := echo.New()
|
|
|
|
|
site.Use(middleware.Logger())
|
|
|
|
|
site.Use(middleware.Recover())
|
|
|
|
|
site.Use(session.HwayMiddleware())
|
|
|
|
|
landing.RegisterRoutes(site)
|
|
|
|
|
hosts["localhost:3000"] = &Host{Echo: site}
|
|
|
|
|
|
|
|
|
|
//---------
|
|
|
|
|
// Gateway
|
|
|
|
|
//---------
|
|
|
|
|
highway := echo.New()
|
|
|
|
|
highway.Use(middleware.Logger())
|
|
|
|
|
highway.Use(middleware.Recover())
|
|
|
|
|
highway.Use(gatewaymiddleware.IPFSMiddleware(api))
|
|
|
|
|
gateway.RegisterRoutes(highway)
|
|
|
|
|
hosts["auth.localhost:3000"] = &Host{Echo: highway}
|
|
|
|
|
|
|
|
|
|
// Server
|
2024-11-23 01:28:58 -05:00
|
|
|
e := echo.New()
|
2024-12-02 14:27:18 -05:00
|
|
|
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
|
|
|
|
|
AllowOrigins: []string{"*"},
|
|
|
|
|
AllowMethods: []string{http.MethodGet, http.MethodPut, http.MethodPost, http.MethodDelete, http.MethodOptions},
|
|
|
|
|
}))
|
2024-11-23 01:28:58 -05:00
|
|
|
|
2024-12-02 14:27:18 -05:00
|
|
|
e.Any("/*", func(c echo.Context) (err error) {
|
|
|
|
|
req := c.Request()
|
|
|
|
|
res := c.Response()
|
2024-11-23 01:28:58 -05:00
|
|
|
|
2024-12-02 14:27:18 -05:00
|
|
|
host := hosts[req.Host]
|
|
|
|
|
if host != nil {
|
|
|
|
|
host.Echo.ServeHTTP(res, req)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Default to site for unmatched hosts
|
|
|
|
|
site.ServeHTTP(res, req)
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Log startup information using Echo's logger
|
|
|
|
|
fmt.Println("\n----------------------------------")
|
|
|
|
|
fmt.Println("Server Configuration:")
|
|
|
|
|
fmt.Println("\nAvailable endpoints:")
|
|
|
|
|
fmt.Println("➜ http://localhost:3000 (main site)")
|
|
|
|
|
fmt.Println("➜ http://to.localhost:3000/QmHash/... (IPFS content)")
|
|
|
|
|
fmt.Println("----------------------------------")
|
|
|
|
|
|
|
|
|
|
e.Logger.Fatal(e.Start(":3000"))
|
2024-11-23 01:28:58 -05:00
|
|
|
}
|