Skip to content

Commit

Permalink
Merge pull request #15 from kurusugawa-computer/fix/request-favicon
Browse files Browse the repository at this point in the history
404ハンドラーの追加
  • Loading branch information
KawaiKenta authored Oct 1, 2024
2 parents 88b02f6 + 8081703 commit 5232fd0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/kurusugawa-computer/freee-go

go 1.21.6

require github.com/go-chi/chi/v5 v5.1.0 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
48 changes: 31 additions & 17 deletions oauth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"net/url"
"strconv"
"strings"

"github.com/go-chi/chi/v5"
)

type opt struct {
Expand Down Expand Up @@ -79,31 +81,43 @@ func Authorize(clientID string, port int, opts ...OptFunc) (string, error) {
}
resultCh := make(chan Result)
defer close(resultCh)

go func() {
shutdownCh := make(chan struct{})
defer close(shutdownCh)
server := &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()

tAuthorizationCode := q.Get("code")
authHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()

tAuthorizationCode := q.Get("code")

var err error
if q.Get("state") != state {
tAuthorizationCode = ""
err = errors.New("state mismatch")
}

if q.Get("error") != "" {
err = errors.New(q.Get("error_description"))
}

var err error
if q.Get("state") != state {
tAuthorizationCode = ""
err = errors.New("state mismatch")
}
o.Renderer(w, tAuthorizationCode, err)

if q.Get("error") != "" {
err = errors.New(q.Get("error_description"))
}
resultCh <- Result{tAuthorizationCode, err}
shutdownCh <- struct{}{}
})

o.Renderer(w, tAuthorizationCode, err)
notFoundHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Not Found", http.StatusNotFound)
})

resultCh <- Result{tAuthorizationCode, err}
shutdownCh <- struct{}{}
}),
mux := chi.NewRouter()
mux.Handle("/", authHandler)
mux.Handle("/*", notFoundHandler)

server := &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: mux,
}
go func() {
<-shutdownCh
Expand Down

0 comments on commit 5232fd0

Please sign in to comment.