mirror of
https://git.sr.ht/~yotam/shavit
synced 2024-11-26 10:23:47 +01:00
Fix go vet and go lint warnings
This commit is contained in:
parent
801060d3ae
commit
46f84de73a
@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Config holds the main configuration data for the server
|
||||||
type Config struct {
|
type Config struct {
|
||||||
SourceDir string `toml:"source"`
|
SourceDir string `toml:"source"`
|
||||||
TLSCert string `toml:"tls_certificate"`
|
TLSCert string `toml:"tls_certificate"`
|
||||||
|
24
handler.go
24
handler.go
@ -8,23 +8,26 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"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 {
|
type GeminiError struct {
|
||||||
Err error
|
Err error
|
||||||
Status int
|
Status int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Error return the string of the inner error to fulfill the error interface
|
||||||
func (e GeminiError) Error() string {
|
func (e GeminiError) Error() string {
|
||||||
return e.Err.Error()
|
return e.Err.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
type MainHandler struct {
|
// Handler is the main handler of the server
|
||||||
|
type Handler struct {
|
||||||
source string
|
source string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h MainHandler) urlAbsPath(rawURL string) (string, error) {
|
func (h Handler) urlAbsPath(rawURL string) (string, error) {
|
||||||
u, err := url.Parse(rawURL)
|
u, err := url.Parse(rawURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", GeminiError{err, gemini.StatusBadRequest}
|
return "", GeminiError{err, gemini.StatusBadRequest}
|
||||||
@ -42,7 +45,7 @@ func (h MainHandler) urlAbsPath(rawURL string) (string, error) {
|
|||||||
return itemPath, nil
|
return itemPath, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h MainHandler) isFile(path string) bool {
|
func (h Handler) isFile(path string) bool {
|
||||||
fileInfo, err := os.Stat(path)
|
fileInfo, err := os.Stat(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
@ -51,7 +54,7 @@ func (h MainHandler) isFile(path string) bool {
|
|||||||
return fileInfo.Mode().IsRegular()
|
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)
|
itemPath, err := h.urlAbsPath(rawURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@ -69,19 +72,20 @@ func (h MainHandler) getFilePath(rawURL string) (string, error) {
|
|||||||
return "", GeminiError{fmt.Errorf("File Not Found"), gemini.StatusNotFound}
|
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 {
|
if err == nil {
|
||||||
panic("nil error is not a valid parameter")
|
panic("nil error is not a valid parameter")
|
||||||
}
|
}
|
||||||
|
|
||||||
if ge, ok := err.(GeminiError); ok {
|
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)
|
itemPath, err := h.getFilePath(r.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return h.errorResponse(err)
|
return h.errorResponse(err)
|
||||||
@ -94,5 +98,5 @@ func (h MainHandler) Handle(r gemini.Request) gemini.Response {
|
|||||||
return h.errorResponse(err)
|
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 (
|
import (
|
||||||
"log"
|
"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 {
|
type LoggingHandler struct {
|
||||||
handler gemini.Handler
|
handler gemini.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle implement the gemini.Handler interface by logging each request and response
|
||||||
func (h LoggingHandler) Handle(req gemini.Request) gemini.Response {
|
func (h LoggingHandler) Handle(req gemini.Request) gemini.Response {
|
||||||
log.Println("Received request for", req.URL)
|
log.Println("Received request for", req.URL)
|
||||||
|
|
||||||
|
4
main.go
4
main.go
@ -4,7 +4,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"git.sr.ht/~yotam/go-gemini"
|
gemini "git.sr.ht/~yotam/go-gemini"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -18,7 +18,7 @@ func main() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
handler := LoggingHandler{MainHandler{absSourceDir}}
|
handler := LoggingHandler{Handler{absSourceDir}}
|
||||||
|
|
||||||
err = gemini.ListenAndServe("", cfg.TLSCert, cfg.TLSKey, handler)
|
err = gemini.ListenAndServe("", cfg.TLSCert, cfg.TLSKey, handler)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user