-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
90 lines (77 loc) · 1.71 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"
"fmt"
"log"
"net/http"
"os"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/google/wire"
"github.com/miya-masa/go-clean-webapp/web"
"github.com/urfave/cli"
)
var (
Version string
Revision string
)
var logger = log.New(os.Stdout, "tx", log.LstdFlags)
func main() {
app := cli.NewApp()
app.Name = "gotxsand"
app.Version = fmt.Sprintf("%v-%v", Version, Revision)
app.Commands = []cli.Command{
{
Name: "serve",
ShortName: "s",
Action: func(c *cli.Context) error {
logger.Printf("start serve")
return serve()
},
},
{
Name: "init",
ShortName: "i",
Action: func(c *cli.Context) error {
logger.Printf("initialize data")
return initializeData()
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
// applicationSet is the Wire provider set for the Guestbook application that
// does not depend on the underlying platform.
var applicationSet = wire.NewSet(
newApplication,
)
func newApplication(ctx context.Context, ah *web.AccountHandler) (Application, error) {
return Application{AccountHandler: ah}, nil
}
func serve() error {
ctx := context.Background()
app, err := setupApplication(ctx)
if err != nil {
return err
}
return app.Start()
}
type Application struct {
AccountHandler *web.AccountHandler
}
func (a *Application) Start() error {
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.Logger)
r.Route("/accounts", func(r chi.Router) {
r.Get("/", a.AccountHandler.List)
r.Post("/", a.AccountHandler.Post)
r.Get("/{accountUUID}", a.AccountHandler.Get)
r.Delete("/{accountUUID}", a.AccountHandler.Delete)
})
http.ListenAndServe(":8080", r)
return nil
}