Skip to content

Commit

Permalink
Removed Main; changed weaver.Run to take lambda.
Browse files Browse the repository at this point in the history
This PR reverts #359. In particular, it removes the main component's
Main method and changes `weaver.Run` to take a lambda.

We made this change for the following reason. The Main method is run by
`weaver.Run`, but not by `weavertest.Runner.Test`. We felt this was
confusing. With the lambda API, it is much clearer what is run. This
change also unifies the `weaver.Run` and `weavertest.Runner.Test` API.
Both take a lambda with component arguments.

Right now, the signature of `weaver.Run` is a bit gnarly. I think I can
simplify it a bit in a future PR to remove PointerToMain at least.
  • Loading branch information
mwhittaker committed Jun 21, 2023
1 parent 85730c9 commit a4a3f37
Show file tree
Hide file tree
Showing 24 changed files with 78 additions and 809 deletions.
9 changes: 1 addition & 8 deletions component.go
Expand Up @@ -15,7 +15,6 @@
package weaver

import (
"context"
"crypto/tls"
"net"
"sync"
Expand Down Expand Up @@ -101,13 +100,7 @@ type component struct {
var _ Instance = &componentImpl{}

// Main is interface implemented by an application's main component.
// This component is instantiated and its Main() method called by
// `weaver.Run`.
type Main interface {
// Main contains the application main. It typically loops
// forever, e.g., inside http.Serve.
Main(context.Context) error
}
type Main interface{}

// Implements[T] is a type that can be embedded inside a component implementation
// struct to indicate that the struct implements a component of type T. E.g.,
Expand Down
2 changes: 1 addition & 1 deletion examples/chat/main.go
Expand Up @@ -26,7 +26,7 @@ import (

func main() {
flag.Parse()
if err := weaver.Run(context.Background()); err != nil {
if err := weaver.Run(context.Background(), serve); err != nil {
log.Fatal(err)
}
}
2 changes: 1 addition & 1 deletion examples/chat/server.go
Expand Up @@ -43,7 +43,7 @@ type server struct {
chat weaver.Listener
}

func (s *server) Main(ctx context.Context) error {
func serve(ctx context.Context, s *server) error {
s.httpServer.Handler = instrument(s.label, s)
s.Logger().Debug("Chat service available", "address", s.chat)
return s.httpServer.Serve(s.chat)
Expand Down
93 changes: 2 additions & 91 deletions examples/chat/weaver_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/collatz/main.go
Expand Up @@ -59,7 +59,7 @@ var localAddr = flag.String("local_addr", "localhost:9000", "Collatz server loca

func main() {
flag.Parse()
if err := weaver.Run(context.Background()); err != nil {
if err := weaver.Run(context.Background(), serve); err != nil {
log.Fatal(err)
}
}
2 changes: 1 addition & 1 deletion examples/collatz/server.go
Expand Up @@ -33,7 +33,7 @@ type server struct {
lis weaver.Listener `weaver:"collatz"`
}

func (s *server) Main(ctx context.Context) error {
func serve(ctx context.Context, s *server) error {
s.mux.Handle("/", weaver.InstrumentHandlerFunc("collatz", s.handle))
s.mux.HandleFunc(weaver.HealthzURL, weaver.HealthzHandler)
s.Logger().Debug("Collatz service available", "address", s.lis)
Expand Down
93 changes: 2 additions & 91 deletions examples/collatz/weaver_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/factors/main.go
Expand Up @@ -28,7 +28,7 @@ import (
func main() {
flag.Parse()
ctx := context.Background()
if err := weaver.Run(ctx); err != nil {
if err := weaver.Run(ctx, serve); err != nil {
log.Fatal(err)
}
}
2 changes: 1 addition & 1 deletion examples/factors/server.go
Expand Up @@ -29,7 +29,7 @@ type server struct {
lis weaver.Listener `weaver:"factors"`
}

func (s *server) Main(ctx context.Context) error {
func serve(ctx context.Context, s *server) error {
http.Handle("/", weaver.InstrumentHandlerFunc("/", s.handleFactors))
s.Logger().Info("factors server running", "addr", s.lis)
return http.Serve(s.lis, nil)
Expand Down

0 comments on commit a4a3f37

Please sign in to comment.