From 46f84de73af65aaafb92b596830ee2570b87b47d Mon Sep 17 00:00:00 2001 From: Yotam Nachum Date: Sat, 2 Nov 2019 14:51:43 +0200 Subject: [PATCH] Fix go vet and go lint warnings --- config.go | 1 + handler.go | 24 ++++++++++++++---------- logger.go | 4 +++- main.go | 4 ++-- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/config.go b/config.go index 6b24a9d..dc57040 100644 --- a/config.go +++ b/config.go @@ -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"` diff --git a/handler.go b/handler.go index aaab9eb..1f5fbea 100644 --- a/handler.go +++ b/handler.go @@ -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} } diff --git a/logger.go b/logger.go index 7ebccff..14891d2 100644 --- a/logger.go +++ b/logger.go @@ -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) diff --git a/main.go b/main.go index dae8d2c..f8b7b8a 100644 --- a/main.go +++ b/main.go @@ -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 {