Skip to content

Commit

Permalink
[REFACTOR] refactor frontend cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
myth committed May 17, 2024
1 parent 69d3892 commit acdcffd
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions cmd/srv/frontend/srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package frontend

import (
"context"
"fmt"
"log"
"net/http"

"openmyth/blockchain/config"
"openmyth/blockchain/internal/contract/services"
"openmyth/blockchain/pkg/iface/processor"
)
Expand All @@ -24,12 +26,11 @@ func NewServer() *Server {

// loadServer initializes the server to listen on a specified port for HTTP requests.
func (s *Server) loadServer() {
port := s.service.Cfg.Frontend.Port
handler := http.FileServer(http.Dir("/html"))
log.Printf("server listening in port: %v", port)
if err := http.ListenAndServe(s.service.Cfg.Frontend.Address(), handler); err != nil {
log.Fatalf("failed to serve: %v", err)
fileSrv := &fileServer{
endpoint: s.service.Cfg.Frontend,
}

s.service.WithProcessors(fileSrv)
}

// Run runs the server with the provided context.
Expand All @@ -38,4 +39,33 @@ func (s *Server) Run(ctx context.Context) {
s.service.LoadConfig()

s.loadServer()

s.service.GracefulShutdown(ctx)
}

type fileServer struct {
server *http.Server

endpoint *config.Endpoint
}

// Start starts the file server to listen on a specified port.
func (s *fileServer) Start(_ context.Context) error {
port := s.endpoint.Port
handler := http.FileServer(http.Dir("/html"))
log.Printf("server listening in port: %v", port)
s.server = &http.Server{
Addr: fmt.Sprintf(":%s", port),
Handler: handler,
}
if err := s.server.ListenAndServe(); err != nil {
return err
}

return nil
}

// Stop stops the file server.
func (s *fileServer) Stop(_ context.Context) error {
return s.server.Close()
}

0 comments on commit acdcffd

Please sign in to comment.