-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
90 lines (73 loc) · 2.32 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"context"
"database/sql"
"example/hello/GO-KIT/account"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"gokit-example/account"
_ "github.com/lib/pq"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
)
// Create a user module that allows us to insert a user into a DB from a Web API and be able to fetch that user back using a unique user ID that we generate inside of the business logic
const dbsource = "postgresql://postgres:postgres@localhost:5432/gokitexample?sslmode=disable"
func main() {
var httpAddr = flag.String("http", ":8080", "http listen address")
// Setup logger so it has timestamp, caller etc.
var logger log.Logger
{
logger = log.NewLogfmtLogger(os.Stderr)
logger = log.NewSyncLogger(logger)
logger = log.With(logger,
"service", "account",
"time:", log.DefaultTimestampUTC,
"caller", log.DefaultCaller)
}
level.Info(logger).Log("msg", "service started")
defer level.Info(logger).Log("msg", "service ended")
var db *sql.DB
{
var err error
db, err = sql.Open("postgres", dbsource)
if err != nil {
level.Error(logger).Log("exit", err)
os.Exit(-1)
}
}
// Parse the CLI flag is we get one
flag.Parse()
// Creates a non-nil empty context
ctx := context.Background()
var srv account.Service
{
// To create the microservice we first need to create a repository
repository := account.NewRepo(db, logger)
// Then create the service itself by passing in the repo and the logger into the call to NewService
srv = account.NewService(respository, logger)
}
// Create errors channel
errs := make(chan error)
// Go routine to check if there's a SIG termination in the operating system
// If that happens then will generate errors
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
errs <- fmt.Errorf("%s", <-c)
}()
// Create endpoints for our service
endpoints := account.Endpoints(srv)
// Go routine which will execute our server
go func() {
fmt.Println("listening on port", *httpAddr)
handler := account.NewHTTPServer(ctx, endpoints)
// If given an error will be piped into our errs channel
errs <- http.ListenAndServe(*httpAddr, handler)
}()
level.Error(logger).Log("exit", <-errs)
}