Skip to content

Commit

Permalink
refactor: 统一相似代码
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Dec 17, 2021
1 parent 2e7bbf5 commit e67439c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 43 deletions.
28 changes: 0 additions & 28 deletions internal/cmd/console/response.go

This file was deleted.

36 changes: 36 additions & 0 deletions internal/cmd/console/visiter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: MIT

package console

import (
"net/http"

"golang.org/x/text/message"
)

type visiterResponse struct {
http.ResponseWriter
status int
}

func Visiter(next http.Handler, p *message.Printer, succ, erro *Logger) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ww := &visiterResponse{ResponseWriter: w}
next.ServeHTTP(ww, r)
ww.printLog(p, r.URL.String(), succ, erro)
})
}

func (r *visiterResponse) WriteHeader(status int) {
r.status = status
r.ResponseWriter.WriteHeader(status)
}

func (r *visiterResponse) printLog(p *message.Printer, url string, succ, erro *Logger) {
msg := p.Sprintf("visit url", r.status, url)
if r.status > 399 {
erro.Println(msg)
} else {
succ.Println(msg)
}
}
7 changes: 1 addition & 6 deletions internal/cmd/preview/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,7 @@ func (o *options) watch(succ, info, erro *console.Logger) error {
BaseURL: o.url,
}

httpServer := o.b.Handler(erro.AsLogger())
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ww := &console.Response{ResponseWriter: w}
httpServer.ServeHTTP(ww, r)
ww.WriteVisitLog(o.p, r.URL.String(), succ, erro)
})
h := console.Visiter(o.b.Handler(erro.AsLogger()), o.p, succ, erro)
o.srv = &http.Server{Addr: o.addr, Handler: http.StripPrefix(o.path, h)}

go func() {
Expand Down
8 changes: 1 addition & 7 deletions internal/cmd/serve/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,8 @@ func (o *options) serve(succ, info, erro *console.Logger) error {
return err
}

httpServer := o.b.Handler(erro.AsLogger())
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ww := &console.Response{ResponseWriter: w}
httpServer.ServeHTTP(ww, r)
ww.WriteVisitLog(o.p, r.URL.String(), succ, erro)
})

mux := http.NewServeMux()
h := console.Visiter(o.b.Handler(erro.AsLogger()), o.p, succ, erro)
mux.Handle(o.path, http.StripPrefix(o.path, h))
if o.hookURL != "" {
mux.Handle(o.hookURL, http.HandlerFunc(o.webhook))
Expand Down
2 changes: 1 addition & 1 deletion internal/filesystem/fileserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ func printError(erro *log.Logger, err error, w http.ResponseWriter) {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
case err != nil:
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
erro.Println(err)
}
erro.Println(err)
}
3 changes: 2 additions & 1 deletion internal/filesystem/fileserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package filesystem

import (
"log"
"net/http"
"os"
"testing"
Expand All @@ -14,7 +15,7 @@ import (
func TestFileServer(t *testing.T) {
a := assert.New(t, false)

fs := FileServer(os.DirFS("./"), nil)
fs := FileServer(os.DirFS("./"), log.Default())
a.NotNil(fs)
http.Handle("/assets/", http.StripPrefix("/assets/", fs))

Expand Down

0 comments on commit e67439c

Please sign in to comment.