mirror of
https://git.sr.ht/~yotam/shavit
synced 2024-06-13 05:06:06 +02:00
Move GeminiError and ErrorResponse to go-gemini
This commit is contained in:
parent
56b1971e99
commit
fe7db5a3ec
2
go.mod
2
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
|
||||
)
|
||||
|
|
2
go.sum
2
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=
|
||||
|
|
35
handler.go
35
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}
|
||||
|
|
Loading…
Reference in New Issue