aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYotam Nachum <me@yotam.net>2019-11-09 22:30:59 +0200
committerYotam Nachum <me@yotam.net>2019-11-09 22:30:59 +0200
commitfe7db5a3ec6a994144f0ede6a9c2b7ee99266be4 (patch)
tree713d780b0f56bc9622d245b6b8e08bd1dc216eeb
parentMove logic into configuration parsing (diff)
downloadshavit-fe7db5a3ec6a994144f0ede6a9c2b7ee99266be4.tar.gz
shavit-fe7db5a3ec6a994144f0ede6a9c2b7ee99266be4.zip
Move GeminiError and ErrorResponse to go-gemini
-rw-r--r--go.mod2
-rw-r--r--go.sum2
-rw-r--r--handler.go35
3 files changed, 9 insertions, 30 deletions
diff --git a/go.mod b/go.mod
index 64752a3..433b80b 100644
--- a/go.mod
+++ b/go.mod
@@ -3,7 +3,7 @@ module git.sr.ht/~yotam/go-gemini-server
go 1.12
require (
- git.sr.ht/~yotam/go-gemini v0.0.0-20191101110121-e84e0b63cd37
+ git.sr.ht/~yotam/go-gemini v0.0.0-20191109201753-fe15cf054c37
github.com/BurntSushi/toml v0.3.1
github.com/pelletier/go-toml v1.6.0 // indirect
)
diff --git a/go.sum b/go.sum
index 3faa810..cb4d0b9 100644
--- a/go.sum
+++ b/go.sum
@@ -1,5 +1,7 @@
git.sr.ht/~yotam/go-gemini v0.0.0-20191101110121-e84e0b63cd37 h1:NWDz215k8nz6PGmbCb1YsUWrp9ocqnyKLnUoDCzzgU8=
git.sr.ht/~yotam/go-gemini v0.0.0-20191101110121-e84e0b63cd37/go.mod h1:KxQlipD0Ti7MfV3itYJfuvgcvd+SOlRTtbOK+A0DCCE=
+git.sr.ht/~yotam/go-gemini v0.0.0-20191109201753-fe15cf054c37 h1:ga6iPmFGpSgIHAc3cxxqOksOyP14FcP2mQz86AF5q8k=
+git.sr.ht/~yotam/go-gemini v0.0.0-20191109201753-fe15cf054c37/go.mod h1:KxQlipD0Ti7MfV3itYJfuvgcvd+SOlRTtbOK+A0DCCE=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
diff --git a/handler.go b/handler.go
index 46dfd9b..bba9955 100644
--- a/handler.go
+++ b/handler.go
@@ -11,17 +11,6 @@ import (
gemini "git.sr.ht/~yotam/go-gemini"
)
-// GeminiError wrap the standard Go error with a Gemini status code
-type GeminiError struct {
- Err error
- Status int
-}
-
-// Error return the string of the inner error to fulfill the error interface
-func (e GeminiError) Error() string {
- return e.Err.Error()
-}
-
// Handler is the main handler of the server
type Handler struct {
cfg Config
@@ -30,16 +19,16 @@ type Handler struct {
func (h Handler) urlAbsPath(rawURL string) (string, error) {
u, err := url.Parse(rawURL)
if err != nil {
- return "", GeminiError{err, gemini.StatusBadRequest}
+ return "", gemini.Error{Err: err, Status: gemini.StatusBadRequest}
}
itemPath, err := filepath.Abs(filepath.Join(h.cfg.SourceDir, u.Path))
if err != nil {
- return "", GeminiError{err, gemini.StatusTemporaryFailure}
+ return "", gemini.Error{Err: err, Status: gemini.StatusTemporaryFailure}
}
if !strings.HasPrefix(itemPath, h.cfg.SourceDir) {
- return "", GeminiError{fmt.Errorf("Permission Denied"), gemini.StatusBadRequest}
+ return "", gemini.Error{Err: fmt.Errorf("Permission Denied"), Status: gemini.StatusBadRequest}
}
return itemPath, nil
@@ -69,33 +58,21 @@ func (h Handler) getFilePath(rawURL string) (string, error) {
return indexPath, nil
}
- return "", GeminiError{fmt.Errorf("File Not Found"), gemini.StatusNotFound}
-}
-
-func (h Handler) errorResponse(err error) gemini.Response {
- if err == nil {
- panic("nil error is not a valid parameter")
- }
-
- if ge, ok := err.(GeminiError); ok {
- return gemini.Response{Status: ge.Status, Meta: ge.Error(), Body: nil}
- }
-
- return gemini.Response{Status: gemini.StatusTemporaryFailure, Meta: err.Error(), Body: nil}
+ return "", gemini.Error{Err: fmt.Errorf("File Not Found"), Status: gemini.StatusNotFound}
}
// Handle implement the gemini.Handler interface by serving files from a given source directory
func (h Handler) Handle(r gemini.Request) gemini.Response {
itemPath, err := h.getFilePath(r.URL)
if err != nil {
- return h.errorResponse(err)
+ return gemini.ErrorResponse(err)
}
log.Println("Serving file from", itemPath)
file, err := os.Open(itemPath)
if err != nil {
- return h.errorResponse(err)
+ return gemini.ErrorResponse(err)
}
return gemini.Response{Status: gemini.StatusSuccess, Meta: "text/gemini", Body: file}