From 7b2423ec40d1ec0764fedab3d146fb0819e1c3c6 Mon Sep 17 00:00:00 2001 From: handlerug Date: Sat, 19 Jun 2021 11:51:10 +0700 Subject: [PATCH] New structure --- cfg/config.go | 8 --- files/files.go | 119 ++++++++++++++--------------------------- flag.go | 3 +- history/history.go | 6 +-- history/information.go | 6 ++- main.go | 18 ++++--- shroom/upload.go | 10 ++-- user/files.go | 2 +- util/util.go | 8 +-- web/stuff.go | 9 ++-- 10 files changed, 74 insertions(+), 115 deletions(-) diff --git a/cfg/config.go b/cfg/config.go index 786fa1c..4cb0e50 100644 --- a/cfg/config.go +++ b/cfg/config.go @@ -25,9 +25,7 @@ var ( GeminiCertificatePath string UseFixedAuth bool - FixedAuthCredentialsPath string UseRegistration bool - RegistrationCredentialsPath string LimitRegistration int OmnipresentScripts []string @@ -80,10 +78,8 @@ type CustomScripts struct { // Authorization is a section of Config that has fields related to authorization and authentication. type Authorization struct { UseFixedAuth bool - FixedAuthCredentialsPath string UseRegistration bool - RegistrationCredentialsPath string LimitRegistration uint64 } @@ -106,10 +102,8 @@ func ReadConfigFile() { }, Authorization: Authorization{ UseFixedAuth: false, - FixedAuthCredentialsPath: "", UseRegistration: false, - RegistrationCredentialsPath: "", LimitRegistration: 0, }, CustomScripts: CustomScripts{ @@ -142,9 +136,7 @@ func ReadConfigFile() { URL = cfg.URL GeminiCertificatePath = cfg.GeminiCertificatePath UseFixedAuth = cfg.UseFixedAuth - FixedAuthCredentialsPath = cfg.FixedAuthCredentialsPath UseRegistration = cfg.UseRegistration - RegistrationCredentialsPath = cfg.RegistrationCredentialsPath LimitRegistration = int(cfg.LimitRegistration) OmnipresentScripts = cfg.OmnipresentScripts ViewScripts = cfg.ViewScripts diff --git a/files/files.go b/files/files.go index 740041b..5ba9b42 100644 --- a/files/files.go +++ b/files/files.go @@ -2,109 +2,68 @@ package files import ( - "errors" - "fmt" - "github.com/bouncepaw/mycorrhiza/cfg" - "path/filepath" - "strings" + "os" + "path" - "github.com/adrg/xdg" - "github.com/mitchellh/go-homedir" + "github.com/bouncepaw/mycorrhiza/cfg" ) var paths struct { + gitRepo string + cacheDir string + staticFiles string tokensJSON string registrationCredentialsJSON string fixedCredentialsJSON string } -// TokensJSON returns a path to the JSON file where users' tokens are stored. -// -// Default path: $XDG_DATA_HOME/mycorrhiza/tokens.json +// HyphaeDir returns the path to hyphae storage. +// A separate function is needed to easily know where a general storage path is +// needed rather than a concrete Git or the whole wiki storage path, so that we +// could easily refactor things later if we'll ever support different storages. +func HyphaeDir() string { return paths.gitRepo } + +// GitRepo returns the path to the Git repository of the wiki. +func GitRepo() string { return paths.gitRepo } + +// StaticFiles returns the path to static files directory +func StaticFiles() string { return paths.staticFiles } + +// TokensJSON returns the path to the JSON user tokens storage. func TokensJSON() string { return paths.tokensJSON } -// RegistrationCredentialsJSON returns a path to the JSON file where registration credentials are stored. -// -// Default path: $XDG_DATA_HOME/mycorrhiza/registration.json +// RegistrationCredentialsJSON returns the path to the JSON registration +// credentials storage. func RegistrationCredentialsJSON() string { return paths.registrationCredentialsJSON } -// FixedCredentialsJSON returns a path to the JSON file where fixed credentials are stored. -// -// There is no default path. +// FixedCredentialsJSON returns the path to the JSON fixed credentials storage. func FixedCredentialsJSON() string { return paths.fixedCredentialsJSON } -// CalculatePaths looks for all external paths and stores them. Tries its best to find any errors. It is safe it to call it multiple times in order to save new paths. -func CalculatePaths() error { - if dir, err := registrationCredentialsPath(); err != nil { +// PrepareWikiRoot ensures all needed directories and files exist and have +// correct permissions. +func PrepareWikiRoot() error { + if err := os.MkdirAll(cfg.WikiDir, os.ModeDir|0777); err != nil { return err - } else { - paths.registrationCredentialsJSON = dir } - if dir, err := tokenStoragePath(); err != nil { + paths.cacheDir = path.Join(cfg.WikiDir, "cache") + if err := os.MkdirAll(paths.cacheDir, os.ModeDir|0777); err != nil { return err - } else { - paths.tokensJSON = dir } - if dir, err := fixedCredentialsPath(); err != nil { + paths.gitRepo = path.Join(cfg.WikiDir, "wiki.git") + if err := os.MkdirAll(paths.gitRepo, os.ModeDir|0777); err != nil { return err - } else { - paths.fixedCredentialsJSON = dir } + paths.staticFiles = path.Join(cfg.WikiDir, "static") + if err := os.MkdirAll(paths.staticFiles, os.ModeDir|0777); err != nil { + return err + } + + paths.tokensJSON = path.Join(paths.cacheDir, "tokens.json") + paths.fixedCredentialsJSON = path.Join(cfg.WikiDir, "fixed-users.json") + paths.registrationCredentialsJSON = path.Join(paths.cacheDir, "registered-users.json") + return nil } - -func tokenStoragePath() (string, error) { - dir, err := xdg.DataFile("mycorrhiza/tokens.json") - if err != nil { - return "", err - } - if strings.HasPrefix(dir, cfg.WikiDir) { - return "", errors.New("wiki storage directory includes private config files") - } - return dir, nil -} - -func registrationCredentialsPath() (string, error) { - var err error - path := cfg.RegistrationCredentialsPath - - if len(path) == 0 { - path, err = xdg.DataFile("mycorrhiza/registration.json") - if err != nil { - return "", fmt.Errorf("cannot get a file to registration credentials, so no registered users will be saved: %w", err) - } - } else { - path, err = homedir.Expand(path) - if err != nil { - return "", fmt.Errorf("cannot expand RegistrationCredentialsPath: %w", err) - } - - path, err = filepath.Abs(path) - if err != nil { - return "", fmt.Errorf("cannot expand RegistrationCredentialsPath: %w", err) - } - } - - return path, nil -} - -func fixedCredentialsPath() (string, error) { - var err error - path := cfg.FixedAuthCredentialsPath - - if len(path) > 0 { - path, err = homedir.Expand(path) - if err != nil { - return "", fmt.Errorf("cannot expand FixedAuthCredentialsPath: %w", err) - } - - path, err = filepath.Abs(path) - if err != nil { - return "", fmt.Errorf("cannot expand FixedAuthCredentialsPath: %w", err) - } - } - return path, nil -} diff --git a/flag.go b/flag.go index 872f44c..e420b29 100644 --- a/flag.go +++ b/flag.go @@ -51,8 +51,9 @@ func parseCliArgs() { } wikiDir, err := filepath.Abs(args[0]) - cfg.WikiDir = wikiDir if err != nil { log.Fatal(err) } + + cfg.WikiDir = wikiDir } diff --git a/history/history.go b/history/history.go index e77430b..3db87c2 100644 --- a/history/history.go +++ b/history/history.go @@ -12,7 +12,7 @@ import ( "strings" "time" - "github.com/bouncepaw/mycorrhiza/cfg" + "github.com/bouncepaw/mycorrhiza/files" "github.com/bouncepaw/mycorrhiza/util" ) @@ -162,7 +162,7 @@ func (rev *Revision) bestLink() string { func gitsh(args ...string) (out bytes.Buffer, err error) { fmt.Printf("$ %v\n", args) cmd := exec.Command(gitpath, args...) - cmd.Dir = cfg.WikiDir + cmd.Dir = files.HyphaeDir() cmd.Env = gitEnv b, err := cmd.CombinedOutput() @@ -175,7 +175,7 @@ func gitsh(args ...string) (out bytes.Buffer, err error) { // silentGitsh is like gitsh, except it writes less to the stdout. func silentGitsh(args ...string) (out bytes.Buffer, err error) { cmd := exec.Command(gitpath, args...) - cmd.Dir = cfg.WikiDir + cmd.Dir = files.HyphaeDir() cmd.Env = gitEnv b, err := cmd.CombinedOutput() diff --git a/history/information.go b/history/information.go index 3d8342e..e27cf19 100644 --- a/history/information.go +++ b/history/information.go @@ -4,13 +4,15 @@ package history // Things related to gathering existing information. import ( "fmt" - "github.com/bouncepaw/mycorrhiza/cfg" "log" "regexp" "strconv" "strings" "time" + "github.com/bouncepaw/mycorrhiza/cfg" + "github.com/bouncepaw/mycorrhiza/files" + "github.com/gorilla/feeds" ) @@ -176,7 +178,7 @@ func parseRevisionLine(line string) Revision { // FileAtRevision shows how the file with the given file path looked at the commit with the hash. It may return an error if git fails. func FileAtRevision(filepath, hash string) (string, error) { - out, err := gitsh("show", hash+":"+strings.TrimPrefix(filepath, cfg.WikiDir+"/")) + out, err := gitsh("show", hash+":"+strings.TrimPrefix(filepath, files.HyphaeDir()+"/")) if err != nil { return "", err } diff --git a/main.go b/main.go index 6bc1091..55b2238 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,10 @@ package main import ( + "log" + "net/http" + "os" + "github.com/bouncepaw/mycorrhiza/cfg" "github.com/bouncepaw/mycorrhiza/files" "github.com/bouncepaw/mycorrhiza/history" @@ -14,9 +18,6 @@ import ( "github.com/bouncepaw/mycorrhiza/static" "github.com/bouncepaw/mycorrhiza/user" "github.com/bouncepaw/mycorrhiza/web" - "log" - "net/http" - "os" ) func main() { @@ -25,24 +26,25 @@ func main() { // It is ok if the path is "" cfg.ReadConfigFile() - if err := files.CalculatePaths(); err != nil { + if err := files.PrepareWikiRoot(); err != nil { log.Fatal(err) } log.Println("Running Mycorrhiza Wiki 1.2.0 indev") - if err := os.Chdir(cfg.WikiDir); err != nil { + if err := os.Chdir(files.HyphaeDir()); err != nil { log.Fatal(err) } - log.Println("Wiki storage directory is", cfg.WikiDir) + log.Println("Wiki directory is", cfg.WikiDir) + log.Println("Using Git storage at", files.HyphaeDir()) // Init the subsystems: - hyphae.Index(cfg.WikiDir) + hyphae.Index(files.HyphaeDir()) user.InitUserDatabase() history.Start() shroom.SetHeaderLinks() // Static files: - static.InitFS(cfg.WikiDir + "/static") + static.InitFS(files.StaticFiles()) // Network: go handleGemini() diff --git a/shroom/upload.go b/shroom/upload.go index 6c45371..55887f7 100644 --- a/shroom/upload.go +++ b/shroom/upload.go @@ -10,8 +10,7 @@ import ( "path/filepath" "strings" - "github.com/bouncepaw/mycorrhiza/cfg" - + "github.com/bouncepaw/mycorrhiza/files" "github.com/bouncepaw/mycorrhiza/history" "github.com/bouncepaw/mycorrhiza/hyphae" "github.com/bouncepaw/mycorrhiza/mimetype" @@ -65,10 +64,11 @@ func UploadBinary(h *hyphae.Hypha, mime string, file multipart.File, u *user.Use // uploadHelp is a helper function for UploadText and UploadBinary func uploadHelp(h *hyphae.Hypha, hop *history.HistoryOp, ext string, data []byte, u *user.User) (*history.HistoryOp, string) { var ( - fullPath = filepath.Join(cfg.WikiDir, h.Name+ext) + fullPath = filepath.Join(files.HyphaeDir(), h.Name+ext) originalFullPath = &h.TextPath ) - if !strings.HasPrefix(fullPath, cfg.WikiDir) { // If the path somehow got outside the wiki dir + // Reject if the path is outside the hyphae dir + if !strings.HasPrefix(fullPath, files.HyphaeDir()) { err := errors.New("bad path") return hop.WithErrAbort(err), err.Error() } @@ -80,7 +80,7 @@ func uploadHelp(h *hyphae.Hypha, hop *history.HistoryOp, ext string, data []byte return hop.WithErrAbort(err), err.Error() } - if err := ioutil.WriteFile(fullPath, data, 0644); err != nil { + if err := ioutil.WriteFile(fullPath, data, 0666); err != nil { return hop.WithErrAbort(err), err.Error() } diff --git a/user/files.go b/user/files.go index eb11039..15c33a8 100644 --- a/user/files.go +++ b/user/files.go @@ -16,7 +16,7 @@ import ( func InitUserDatabase() { AuthUsed = cfg.UseFixedAuth || cfg.UseRegistration - if AuthUsed && (cfg.FixedAuthCredentialsPath != "" || cfg.RegistrationCredentialsPath != "") { + if AuthUsed { ReadUsersFromFilesystem() } } diff --git a/util/util.go b/util/util.go index 7dd48ba..a21e8ff 100644 --- a/util/util.go +++ b/util/util.go @@ -3,6 +3,7 @@ package util import ( "crypto/rand" "encoding/hex" + "github.com/bouncepaw/mycorrhiza/files" "log" "net/http" "regexp" @@ -17,10 +18,11 @@ func PrepareRq(rq *http.Request) { rq.URL.Path = strings.TrimSuffix(rq.URL.Path, "/") } -// ShorterPath is used by handlerList to display shorter path to the files. It simply strips WikiDir. +// ShorterPath is used by handlerList to display shorter path to the files. It +// simply strips the hyphae directory name. func ShorterPath(path string) string { - if strings.HasPrefix(path, cfg.WikiDir) { - tmp := strings.TrimPrefix(path, cfg.WikiDir) + if strings.HasPrefix(path, files.HyphaeDir()) { + tmp := strings.TrimPrefix(path, files.HyphaeDir()) if tmp == "" { return "" } diff --git a/web/stuff.go b/web/stuff.go index 2b6ea60..a8352b7 100644 --- a/web/stuff.go +++ b/web/stuff.go @@ -2,12 +2,14 @@ package web // stuff.go is used for meta stuff about the wiki or all hyphae at once. import ( - "github.com/bouncepaw/mycorrhiza/cfg" "io" "log" "math/rand" "net/http" + "github.com/bouncepaw/mycorrhiza/cfg" + "github.com/bouncepaw/mycorrhiza/files" + "github.com/bouncepaw/mycorrhiza/hyphae" "github.com/bouncepaw/mycorrhiza/shroom" "github.com/bouncepaw/mycorrhiza/user" @@ -41,9 +43,8 @@ func handlerReindex(w http.ResponseWriter, rq *http.Request) { return } hyphae.ResetCount() - log.Println("Wiki storage directory is", cfg.WikiDir) - log.Println("Start indexing hyphae...") - hyphae.Index(cfg.WikiDir) + log.Println("Reindexing hyphae in", files.HyphaeDir()) + hyphae.Index(files.HyphaeDir()) log.Println("Indexed", hyphae.Count(), "hyphae") http.Redirect(w, rq, "/", http.StatusSeeOther) }