Author: Adam Evyčędo <git@apiote.xyz>
password and shorts are in state dir
main.go | 8 ++++-- router.go | 62 +++++++++++++++++++++++++++++---------------------------
diff --git a/main.go b/main.go index 6941aa5028cd28a0d9ed003dde63eb249a637f8c..827ee63d47464b72346c93c391cb558ff8fe0e1f 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ "flag" "fmt" "io/ioutil" "math/rand" + "path/filepath" "time" ) @@ -12,7 +13,8 @@ func main() { rand.Seed(time.Now().UnixNano()) pass := flag.String("p", "", "password") - dataDir := flag.String("d", ".", "data directory") + dataDir := flag.String("d", "/usr/share/website", "data directory") + stateDir := flag.String("s", "/var/lib/website", "data directory") flag.Parse() if *pass != "" { @@ -21,10 +23,10 @@ if err != nil { fmt.Println(err) return } - ioutil.WriteFile("password", []byte(hash), 0600) + ioutil.WriteFile(filepath.Join(*stateDir, "password"), []byte(hash), 0600) fmt.Println("Password updated") return } - route(*dataDir) + route(*dataDir, *stateDir) } diff --git a/router.go b/router.go index 688cbf7200a4034dd955da54828235914f2704a2..d90ba8d040ec0ea9b3c7cd176dbb298bd1984bda 100644 --- a/router.go +++ b/router.go @@ -459,7 +459,7 @@ } } } -func shortRedirect(dataDir string) func(w http.ResponseWriter, r *http.Request) { +func shortRedirect(dataDir, stateDir string) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { acceptLanguage := r.Header.Get("Accept-Language") path := strings.Split(r.URL.Path[1:], "/") @@ -485,7 +485,7 @@ if url == "" { renderStatus(w, 400, acceptLanguage) return } - hash, err := ioutil.ReadFile("password") + hash, err := ioutil.ReadFile(filepath.Join(stateDir, "password")) if err != nil { log.Println(err) fmt.Fprint(w, "Error 500") @@ -504,7 +504,7 @@ } // todo check duplicates - file, err := os.OpenFile(filepath.Join(dataDir, "s.toml"), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) + file, err := os.OpenFile(filepath.Join(stateDir, "s.toml"), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) defer file.Close() if err != nil { log.Println(err) @@ -525,7 +525,7 @@ } else if len(path) != 2 { renderStatus(w, 404, acceptLanguage) } else { id := strings.ToUpper(path[1]) - data, err := ioutil.ReadFile("s.toml") + data, err := ioutil.ReadFile(filepath.Join(stateDir, "s.toml")) if err != nil { log.Println(err) fmt.Fprint(w, "Error 500") @@ -548,32 +548,34 @@ } } } -func shortShow(w http.ResponseWriter, r *http.Request) { - acceptLanguage := r.Header.Get("Accept-Language") - path := strings.Split(r.URL.Path[1:], "/") - if len(path) == 2 && path[1] != "" { - id := strings.ToUpper(path[1]) - data, err := ioutil.ReadFile("s.toml") - if err != nil { - log.Println(err) - fmt.Fprint(w, "Error 500") - return - } - for _, line := range strings.Split(string(data), "\n") { - if line == "" { - break - } - redirection := strings.Split(line, "=") - redirectionID := strings.Trim(redirection[0], " ") - redirectionURL := strings.Trim(redirection[1], " ") - if redirectionID == id { - showHtml(w, "short", Redirection{redirectionID, redirectionURL, "https://apiote.xyz/short/" + redirectionID}, acceptLanguage) +func shortShow(stateDir string) func(w http.ResponseWriter, r *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + acceptLanguage := r.Header.Get("Accept-Language") + path := strings.Split(r.URL.Path[1:], "/") + if len(path) == 2 && path[1] != "" { + id := strings.ToUpper(path[1]) + data, err := ioutil.ReadFile(filepath.Join(stateDir, "s.toml")) + if err != nil { + log.Println(err) + fmt.Fprint(w, "Error 500") return } + for _, line := range strings.Split(string(data), "\n") { + if line == "" { + break + } + redirection := strings.Split(line, "=") + redirectionID := strings.Trim(redirection[0], " ") + redirectionURL := strings.Trim(redirection[1], " ") + if redirectionID == id { + showHtml(w, "short", Redirection{redirectionID, redirectionURL, "https://apiote.xyz/short/" + redirectionID}, acceptLanguage) + return + } + } + renderStatus(w, 404, acceptLanguage) + } else { + renderStatus(w, 404, acceptLanguage) } - renderStatus(w, 404, acceptLanguage) - } else { - renderStatus(w, 404, acceptLanguage) } } @@ -608,7 +610,7 @@ acceptLanguage := r.Header.Get("Accept-Language") showHtml(w, "dogtag", Nil{"https://apiote.xyz/tag"}, acceptLanguage) } -func route(dataDir string) { +func route(dataDir, stateDir string) { http.HandleFunc("/", index) http.Handle("/static/", http.FileServer(http.FS(staticFS))) @@ -622,8 +624,8 @@ http.HandleFunc("/pgp/", pgp) http.HandleFunc("/calendar", calendar(dataDir)) http.HandleFunc("/tag", dogtag) - http.HandleFunc("/s/", shortRedirect(dataDir)) - http.HandleFunc("/short/", shortShow) + http.HandleFunc("/s/", shortRedirect(dataDir, stateDir)) + http.HandleFunc("/short/", shortShow(stateDir)) http.HandleFunc("/.well-known/openpgpkey/hu/", wkd)