Skip to content

Commit

Permalink
docs(mdox): Disable go reformatting
Browse files Browse the repository at this point in the history
Disable reformatting of Go code by mdox because it produces wildly
inconsistent output.
  • Loading branch information
abhinav committed Oct 11, 2022
1 parent 021ea79 commit 5aa4553
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 75 deletions.
2 changes: 1 addition & 1 deletion docs/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export PATH := $(shell pwd)/bin:$(PATH)

MDOX = $(shell pwd)/../bin/mdox
MDOX_FMT_FLAGS = --soft-wraps --links.validate
MDOX_FMT_FLAGS = --soft-wraps --links.validate --no-gofmt
MD_FILES = $(shell find . -name '*.md') ../README.md

.PHONY:
Expand Down
148 changes: 74 additions & 74 deletions docs/get-started/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ This application won't do anything yet except print a bunch of logs.
import "go.uber.org/fx"

func main() {
fx.New().Run()
fx.New().Run()
}
```

Expand Down Expand Up @@ -93,8 +93,8 @@ Let's add an HTTP server to it.
// NewHTTPServer builds an HTTP server that will begin serving requests
// when the Fx application starts.
func NewHTTPServer(lc fx.Lifecycle) *http.Server {
srv := &http.Server{Addr: ":8080"}
return srv
srv := &http.Server{Addr: ":8080"}
return srv
}
```

Expand All @@ -106,32 +106,32 @@ Let's add an HTTP server to it.

```go mdox-exec="region 02-http-server/main.go full"
func NewHTTPServer(lc fx.Lifecycle) *http.Server {
srv := &http.Server{Addr: ":8080"}
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
ln, err := net.Listen("tcp", srv.Addr)
if err != nil {
return err
}
fmt.Println("Starting HTTP server at", srv.Addr)
go srv.Serve(ln)
return nil
},
OnStop: func(ctx context.Context) error {
return srv.Shutdown(ctx)
},
})
return srv
srv := &http.Server{Addr: ":8080"}
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
ln, err := net.Listen("tcp", srv.Addr)
if err != nil {
return err
}
fmt.Println("Starting HTTP server at", srv.Addr)
go srv.Serve(ln)
return nil
},
OnStop: func(ctx context.Context) error {
return srv.Shutdown(ctx)
},
})
return srv
}
```

3. Provide this to your Fx application above with `fx.Provide`.

```go mdox-exec="region 02-http-server/main.go provide-server"
func main() {
fx.New(
fx.Provide(NewHTTPServer),
).Run()
fx.New(
fx.Provide(NewHTTPServer),
).Run()
}
```

Expand All @@ -153,10 +153,10 @@ Let's add an HTTP server to it.
5. To fix that, add an `fx.Invoke` that requests the constructed server.

```go mdox-exec="region 02-http-server/main.go app"
fx.New(
fx.Provide(NewHTTPServer),
fx.Invoke(func(*http.Server) {}),
).Run()
fx.New(
fx.Provide(NewHTTPServer),
fx.Invoke(func(*http.Server) {}),
).Run()
```

6. Run the application again.
Expand Down Expand Up @@ -230,14 +230,14 @@ Let's fix that.

// NewEchoHandler builds a new EchoHandler.
func NewEchoHandler() *EchoHandler {
return &EchoHandler{}
return &EchoHandler{}
}

// ServeHTTP handles an HTTP request to the /echo endpoint.
func (*EchoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if _, err := io.Copy(w, r.Body); err != nil {
fmt.Fprintln(os.Stderr, "Failed to handle request:", err)
}
if _, err := io.Copy(w, r.Body); err != nil {
fmt.Fprintln(os.Stderr, "Failed to handle request:", err)
}
}
```

Expand All @@ -259,9 +259,9 @@ Let's fix that.
// NewServeMux builds a ServeMux that will route requests
// to the given EchoHandler.
func NewServeMux(echo *EchoHandler) *http.ServeMux {
mux := http.NewServeMux()
mux.Handle("/echo", echo)
return mux
mux := http.NewServeMux()
mux.Handle("/echo", echo)
return mux
}
```

