-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e99ade
commit 1ed585a
Showing
7 changed files
with
201 additions
and
56 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package captcha | ||
|
||
type Config struct { | ||
Enabled bool | ||
InsertSecret string | ||
ExpireSecs int | ||
Recaptcha RecaptchaConfig | ||
Redis RedisConfig | ||
} | ||
|
||
type RecaptchaConfig struct { | ||
SiteKey string | ||
Secret string | ||
} | ||
|
||
// See https://godoc.org/github.com/go-redis/redis#Options | ||
type RedisConfig struct { | ||
Network string | ||
Addr string | ||
Password string | ||
DB int | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package page | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
type Context interface { | ||
Request() *http.Request | ||
} | ||
|
||
type context struct { | ||
req *http.Request | ||
} | ||
|
||
func newContext(req *http.Request) (*context, error) { | ||
return &context{ | ||
req: req, | ||
}, nil | ||
} | ||
|
||
func (c *context) Request() *http.Request { | ||
return c.req | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package page | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/ptt/pttweb/pttbbs" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/codes" | ||
) | ||
|
||
func setCommonResponseHeaders(w http.ResponseWriter) { | ||
h := w.Header() | ||
h.Set("Server", "Cryophoenix") | ||
h.Set("Content-Type", "text/html; charset=utf-8") | ||
} | ||
|
||
type ErrorWrapper func(Context, http.ResponseWriter) error | ||
|
||
func (fn ErrorWrapper) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
setCommonResponseHeaders(w) | ||
|
||
if err := clarifyRemoteError(handleRequest(w, r, fn)); err != nil { | ||
if pg, ok := err.(Page); ok { | ||
if err = ExecutePage(w, pg); err != nil { | ||
log.Println("Failed to emit error page:", err) | ||
} | ||
return | ||
} | ||
internalError(w, err) | ||
} | ||
} | ||
|
||
func clarifyRemoteError(err error) error { | ||
if err == pttbbs.ErrNotFound { | ||
return newNotFoundError(err) | ||
} | ||
|
||
switch grpc.Code(err) { | ||
case codes.NotFound, codes.PermissionDenied: | ||
return newNotFoundError(err) | ||
} | ||
|
||
return err | ||
} | ||
|
||
func internalError(w http.ResponseWriter, err error) { | ||
log.Println(err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
ExecutePage(w, &Error{ | ||
Title: `500 - Internal Server Error`, | ||
ContentHtml: `500 - Internal Server Error / Server Too Busy.`, | ||
}) | ||
} | ||
|
||
func handleRequest(w http.ResponseWriter, r *http.Request, f func(Context, http.ResponseWriter) error) error { | ||
ctx, err := newContext(r) | ||
if err != nil { | ||
return err | ||
} | ||
return f(ctx, w) | ||
} | ||
|
||
type NotFoundError struct { | ||
NotFound | ||
UnderlyingErr error | ||
} | ||
|
||
func (e *NotFoundError) Error() string { | ||
return fmt.Sprintf("not found error page: %v", e.UnderlyingErr) | ||
} | ||
|
||
func newNotFoundError(err error) *NotFoundError { | ||
return &NotFoundError{ | ||
UnderlyingErr: err, | ||
} | ||
} |
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