Skip to content

Commit

Permalink
refactor(cmd/web): 统一终端输出
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Nov 22, 2023
1 parent 8809148 commit 9a8deb4
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 43 deletions.
12 changes: 3 additions & 9 deletions cmd/web/locale/locale.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import (
"github.com/issue9/localeutil/message/extract"
"github.com/issue9/localeutil/message/serialize"
"github.com/issue9/web"
"github.com/issue9/web/logs"
"golang.org/x/text/language"
"gopkg.in/yaml.v3"

"github.com/issue9/web/cmd/web/termlog"
)

const (
Expand Down Expand Up @@ -49,14 +50,7 @@ func Init(opt *cmdopt.CmdOpt, p *localeutil.Printer) {
funcs := fs.String("func", defaultFuncs, funcs.LocaleString(p))
skip := fs.Bool("m", true, skipMod.LocaleString(p))

log, err := logs.New(nil, &logs.Options{
Levels: logs.AllLevels(),
Handler: logs.NewTermHandler(os.Stdout, nil),
Created: logs.NanoLayout,
})
if err != nil {
panic(err)
}
log := termlog.New(p, os.Stdout)

return func(io.Writer) error {
u, ext, err := GetMarshalByExt(*f)
Expand Down
11 changes: 2 additions & 9 deletions cmd/web/locale/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (
"github.com/issue9/localeutil/message"
"github.com/issue9/localeutil/message/serialize"
"github.com/issue9/web"
"github.com/issue9/web/logs"
"gopkg.in/yaml.v3"

"github.com/issue9/web/cmd/web/locale"
"github.com/issue9/web/cmd/web/termlog"
)

const (
Expand All @@ -38,14 +38,7 @@ func Init(opt *cmdopt.CmdOpt, p *localeutil.Printer) {
dest := fs.String("dest", "", destUsage.LocaleString(p))

return func(io.Writer) error {
log, err := logs.New(nil, &logs.Options{
Levels: logs.AllLevels(),
Handler: logs.NewTermHandler(os.Stdout, nil),
Created: logs.NanoLayout,
})
if err != nil {
panic(err)
}
log := termlog.New(p, os.Stdout)

srcMsg, err := getSrc(*src)
if err != nil {
Expand Down
11 changes: 2 additions & 9 deletions cmd/web/restdoc/restdoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import (
"github.com/issue9/localeutil"
"github.com/issue9/source"
"github.com/issue9/web"
"github.com/issue9/web/logs"
"golang.org/x/mod/modfile"

"github.com/issue9/web/cmd/web/restdoc/logger"
"github.com/issue9/web/cmd/web/restdoc/parser"
"github.com/issue9/web/cmd/web/termlog"
)

const (
Expand All @@ -48,14 +48,7 @@ func Init(opt *cmdopt.CmdOpt, p *localeutil.Printer) {

return func(io.Writer) error {
ctx := context.Background()
ls, err := logs.New(p, &logs.Options{
Levels: logs.AllLevels(),
Handler: logs.NewTermHandler(os.Stdout, nil),
Created: logs.NanoLayout,
})
if err != nil {
return err
}
ls := termlog.New(p, os.Stdout)

var tags []string
if *t != "" {
Expand Down
27 changes: 27 additions & 0 deletions cmd/web/termlog/termlog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: MIT

// Package termlog 统一的终端日志类型
package termlog

import (
"io"

"github.com/issue9/localeutil"
"github.com/issue9/web/logs"
)

// New 声明用于终端输出的日志
//
// 如果初始化时出错,则会直接 Panic。
func New(p *localeutil.Printer, out io.Writer) logs.Logs {
log, err := logs.New(p, &logs.Options{
Levels: logs.AllLevels(),
Handler: logs.NewTermHandler(out, nil),
Created: logs.NanoLayout,
})
if err != nil {
panic(err)
}

return log
}
18 changes: 18 additions & 0 deletions cmd/web/termlog/termlog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: MIT

package termlog

import (
"os"
"testing"

"github.com/issue9/assert/v3"
"golang.org/x/text/language"
"golang.org/x/text/message"
)

func TestNew(t *testing.T) {
a := assert.New(t, false)
a.NotNil(New(nil, os.Stdout))
a.NotNil(New(message.NewPrinter(language.SimplifiedChinese), os.Stdout))
}
2 changes: 1 addition & 1 deletion cmd/web/watch/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Init(opt *cmdopt.CmdOpt, p *localeutil.Printer) {
watch.App: web.StringPhrase("watch.app").LocaleString(p),
}

return func(w io.Writer) error {
return func(io.Writer) error {
f, err := time.ParseDuration(*freq)
if err != nil {
return err
Expand Down
18 changes: 9 additions & 9 deletions server/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ func BenchmarkHTTPServer_Serve(b *testing.B) {
a := assert.New(b, false)
for i := 0; i < b.N; i++ {
r := servertest.Get(a, "http://localhost:8080/path").
Header("Content-type", header.BuildContentType("application/json", "gbk")).
Header("accept", "application/json").
Header("accept-charset", "gbk;q=1,gb18080;q=0.1").
Header(header.ContentType, header.BuildContentType("application/json", "gbk")).
Header(header.Accept, "application/json").
Header(header.AcceptCharset, "gbk;q=1,gb18080;q=0.1").
Request()
resp, err := http.DefaultClient.Do(r)
a.NotError(err).NotNil(resp)
Expand All @@ -66,10 +66,10 @@ func BenchmarkHTTPServer_Serve(b *testing.B) {
a := assert.New(b, false)
for i := 0; i < b.N; i++ {
r := servertest.Get(a, "http://localhost:8080/path").
Header("Content-type", header.BuildContentType("application/json", "gbk")).
Header("accept", "application/json").
Header("accept-charset", "gbk;q=1,gb18080;q=0.1").
Header("accept-encoding", "gzip").
Header(header.ContentType, header.BuildContentType("application/json", "gbk")).
Header(header.Accept, "application/json").
Header(header.AcceptCharset, "gbk;q=1,gb18080;q=0.1").
Header(header.AcceptEncoding, "gzip").
Request()
resp, err := http.DefaultClient.Do(r)
a.NotError(err).NotNil(resp)
Expand All @@ -83,8 +83,8 @@ func BenchmarkHTTPServer_Serve(b *testing.B) {
a := assert.New(b, false)
for i := 0; i < b.N; i++ {
r := servertest.Get(a, "http://localhost:8080/path").
Header("Content-type", header.BuildContentType("application/json", header.UTF8Name)).
Header("accept", "application/json").
Header(header.ContentType, header.BuildContentType("application/json", header.UTF8Name)).
Header(header.Accept, "application/json").
Request()
resp, err := http.DefaultClient.Do(r)
a.NotError(err).NotNil(resp)
Expand Down
10 changes: 5 additions & 5 deletions server/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ func TestServer_Routers(t *testing.T) {

r1.Prefix("/p1").Delete("/path", buildHandler(http.StatusCreated))
servertest.Delete(a, "http://localhost:8080/p1/path").
Header("Accept", "application/json;v=2").
Header(header.Accept, "application/json;v=2").
Do(nil).
Status(http.StatusCreated)
servertest.NewRequest(a, http.MethodOptions, "http://localhost:8080/p1/path").
Header("Accept", "application/json;v=2").
Header(header.Accept, "application/json;v=2").
Do(nil).Status(http.StatusOK)

r2 := srv.GetRouter("ver")
Expand All @@ -65,7 +65,7 @@ func TestServer_Routers(t *testing.T) {
srv.RemoveRouter("ver")
a.Equal(0, len(srv.Routers()))
servertest.Delete(a, "http://localhost:8080/p1/path").
Header("Accept", "application/json;v=2").
Header(header.Accept, "application/json;v=2").
Do(nil).
Status(http.StatusNotFound)
}
Expand Down Expand Up @@ -94,15 +94,15 @@ func TestMiddleware(t *testing.T) {
defer srv.Close(500 * time.Millisecond)

servertest.Get(a, "http://localhost:8080/p1/path").
Header("accept", "application/json").
Header(header.Accept, "application/json").
Do(nil).
Status(http.StatusCreated).
Header("h", "p1p2-b1b2-").
StringBody("201")
a.Equal(count, 1)

servertest.Get(a, "http://localhost:8080/path").
Header("accept", "application/json").
Header(header.Accept, "application/json").
Do(nil).
Status(http.StatusCreated).
Header("h", "b1b2-").
Expand Down
2 changes: 1 addition & 1 deletion web.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

// Version 当前框架的版本
const Version = "0.86.0"
const Version = "0.86.2"

// 服务的几种状态
const (
Expand Down

0 comments on commit 9a8deb4

Please sign in to comment.