Expand Down Expand Up @@ -348,11 +348,11 @@ but you should be able to use any logging system.
```go mdox-exec="region 04-logger/main.go echo-init"
type EchoHandler struct {
log *zap.Logger
log *zap.Logger
}

func NewEchoHandler(log *zap.Logger) *EchoHandler {
return &EchoHandler{log: log}
return &EchoHandler{log: log}
}
```
Expand All @@ -361,9 +361,9 @@ but you should be able to use any logging system.
```go mdox-exec="region 04-logger/main.go echo-serve"
func (h *EchoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if _, err := io.Copy(w, r.Body); err != nil {
h.log.Warn("Failed to handle request", zap.Error(err))
}
if _, err := io.Copy(w, r.Body); err != nil {
h.log.Warn("Failed to handle request", zap.Error(err))
}
}
```
Expand Down Expand Up @@ -449,18 +449,18 @@ Let's try to fix this.
// Route is an http.Handler that knows the mux pattern
// under which it will be registered.
type Route interface {
http.Handler
http.Handler

// Pattern reports the path at which this is registered.
Pattern() string
// Pattern reports the path at which this is registered.
Pattern() string
}
```
2. Modify `EchoHandler` to implement this interface.
```go mdox-exec="region 05-registration/main.go echo-pattern"
func (*EchoHandler) Pattern() string {
return "/echo"
return "/echo"
}
```
Expand All @@ -485,9 +485,9 @@ Let's try to fix this.
// NewServeMux builds a ServeMux that will route requests
// to the given Route.
func NewServeMux(route Route) *http.ServeMux {
mux := http.NewServeMux()
mux.Handle(route.Pattern(), route)
return mux
mux := http.NewServeMux()
mux.Handle(route.Pattern(), route)
return mux
}
```
Expand Down Expand Up @@ -535,35 +535,35 @@ Let's add another.
// HelloHandler is an HTTP handler that
// prints a greeting to the user.
type HelloHandler struct {
log *zap.Logger
log *zap.Logger
}

// NewHelloHandler builds a new HelloHandler.
func NewHelloHandler(log *zap.Logger) *HelloHandler {
return &HelloHandler{log: log}
return &HelloHandler{log: log}
}
```
2. Implement the `Route` interface for this handler.
```go mdox-exec="region 06-another-handler/main.go hello-methods"
func (*HelloHandler) Pattern() string {
return "/hello"
return "/hello"
}

func (h *HelloHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
h.log.Error("Failed to read request", zap.Error(err))
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}

if _, err := fmt.Fprintf(w, "Hello, %s\n", body); err != nil {
h.log.Error("Failed to write response", zap.Error(err))
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
h.log.Error("Failed to read request", zap.Error(err))
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}

if _, err := fmt.Fprintf(w, "Hello, %s\n", body); err != nil {
h.log.Error("Failed to write response", zap.Error(err))
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
}
```
Expand Down Expand Up @@ -629,10 +629,10 @@ Let's add another.
// NewServeMux builds a ServeMux that will route requests
// to the given routes.
func NewServeMux(route1, route2 Route) *http.ServeMux {
mux := http.NewServeMux()
mux.Handle(route1.Pattern(), route1)
mux.Handle(route2.Pattern(), route2)
return mux
mux := http.NewServeMux()
mux.Handle(route1.Pattern(), route1)
mux.Handle(route2.Pattern(), route2)
return mux
}
```
Expand Down Expand Up @@ -698,11 +698,11 @@ Let's do that.
```go mdox-exec="region 07-many-handlers/main.go mux"
func NewServeMux(routes []Route) *http.ServeMux {
mux := http.NewServeMux()
for _, route := range routes {
mux.Handle(route.Pattern(), route)
}
return mux
mux := http.NewServeMux()
for _, route := range routes {
mux.Handle(route.Pattern(), route)
}
return mux
}
```
Expand All @@ -725,11 +725,11 @@ Let's do that.
// AsRoute annotates the given constructor to state that
// it provides a route to the "routes" group.
func AsRoute(f any) any {
return fx.Annotate(
f,
fx.As(new(Route)),
fx.ResultTags(`group:"routes"`),
)
return fx.Annotate(
f,
fx.As(new(Route)),
fx.ResultTags(`group:"routes"`),
)
}
```
Expand Down

0 comments on commit 5aa4553

Please sign in to comment.