Created
September 23, 2020 01:26
-
-
Save podhmo/8789eb1d59adedb8b0903e14ddd7b1a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module m | |
go 1.15 | |
require ( | |
github.com/go-chi/chi v4.1.2+incompatible | |
github.com/go-chi/render v1.0.1 | |
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
github.com/go-chi/chi v1.0.0 h1:s/kv1cTXfivYjdKJdyUzNGyAWZ/2t7duW1gKn5ivu+c= | |
github.com/go-chi/chi v4.1.2+incompatible h1:fGFk2Gmi/YKXk0OmGfBh0WgmN3XB8lVnEyNz34tQRec= | |
github.com/go-chi/chi v4.1.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ= | |
github.com/go-chi/render v1.0.1 h1:4/5tis2cKaNdnv9zFLfXzcquC9HbeZgCnxGnKrltBS8= | |
github.com/go-chi/render v1.0.1/go.mod h1:pq4Rr7HbnsdaeHagklXub+p6Wd16Af5l9koip1OvJns= | |
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= | |
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
"os" | |
"github.com/go-chi/chi" | |
"github.com/go-chi/render" | |
"golang.org/x/xerrors" | |
) | |
func main() { | |
s := &Server{ | |
Router: chi.NewRouter(), | |
logger: log.New(os.Stdout, "app", 0), | |
} | |
s.Mount() | |
addr := os.Getenv("Addr") | |
if addr == "" { | |
addr = ":4444" | |
} | |
log.Printf("listen: %s", addr) | |
if err := http.ListenAndServe(addr, s); err != nil { | |
log.Fatalf("!! %+v", err) | |
} | |
} | |
type logger interface { | |
Printf(string, ...interface{}) | |
} | |
type Server struct { | |
logger logger | |
chi.Router | |
} | |
func (s *Server) Mount() { | |
s.Get("/500", func(w http.ResponseWriter, r *http.Request) { | |
err := f() | |
if err != nil { | |
s.logger.Printf("! %+v", err) | |
render.Status(r, 500) | |
render.JSON(w, r, map[string]string{"error": fmt.Sprintf("%v", err)}) | |
return | |
} | |
render.JSON(w, r, map[string]string{"message": "never"}) | |
}) | |
} | |
func f() error { | |
err := g() | |
return xerrors.Errorf("f %w", err) | |
} | |
func g() error { | |
err := h() | |
return xerrors.Errorf("g %w", err) | |
} | |
func h() error { | |
return fmt.Errorf("h") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io" | |
"net/http" | |
"net/http/httptest" | |
"os" | |
"testing" | |
"github.com/go-chi/chi" | |
) | |
type testlogger struct { | |
testing.TB | |
} | |
func (tl *testlogger) Printf(fmt string, args ...interface{}) { | |
tl.Helper() | |
tl.Logf("\x1b[90m"+fmt+"\x1b[0m", args...) | |
} | |
func TestIt(t *testing.T) { | |
s := &Server{ | |
Router: chi.NewRouter(), | |
logger: &testlogger{TB: t}, | |
} | |
s.Mount() | |
ts := httptest.NewServer(s) | |
defer ts.Close() | |
res, err := http.Get(fmt.Sprintf("%s/500", ts.URL)) | |
if err != nil { | |
t.Fatalf("unexpected %v", err) | |
} | |
if res.StatusCode != 200 { | |
t.Errorf("want 200 but %d", res.StatusCode) | |
} | |
fmt.Println("----------------------------------------") | |
fmt.Print("response: ") | |
io.Copy(os.Stderr, res.Body) | |
fmt.Println("----------------------------------------") | |
defer res.Body.Close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment