aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYotam Nachum <me@yotam.net>2019-11-09 22:05:48 +0200
committerYotam Nachum <me@yotam.net>2019-11-09 22:05:48 +0200
commit56b1971e9945bc8573efe251686084e60bf73f49 (patch)
treede61e2ee8672f214582f0ef277f627cb94169c58
parentAdd CI with linting, testing and annotations (diff)
downloadshavit-56b1971e9945bc8573efe251686084e60bf73f49.tar.gz
shavit-56b1971e9945bc8573efe251686084e60bf73f49.zip
Move logic into configuration parsing
-rw-r--r--config.go8
-rw-r--r--handler.go6
-rw-r--r--main.go9
3 files changed, 11 insertions, 12 deletions
diff --git a/config.go b/config.go
index dc57040..21dff3d 100644
--- a/config.go
+++ b/config.go
@@ -3,6 +3,7 @@ package main
import (
"fmt"
"io/ioutil"
+ "path/filepath"
"github.com/BurntSushi/toml"
)
@@ -23,7 +24,12 @@ func getConfig(path string) (Config, error) {
var cfg Config
err = toml.Unmarshal(raw, &cfg)
if err != nil {
- return Config{}, fmt.Errorf("failed to parse config file: %v", err)
+ return cfg, fmt.Errorf("failed to parse config file: %v", err)
+ }
+
+ cfg.SourceDir, err = filepath.Abs(cfg.SourceDir)
+ if err != nil {
+ return cfg, fmt.Errorf("failed to get absolute source dir: %v", err)
}
return cfg, nil
diff --git a/handler.go b/handler.go
index 1f5fbea..46dfd9b 100644
--- a/handler.go
+++ b/handler.go
@@ -24,7 +24,7 @@ func (e GeminiError) Error() string {
// Handler is the main handler of the server
type Handler struct {
- source string
+ cfg Config
}
func (h Handler) urlAbsPath(rawURL string) (string, error) {
@@ -33,12 +33,12 @@ func (h Handler) urlAbsPath(rawURL string) (string, error) {
return "", GeminiError{err, gemini.StatusBadRequest}
}
- itemPath, err := filepath.Abs(filepath.Join(h.source, u.Path))
+ itemPath, err := filepath.Abs(filepath.Join(h.cfg.SourceDir, u.Path))
if err != nil {
return "", GeminiError{err, gemini.StatusTemporaryFailure}
}
- if !strings.HasPrefix(itemPath, h.source) {
+ if !strings.HasPrefix(itemPath, h.cfg.SourceDir) {
return "", GeminiError{fmt.Errorf("Permission Denied"), gemini.StatusBadRequest}
}
diff --git a/main.go b/main.go
index f8b7b8a..79a4758 100644
--- a/main.go
+++ b/main.go
@@ -2,7 +2,6 @@ package main
import (
"log"
- "path/filepath"
gemini "git.sr.ht/~yotam/go-gemini"
)
@@ -13,13 +12,7 @@ func main() {
log.Fatal(err)
}
- absSourceDir, err := filepath.Abs(cfg.SourceDir)
- if err != nil {
- log.Fatal(err)
- }
-
- handler := LoggingHandler{Handler{absSourceDir}}
-
+ handler := LoggingHandler{Handler{cfg}}
err = gemini.ListenAndServe("", cfg.TLSCert, cfg.TLSKey, handler)
if err != nil {
log.Fatal(err)