Skip to content

Commit

Permalink
simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Jul 7, 2023
1 parent 7f55907 commit 9a5483e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 12 deletions.
6 changes: 5 additions & 1 deletion cmd/serve/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"log"
"net/http"

"github.com/go-rod/bartender"
)
Expand All @@ -19,5 +20,8 @@ func main() {

log.Printf("Bartender started %s -> %s\n", *port, *target)

bartender.New(*port, *target).Serve()
err := http.ListenAndServe(*port, bartender.New(*port, *target))
if err != nil {
log.Fatalln(err)
}
}
11 changes: 1 addition & 10 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,7 @@ func New(addr, target string) *Bartender {
}
}

func (b *Bartender) Serve() {
http.HandleFunc("/", b.Handler)

err := http.ListenAndServe(b.addr, nil)
if err != nil {
panic(err)
}
}

func (b *Bartender) Handler(w http.ResponseWriter, r *http.Request) {
func (b *Bartender) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet && r.Header.Get("Accept-Language") == "" {
b.RenderPage(w, r)

Expand Down
2 changes: 1 addition & 1 deletion service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestBasic(t *testing.T) {

bt := bartender.New("", website.URL())

proxy.Mux.HandleFunc("/", bt.Handler)
proxy.Mux.HandleFunc("/", bt.ServeHTTP)

{
res := g.Req("", proxy.URL("/test?q=ok"), http.Header{"Accept-Language": []string{"en"}})
Expand Down

0 comments on commit 9a5483e

Please sign in to comment.