2024-09-07 18:12:58 -04:00
|
|
|
//go:build js && wasm
|
|
|
|
|
// +build js,wasm
|
|
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
2024-09-11 15:10:54 -04:00
|
|
|
"github.com/onsonr/sonr/internal/vfs/wasm"
|
2024-09-07 18:12:58 -04:00
|
|
|
|
2024-09-11 15:10:54 -04:00
|
|
|
"github.com/donseba/go-htmx"
|
2024-09-07 18:12:58 -04:00
|
|
|
"github.com/onsonr/sonr/internal/db"
|
|
|
|
|
)
|
|
|
|
|
|
2024-09-11 15:10:54 -04:00
|
|
|
var dwn *DWN
|
|
|
|
|
|
|
|
|
|
type DWN struct {
|
|
|
|
|
*echo.Echo
|
|
|
|
|
DB *db.DB
|
|
|
|
|
Htmx *htmx.HTMX
|
|
|
|
|
}
|
2024-09-07 18:12:58 -04:00
|
|
|
|
|
|
|
|
func main() {
|
2024-09-11 15:10:54 -04:00
|
|
|
dwn = initRails()
|
|
|
|
|
|
|
|
|
|
dwn.GET("/", htmxHandler)
|
|
|
|
|
dwn.POST("/accounts", accountsHandler)
|
2024-09-07 18:12:58 -04:00
|
|
|
|
2024-09-11 15:10:54 -04:00
|
|
|
wasm.Serve(dwn.Echo)
|
2024-09-07 18:12:58 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-11 15:10:54 -04:00
|
|
|
// initRails initializes the Rails application
|
|
|
|
|
func initRails() *DWN {
|
|
|
|
|
// Open the database
|
|
|
|
|
cnfg := db.New()
|
|
|
|
|
db, err := cnfg.Open()
|
2024-09-07 18:12:58 -04:00
|
|
|
if err != nil {
|
|
|
|
|
panic(err.Error())
|
|
|
|
|
}
|
2024-09-11 15:10:54 -04:00
|
|
|
|
|
|
|
|
// Initialize the htmx handler
|
|
|
|
|
return &DWN{
|
|
|
|
|
Echo: echo.New(),
|
|
|
|
|
DB: db,
|
|
|
|
|
Htmx: htmx.New(),
|
|
|
|
|
}
|
2024-09-07 18:12:58 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-11 15:10:54 -04:00
|
|
|
func accountsHandler(e echo.Context) error {
|
2024-09-07 18:12:58 -04:00
|
|
|
switch e.Request().Method {
|
|
|
|
|
case "GET":
|
2024-09-11 15:10:54 -04:00
|
|
|
case "PUT":
|
|
|
|
|
// dwn.DB.AddAccount(e.Param("account"), e.Param("address"))
|
2024-09-07 18:12:58 -04:00
|
|
|
default:
|
|
|
|
|
e.Error(errors.New("Method not allowed"))
|
|
|
|
|
}
|
|
|
|
|
return e.JSON(200, "OK")
|
|
|
|
|
}
|
2024-09-11 15:10:54 -04:00
|
|
|
|
|
|
|
|
func htmxHandler(e echo.Context) error {
|
|
|
|
|
// initiate a new htmx handler
|
|
|
|
|
h := dwn.Htmx.NewHandler(e.Response(), e.Request())
|
|
|
|
|
|
|
|
|
|
// check if the request is a htmx request
|
|
|
|
|
if h.IsHxRequest() {
|
|
|
|
|
// do something
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check if the request is boosted
|
|
|
|
|
if h.IsHxBoosted() {
|
|
|
|
|
// do something
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check if the request is a history restore request
|
|
|
|
|
if h.IsHxHistoryRestoreRequest() {
|
|
|
|
|
// do something
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check if the request is a prompt request
|
|
|
|
|
if h.RenderPartial() {
|
|
|
|
|
// do something
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// set the headers for the response, see docs for more options
|
|
|
|
|
h.PushURL("http://push.url")
|
|
|
|
|
h.ReTarget("#ReTarged")
|
|
|
|
|
|
|
|
|
|
// write the output like you normally do.
|
|
|
|
|
// check the inspector tool in the browser to see that the headers are set.
|
|
|
|
|
_, err := h.Write([]byte("OK"))
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// func credentialsHandler(e echo.Context) error {
|
|
|
|
|
// switch e.Request().Method {
|
|
|
|
|
// case "GET":
|
|
|
|
|
// dwn.DB.GetCredentials(e.Param("account"))
|
|
|
|
|
// case "PUT":
|
|
|
|
|
// dwn.DB.AddCredentials(e.Param("account"), e.Param("address"))
|
|
|
|
|
// default:
|
|
|
|
|
// e.Error(errors.New("Method not allowed"))
|
|
|
|
|
// }
|
|
|
|
|
// return e.JSON(200, "OK")
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// func keysharesHandler(e echo.Context) error {
|
|
|
|
|
// switch e.Request().Method {
|
|
|
|
|
// case "GET":
|
|
|
|
|
// dwn.DB.GetKeyshares(e.Param("account"))
|
|
|
|
|
// case "PUT":
|
|
|
|
|
// dwn.DB.AddKeyshares(e.Param("account"), e.Param("address"))
|
|
|
|
|
// default:
|
|
|
|
|
// e.Error(errors.New("Method not allowed"))
|
|
|
|
|
// }
|
|
|
|
|
// return e.JSON(200, "OK")
|
|
|
|
|
// }
|