Skip to content

Commit

Permalink
iter8 features
Browse files Browse the repository at this point in the history
  • Loading branch information
ex0rcist committed Jul 25, 2024
1 parent 8b7cf0c commit f045716
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 23 deletions.
5 changes: 0 additions & 5 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ func main() {
logging.LogFatal(err)
}

err = srv.ParseFlags()
if err != nil {
logging.LogFatal(err)
}

err = srv.Run()
if err != nil {
logging.LogFatal(err)
Expand Down
2 changes: 1 addition & 1 deletion internal/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewRouter(storageService storage.StorageService) http.Handler {

resource := NewMetricResource(storageService)

router.Get("/", resource.Homepage) // TODO: resource?
router.Get("/", resource.Homepage)

router.Post("/update/{metricKind}/{metricName}/{metricValue}", resource.UpdateMetric)
router.Post("/update", resource.UpdateMetricJSON)
Expand Down
62 changes: 45 additions & 17 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
"fmt"
"net/http"
"os"

"github.com/caarlos0/env/v11"
"github.com/ex0rcist/metflix/internal/entities"
Expand All @@ -12,9 +13,10 @@ import (
)

type Server struct {
config *Config
Storage storage.MetricsStorage
Router http.Handler
config *Config
httpServer *http.Server
Storage storage.MetricsStorage
Router http.Handler
}

type Config struct {
Expand All @@ -26,34 +28,60 @@ func New() (*Server, error) {
Address: "0.0.0.0:8080",
}

err := parseFlags(config, os.Args[0], os.Args[1:])
if err != nil {
return nil, err
}

err = parseEnv(config)
if err != nil {
return nil, err
}

memStorage := storage.NewMemStorage()
storageService := storage.NewService(memStorage)

router := NewRouter(storageService)

httpServer := &http.Server{
Addr: config.Address.String(),
Handler: router,
}

return &Server{
config: config,
Storage: memStorage,
Router: router,
config: config,
httpServer: httpServer,
Storage: memStorage,
Router: router,
}, nil
}

func (s *Server) ParseFlags() error {
address := s.config.Address
func parseFlags(config *Config, progname string, args []string) error {
flags := pflag.NewFlagSet(progname, pflag.ContinueOnError)

pflag.VarP(&address, "address", "a", "address:port for HTTP API requests")
pflag.Parse()
address := config.Address

flags.VarP(&address, "address", "a", "address:port for HTTP API requests")
err := flags.Parse(args)

if err != nil {
return err
}

// because VarP gets non-pointer value, set it manually
pflag.Visit(func(f *pflag.Flag) {
flags.Visit(func(f *pflag.Flag) {
switch f.Name {
case "address":
s.config.Address = address
config.Address = address
}
})

if err := env.Parse(s.config); err != nil {
return entities.NewStackError(err)
return nil
}

func parseEnv(config *Config) error {
fmt.Println("====== " + os.Getenv("ADDRESS") + " ======")
if err := env.Parse(config); err != nil {
return err
}

return nil
Expand All @@ -63,9 +91,9 @@ func (s *Server) Run() error {
logging.LogInfo(s.config.String())
logging.LogInfo("server ready")

return http.ListenAndServe(s.config.Address.String(), s.Router)
return s.httpServer.ListenAndServe()
}

func (c Config) String() string {
return "server config: " + fmt.Sprintf("address=%s\t", c.Address)
return "server config: " + fmt.Sprintf("address=%s", c.Address)
}
70 changes: 70 additions & 0 deletions internal/server/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package server

import (
"testing"
)

func TestNew(t *testing.T) {
server, err := New()
if err != nil {
t.Fatalf("expected no error, got %v", err)
}

if server.Storage == nil {
t.Fatal("expected storage to not be nil")
}

if server.Router == nil {
t.Fatal("expected router to not be nil")
}
}

func TestParseFlags(t *testing.T) {

tests := []struct {
name string
args []string
want Config
wantErr bool
}{
{
name: "shortcut",
args: []string{"-a0.0.0.0:8080"},
want: Config{Address: "0.0.0.0:8080"},
wantErr: false,
},
{
name: "shortcut",
args: []string{"--address=127.0.0.1:81"},
want: Config{Address: "127.0.0.1:81"},
wantErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config := &Config{Address: "default"}

err := parseFlags(config, "progname", tt.args)
if (err != nil) != tt.wantErr {
t.Fatalf("Expected no error, got %v", err)
}
if tt.want != *config {
t.Errorf("Expected %v, got %v", tt.want, config)
}
})
}
}

func TestRun(t *testing.T) {
// pending: how to test lsitenAndServe? goroutine?
}

// TestConfigString tests the String method of Config.
func TestConfigString(t *testing.T) {
config := Config{Address: "0.0.0.0:8080"}
expected := "server config: address=0.0.0.0:8080"
if config.String() != expected {
t.Errorf("Expected %v, got %v", expected, config.String())
}
}

0 comments on commit f045716

Please sign in to comment.