Skip to content

Commit

Permalink
Export Dev Wallet Server (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjartek authored Feb 2, 2025
1 parent bb33f8f commit 69de8fd
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 100 deletions.
6 changes: 3 additions & 3 deletions go/wallet/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func main() {
var port uint
var rootCmd = &cobra.Command{
rootCmd := &cobra.Command{
Use: "wallet",
Short: "Flow Dev Wallet",
Long: `Flow Dev Wallet`,
Expand All @@ -21,12 +21,12 @@ func main() {
}

fmt.Printf("Development server started on port %d\n", port)
srv.Start()
srv.StartStandalone()
},
}

rootCmd.PersistentFlags().UintVar(&port, "port", 8701, "Port to run the server on")
viper.BindPFlag("port", rootCmd.PersistentFlags().Lookup("port"))
viper.BindPFlag("port", rootCmd.PersistentFlags().Lookup("port"))

if err := rootCmd.Execute(); err != nil {
panic(err)
Expand Down
6 changes: 3 additions & 3 deletions go/wallet/config_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

// configHandler handles config endpoints
func (server *server) configHandler(w http.ResponseWriter, r *http.Request) {
func (server *Server) configHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)

Expand All @@ -33,7 +33,6 @@ func buildConfig(flowConfig *FlowConfig, envConfig []byte) (map[string]string, e
return nil, err
}


var flow map[string]string
err = json.Unmarshal(flowConf, &flow)
if err != nil {
Expand All @@ -54,4 +53,5 @@ func buildConfig(flowConfig *FlowConfig, envConfig []byte) (map[string]string, e
}

return tempt, nil
}
}

5 changes: 3 additions & 2 deletions go/wallet/dev_wallet_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

// devWalletHandler handles endpoints to exported static html files
func (srv *server) devWalletHandler(writer http.ResponseWriter, request *http.Request) {
func (srv *Server) devWalletHandler(writer http.ResponseWriter, request *http.Request) {
zipContent, _ := srv.bundle.ReadFile(srv.bundleZip)
zipFS, _ := zip.NewReader(bytes.NewReader(zipContent), int64(len(zipContent)))
rootFS := http.FS(zipFS)
Expand All @@ -28,4 +28,5 @@ func (srv *server) devWalletHandler(writer http.ResponseWriter, request *http.Re

request.URL.Path = path
http.FileServer(rootFS).ServeHTTP(writer, request)
}
}

67 changes: 34 additions & 33 deletions go/wallet/discovery_handler.go

Large diffs are not rendered by default.

18 changes: 8 additions & 10 deletions go/wallet/polling_session_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import (
)

type UpdatePollingSessionRequest struct {
PollingId string `json:"pollingId"`
Data map[string]interface{} `json:"data"`
PollingId string `json:"pollingId"`
Data map[string]interface{} `json:"data"`
}

func (srv *server) getPollingSessionHandler(w http.ResponseWriter, r *http.Request) {
func (srv *Server) getPollingSessionHandler(w http.ResponseWriter, r *http.Request) {
pollingId, err := strconv.Atoi(r.URL.Query().Get("pollingId"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

if _, ok := srv.pollingSessions[pollingId]; !ok {
w.WriteHeader(http.StatusNotFound)
} else {
Expand All @@ -27,7 +27,6 @@ func (srv *server) getPollingSessionHandler(w http.ResponseWriter, r *http.Reque
pollingSession := srv.pollingSessions[pollingId]

obj, err := json.Marshal(pollingSession)

if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
Expand All @@ -41,23 +40,22 @@ func (srv *server) getPollingSessionHandler(w http.ResponseWriter, r *http.Reque
}
}

func (srv *server) postPollingSessionHandler(w http.ResponseWriter, r *http.Request) {
func (srv *Server) postPollingSessionHandler(w http.ResponseWriter, r *http.Request) {
var req UpdatePollingSessionRequest
err := json.NewDecoder(r.Body).Decode(&req)

if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

pollingId, err := strconv.Atoi(req.PollingId)

if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}

srv.pollingSessions[pollingId] = req.Data

w.WriteHeader(http.StatusCreated)
}
}

46 changes: 27 additions & 19 deletions go/wallet/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,38 @@ var bundle embed.FS
var envConfig []byte

const bundleZip = "bundle.zip"
type server struct {
http *http.Server
config *FlowConfig
bundle embed.FS
bundleZip string

type Server struct {
http *http.Server
config *FlowConfig
bundle embed.FS
bundleZip string
pollingSessions map[int]map[string]interface{}
nextPollingId int
nextPollingId int
}

type FlowConfig struct {
Address string `json:"flowAccountAddress"`
PrivateKey string `json:"flowAccountPrivateKey"`
PublicKey string `json:"flowAccountPublicKey"`
AccessNode string `json:"flowAccessNode"`
AvatarUrl string `json:"flowAvatarUrl"`
AvatarUrl string `json:"flowAvatarUrl"`
}

// NewHTTPServer returns a new wallet server listening on provided port number.
func NewHTTPServer(port uint, config *FlowConfig) (*server, error) {
func NewHTTPServer(port uint, config *FlowConfig) (*Server, error) {
http := http.Server{
Addr: fmt.Sprintf(":%d", port),
Addr: fmt.Sprintf(":%d", port),
Handler: nil,
}

srv := &server{
http: &http,
config: config,
bundle: bundle,
bundleZip: bundleZip,
srv := &Server{
http: &http,
config: config,
bundle: bundle,
bundleZip: bundleZip,
pollingSessions: make(map[int]map[string]interface{}),
nextPollingId: 0,
nextPollingId: 0,
}

r := mux.NewRouter()
Expand All @@ -70,7 +71,11 @@ func NewHTTPServer(port uint, config *FlowConfig) (*server, error) {
return srv, nil
}

func (s *server) Start() {
func (s *Server) Start() error {
return s.http.ListenAndServe()
}

func (s *Server) StartStandalone() {
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

Expand All @@ -86,8 +91,11 @@ func (s *server) Start() {
s.Stop()
}

func (s *server) Stop() {
_ = s.http.Shutdown(context.Background())
func (s *Server) Stop() {
err := s.http.Shutdown(context.Background())
if err != nil {
panic(err)
}
}

func enableCors(handler http.Handler) http.Handler {
Expand All @@ -105,4 +113,4 @@ func enableCors(handler http.Handler) http.Handler {
// Call the next handler
handler.ServeHTTP(w, r)
})
}
}
57 changes: 27 additions & 30 deletions go/wallet/service_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import (
)

type Service struct {
FType string `json:"f_type"`
FVsn string `json:"f_vsn"`
Type string `json:"type"`
Endpoint string `json:"endpoint"`
Method string `json:"method"`
Params map[string]string `json:"params"`
FType string `json:"f_type"`
FVsn string `json:"f_vsn"`
Type string `json:"type"`
Endpoint string `json:"endpoint"`
Method string `json:"method"`
Params map[string]string `json:"params"`
}

type FclResponse struct {
FType string `json:"f_type"`
FVsn string `json:"f_vsn"`
Status string `json:"status"`
FType string `json:"f_type"`
FVsn string `json:"f_vsn"`
Status string `json:"status"`
Updates Service `json:"updates"`
}

Expand All @@ -42,10 +42,8 @@ type FCLMessage struct {
}

func getMethod(fclMessageJson []byte) string {

var fclMessage FCLMessage
err := json.Unmarshal(fclMessageJson, &fclMessage)

if err != nil {
fmt.Println("Error:", err)
return "VIEW/IFRAME"
Expand All @@ -58,7 +56,7 @@ func getMethod(fclMessageJson []byte) string {
return "VIEW/IFRAME"
}

func (server *server) postServiceHandler(w http.ResponseWriter, r *http.Request) {
func (server *Server) postServiceHandler(w http.ResponseWriter, r *http.Request) {
service := strings.TrimPrefix(r.URL.Path, "/api/")
if service == "" {
w.WriteHeader(http.StatusNotFound)
Expand All @@ -69,8 +67,8 @@ func (server *server) postServiceHandler(w http.ResponseWriter, r *http.Request)
method := getMethod(fclMessageJson)

if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
w.WriteHeader(http.StatusBadRequest)
return
}

pollingId := server.nextPollingId
Expand All @@ -80,22 +78,21 @@ func (server *server) postServiceHandler(w http.ResponseWriter, r *http.Request)
baseUrl := getBaseUrl(r)

pendingResponse := FclResponse{
FType: "PollingResponse",
FVsn: "1.0.0",
FType: "PollingResponse",
FVsn: "1.0.0",
Status: "PENDING",
Updates: Service {
FType: "PollingResponse",
FVsn: "1.0.0",
Type: "back-channel-rpc",
Updates: Service{
FType: "PollingResponse",
FVsn: "1.0.0",
Type: "back-channel-rpc",
Endpoint: baseUrl + "/api/polling-session",
Method: "HTTP/GET",
Method: "HTTP/GET",
Params: map[string]string{
"pollingId": fmt.Sprint(pollingId),
},
},
}


// Use json to convert struct to map
tmp, err := json.Marshal(pendingResponse)
if err != nil {
Expand All @@ -114,19 +111,18 @@ func (server *server) postServiceHandler(w http.ResponseWriter, r *http.Request)
responseJson, err := json.Marshal(ServicePostResponse{
FclResponse: pendingResponse,
Local: Service{
FType: "Service",
FVsn: "1.0.0",
Type: "local-view",
FType: "Service",
FVsn: "1.0.0",
Type: "local-view",
Endpoint: baseUrl + "/fcl/" + service,
Method: method,
Method: method,
Params: map[string]string{
"pollingId": fmt.Sprint(pollingId),
"channel": "back",
"pollingId": fmt.Sprint(pollingId),
"channel": "back",
"fclMessageJson": string(fclMessageJson),
},
},
})

if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
Expand All @@ -135,4 +131,5 @@ func (server *server) postServiceHandler(w http.ResponseWriter, r *http.Request)
w.WriteHeader(http.StatusCreated)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(responseJson))
}
}

0 comments on commit 69de8fd

Please sign in to comment.