forked from ServiceWeaver/template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
55 lines (47 loc) · 1.6 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"context"
"fmt"
"log"
"net/http"
"github.com/ServiceWeaver/weaver"
)
// This is a template of a simple Service Weaver application. You can run the
// application by running `go run .`.
//
// $ go run .
//
// To use a config file, first update the binary name in weaver.toml to be the
// name of the binary that's produced when you run `go build .`. Then, you can
// run the app in a single process or in multiple processes.
//
// $ weaver single deploy weaver.toml
// $ weaver multi deploy weaver.toml
//
// See https://serviceweaver.dev/docs for more information on writing Service
// Weaver applications.
//go:generate weaver generate ./...
func main() {
// weaver.Run runs a Service Weaver application. It creates and initializes
// all components, and then passes a pointer to the main component to the
// serve function.
if err := weaver.Run(context.Background(), serve); err != nil {
log.Fatal(err)
}
}
// app implements the main component, the entry point to a Service Weaver app.
type app struct {
weaver.Implements[weaver.Main]
// lis is the network listener on which this app serves HTTP traffic. By
// default, the name of the listener is the same as the name of the field,
// but you can override the name using a `weaver:"NAME"` annotation.
lis weaver.Listener `weaver:"lis"`
}
// serve serves HTTP traffic.
func serve(ctx context.Context, app *app) error {
http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprintln(w, "Hello, World!")
})
app.Logger(ctx).Info("Listening on...", "address", app.lis)
return http.Serve(app.lis, nil)
}