2024-11-23 01:28:58 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
_ "embed"
|
2024-12-02 14:27:18 -05:00
|
|
|
"fmt"
|
2024-12-05 20:36:58 -05:00
|
|
|
"os"
|
2024-12-13 15:10:27 -05:00
|
|
|
|
|
|
|
|
"github.com/labstack/echo-contrib/echoprometheus"
|
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
|
"github.com/labstack/echo/v4/middleware"
|
|
|
|
|
"github.com/onsonr/sonr/crypto/ucan"
|
|
|
|
|
"github.com/onsonr/sonr/internal/gateway"
|
|
|
|
|
config "github.com/onsonr/sonr/pkg/config/hway"
|
|
|
|
|
"github.com/onsonr/sonr/pkg/didauth/producer"
|
|
|
|
|
"github.com/onsonr/sonr/pkg/ipfsapi"
|
|
|
|
|
"gorm.io/gorm"
|
2024-11-23 01:28:58 -05:00
|
|
|
)
|
|
|
|
|
|
2024-12-05 20:36:58 -05:00
|
|
|
// main is the entry point for the application
|
2024-11-23 01:28:58 -05:00
|
|
|
func main() {
|
2024-12-10 15:27:36 -05:00
|
|
|
cmd := rootCmd()
|
2024-12-10 15:25:51 -05:00
|
|
|
if err := cmd.Execute(); err != nil {
|
|
|
|
|
fmt.Println(err)
|
2024-12-05 20:36:58 -05:00
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
2024-12-10 15:25:51 -05:00
|
|
|
os.Exit(0)
|
2024-11-23 01:28:58 -05:00
|
|
|
}
|
2024-12-13 15:10:27 -05:00
|
|
|
|
|
|
|
|
func initDeps(env config.Hway) (*gorm.DB, ipfsapi.Client, error) {
|
|
|
|
|
db, err := gateway.NewDB(env)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ipc, err := ipfsapi.NewClient()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return db, ipc, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func loadEnvImplFromArgs(args []string) (config.Hway, error) {
|
|
|
|
|
cmd := rootCmd()
|
|
|
|
|
if err := cmd.ParseFlags(args); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
env := &config.HwayImpl{
|
|
|
|
|
ServePort: servePort,
|
|
|
|
|
SqliteFile: sqliteFile,
|
|
|
|
|
ChainId: chainID,
|
|
|
|
|
IpfsGatewayUrl: ipfsGatewayURL,
|
|
|
|
|
SonrApiUrl: sonrAPIURL,
|
|
|
|
|
SonrGrpcUrl: sonrGrpcURL,
|
|
|
|
|
SonrRpcUrl: sonrRPCURL,
|
|
|
|
|
PsqlDSN: formatPsqlDSN(),
|
|
|
|
|
}
|
|
|
|
|
return env, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// setupServer sets up the server
|
|
|
|
|
func setupServer(env config.Hway, db *gorm.DB, ipc ipfsapi.Client) (*echo.Echo, error) {
|
|
|
|
|
e := echo.New()
|
|
|
|
|
e.Use(echoprometheus.NewMiddleware("hway"))
|
|
|
|
|
e.IPExtractor = echo.ExtractIPDirect()
|
|
|
|
|
e.Use(middleware.Logger())
|
|
|
|
|
e.Use(middleware.Recover())
|
|
|
|
|
e.Use(producer.Middleware(ipc, ucan.ServicePermissions))
|
|
|
|
|
gateway.RegisterRoutes(e, env, db)
|
|
|
|
|
return e, nil
|
|
|
|
|
}
|