diff options
author | Yotam Nachum <me@yotam.net> | 2019-11-02 14:51:43 +0200 |
---|---|---|
committer | Yotam Nachum <me@yotam.net> | 2019-11-02 14:51:43 +0200 |
commit | 46f84de73af65aaafb92b596830ee2570b87b47d (patch) | |
tree | e04476c4b7f3a32b7d9c737c00c6d832535f7711 | |
parent | Add a license (AGPLv3) (diff) | |
download | shavit-46f84de73af65aaafb92b596830ee2570b87b47d.tar.gz shavit-46f84de73af65aaafb92b596830ee2570b87b47d.zip |
Fix go vet and go lint warnings
-rw-r--r-- | config.go | 1 | ||||
-rw-r--r-- | handler.go | 24 | ||||
-rw-r--r-- | logger.go | 4 | ||||
-rw-r--r-- | main.go | 4 |
4 files changed, 20 insertions, 13 deletions
@@ -7,6 +7,7 @@ import ( "github.com/BurntSushi/toml" ) +// Config holds the main configuration data for the server type Config struct { SourceDir string `toml:"source"` TLSCert string `toml:"tls_certificate"` @@ -8,23 +8,26 @@ import ( "path/filepath" "strings" - "git.sr.ht/~yotam/go-gemini" + 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() } -type MainHandler struct { +// Handler is the main handler of the server +type Handler struct { source string } -func (h MainHandler) urlAbsPath(rawURL string) (string, error) { +func (h Handler) urlAbsPath(rawURL string) (string, error) { u, err := url.Parse(rawURL) if err != nil { return "", GeminiError{err, gemini.StatusBadRequest} @@ -42,7 +45,7 @@ func (h MainHandler) urlAbsPath(rawURL string) (string, error) { return itemPath, nil } -func (h MainHandler) isFile(path string) bool { +func (h Handler) isFile(path string) bool { fileInfo, err := os.Stat(path) if err != nil { return false @@ -51,7 +54,7 @@ func (h MainHandler) isFile(path string) bool { return fileInfo.Mode().IsRegular() } -func (h MainHandler) getFilePath(rawURL string) (string, error) { +func (h Handler) getFilePath(rawURL string) (string, error) { itemPath, err := h.urlAbsPath(rawURL) if err != nil { return "", err @@ -69,19 +72,20 @@ func (h MainHandler) getFilePath(rawURL string) (string, error) { return "", GeminiError{fmt.Errorf("File Not Found"), gemini.StatusNotFound} } -func (h MainHandler) errorResponse(err error) gemini.Response { +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{ge.Status, ge.Error(), nil} + return gemini.Response{Status: ge.Status, Meta: ge.Error(), Body: nil} } - return gemini.Response{gemini.StatusTemporaryFailure, err.Error(), nil} + return gemini.Response{Status: gemini.StatusTemporaryFailure, Meta: err.Error(), Body: nil} } -func (h MainHandler) Handle(r gemini.Request) gemini.Response { +// 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) @@ -94,5 +98,5 @@ func (h MainHandler) Handle(r gemini.Request) gemini.Response { return h.errorResponse(err) } - return gemini.Response{gemini.StatusSuccess, "text/gemini", file} + return gemini.Response{Status: gemini.StatusSuccess, Meta: "text/gemini", Body: file} } @@ -3,13 +3,15 @@ package main import ( "log" - "git.sr.ht/~yotam/go-gemini" + gemini "git.sr.ht/~yotam/go-gemini" ) +// LoggingHandler wrap a Gemini handler and log all the requsts and responses type LoggingHandler struct { handler gemini.Handler } +// Handle implement the gemini.Handler interface by logging each request and response func (h LoggingHandler) Handle(req gemini.Request) gemini.Response { log.Println("Received request for", req.URL) @@ -4,7 +4,7 @@ import ( "log" "path/filepath" - "git.sr.ht/~yotam/go-gemini" + gemini "git.sr.ht/~yotam/go-gemini" ) func main() { @@ -18,7 +18,7 @@ func main() { log.Fatal(err) } - handler := LoggingHandler{MainHandler{absSourceDir}} + handler := LoggingHandler{Handler{absSourceDir}} err = gemini.ListenAndServe("", cfg.TLSCert, cfg.TLSKey, handler) if err != nil { |