1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-01-07 02:10:26 +00:00

New structure

This commit is contained in:
handlerug 2021-06-19 11:51:10 +07:00
parent b90c288549
commit 7b2423ec40
No known key found for this signature in database
GPG Key ID: 38009F0605051491
10 changed files with 74 additions and 115 deletions

View File

@ -25,9 +25,7 @@ var (
GeminiCertificatePath string GeminiCertificatePath string
UseFixedAuth bool UseFixedAuth bool
FixedAuthCredentialsPath string
UseRegistration bool UseRegistration bool
RegistrationCredentialsPath string
LimitRegistration int LimitRegistration int
OmnipresentScripts []string OmnipresentScripts []string
@ -80,10 +78,8 @@ type CustomScripts struct {
// Authorization is a section of Config that has fields related to authorization and authentication. // Authorization is a section of Config that has fields related to authorization and authentication.
type Authorization struct { type Authorization struct {
UseFixedAuth bool UseFixedAuth bool
FixedAuthCredentialsPath string
UseRegistration bool UseRegistration bool
RegistrationCredentialsPath string
LimitRegistration uint64 LimitRegistration uint64
} }
@ -106,10 +102,8 @@ func ReadConfigFile() {
}, },
Authorization: Authorization{ Authorization: Authorization{
UseFixedAuth: false, UseFixedAuth: false,
FixedAuthCredentialsPath: "",
UseRegistration: false, UseRegistration: false,
RegistrationCredentialsPath: "",
LimitRegistration: 0, LimitRegistration: 0,
}, },
CustomScripts: CustomScripts{ CustomScripts: CustomScripts{
@ -142,9 +136,7 @@ func ReadConfigFile() {
URL = cfg.URL URL = cfg.URL
GeminiCertificatePath = cfg.GeminiCertificatePath GeminiCertificatePath = cfg.GeminiCertificatePath
UseFixedAuth = cfg.UseFixedAuth UseFixedAuth = cfg.UseFixedAuth
FixedAuthCredentialsPath = cfg.FixedAuthCredentialsPath
UseRegistration = cfg.UseRegistration UseRegistration = cfg.UseRegistration
RegistrationCredentialsPath = cfg.RegistrationCredentialsPath
LimitRegistration = int(cfg.LimitRegistration) LimitRegistration = int(cfg.LimitRegistration)
OmnipresentScripts = cfg.OmnipresentScripts OmnipresentScripts = cfg.OmnipresentScripts
ViewScripts = cfg.ViewScripts ViewScripts = cfg.ViewScripts

View File

@ -2,109 +2,68 @@
package files package files
import ( import (
"errors" "os"
"fmt" "path"
"github.com/bouncepaw/mycorrhiza/cfg"
"path/filepath"
"strings"
"github.com/adrg/xdg" "github.com/bouncepaw/mycorrhiza/cfg"
"github.com/mitchellh/go-homedir"
) )
var paths struct { var paths struct {
gitRepo string
cacheDir string
staticFiles string
tokensJSON string tokensJSON string
registrationCredentialsJSON string registrationCredentialsJSON string
fixedCredentialsJSON string fixedCredentialsJSON string
} }
// TokensJSON returns a path to the JSON file where users' tokens are stored. // HyphaeDir returns the path to hyphae storage.
// // A separate function is needed to easily know where a general storage path is
// Default path: $XDG_DATA_HOME/mycorrhiza/tokens.json // 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 } func TokensJSON() string { return paths.tokensJSON }
// RegistrationCredentialsJSON returns a path to the JSON file where registration credentials are stored. // RegistrationCredentialsJSON returns the path to the JSON registration
// // credentials storage.
// Default path: $XDG_DATA_HOME/mycorrhiza/registration.json
func RegistrationCredentialsJSON() string { return paths.registrationCredentialsJSON } func RegistrationCredentialsJSON() string { return paths.registrationCredentialsJSON }
// FixedCredentialsJSON returns a path to the JSON file where fixed credentials are stored. // FixedCredentialsJSON returns the path to the JSON fixed credentials storage.
//
// There is no default path.
func FixedCredentialsJSON() string { return paths.fixedCredentialsJSON } 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. // PrepareWikiRoot ensures all needed directories and files exist and have
func CalculatePaths() error { // correct permissions.
if dir, err := registrationCredentialsPath(); err != nil { func PrepareWikiRoot() error {
if err := os.MkdirAll(cfg.WikiDir, os.ModeDir|0777); err != nil {
return err 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 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 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 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
}

View File

@ -51,8 +51,9 @@ func parseCliArgs() {
} }
wikiDir, err := filepath.Abs(args[0]) wikiDir, err := filepath.Abs(args[0])
cfg.WikiDir = wikiDir
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
cfg.WikiDir = wikiDir
} }

View File

@ -12,7 +12,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/bouncepaw/mycorrhiza/cfg" "github.com/bouncepaw/mycorrhiza/files"
"github.com/bouncepaw/mycorrhiza/util" "github.com/bouncepaw/mycorrhiza/util"
) )
@ -162,7 +162,7 @@ func (rev *Revision) bestLink() string {
func gitsh(args ...string) (out bytes.Buffer, err error) { func gitsh(args ...string) (out bytes.Buffer, err error) {
fmt.Printf("$ %v\n", args) fmt.Printf("$ %v\n", args)
cmd := exec.Command(gitpath, args...) cmd := exec.Command(gitpath, args...)
cmd.Dir = cfg.WikiDir cmd.Dir = files.HyphaeDir()
cmd.Env = gitEnv cmd.Env = gitEnv
b, err := cmd.CombinedOutput() 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. // silentGitsh is like gitsh, except it writes less to the stdout.
func silentGitsh(args ...string) (out bytes.Buffer, err error) { func silentGitsh(args ...string) (out bytes.Buffer, err error) {
cmd := exec.Command(gitpath, args...) cmd := exec.Command(gitpath, args...)
cmd.Dir = cfg.WikiDir cmd.Dir = files.HyphaeDir()
cmd.Env = gitEnv cmd.Env = gitEnv
b, err := cmd.CombinedOutput() b, err := cmd.CombinedOutput()

View File

@ -4,13 +4,15 @@ package history
// Things related to gathering existing information. // Things related to gathering existing information.
import ( import (
"fmt" "fmt"
"github.com/bouncepaw/mycorrhiza/cfg"
"log" "log"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"github.com/bouncepaw/mycorrhiza/cfg"
"github.com/bouncepaw/mycorrhiza/files"
"github.com/gorilla/feeds" "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. // 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) { 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 { if err != nil {
return "", err return "", err
} }

18
main.go
View File

@ -6,6 +6,10 @@
package main package main
import ( import (
"log"
"net/http"
"os"
"github.com/bouncepaw/mycorrhiza/cfg" "github.com/bouncepaw/mycorrhiza/cfg"
"github.com/bouncepaw/mycorrhiza/files" "github.com/bouncepaw/mycorrhiza/files"
"github.com/bouncepaw/mycorrhiza/history" "github.com/bouncepaw/mycorrhiza/history"
@ -14,9 +18,6 @@ import (
"github.com/bouncepaw/mycorrhiza/static" "github.com/bouncepaw/mycorrhiza/static"
"github.com/bouncepaw/mycorrhiza/user" "github.com/bouncepaw/mycorrhiza/user"
"github.com/bouncepaw/mycorrhiza/web" "github.com/bouncepaw/mycorrhiza/web"
"log"
"net/http"
"os"
) )
func main() { func main() {
@ -25,24 +26,25 @@ func main() {
// It is ok if the path is "" // It is ok if the path is ""
cfg.ReadConfigFile() cfg.ReadConfigFile()
if err := files.CalculatePaths(); err != nil { if err := files.PrepareWikiRoot(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
log.Println("Running Mycorrhiza Wiki 1.2.0 indev") 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.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: // Init the subsystems:
hyphae.Index(cfg.WikiDir) hyphae.Index(files.HyphaeDir())
user.InitUserDatabase() user.InitUserDatabase()
history.Start() history.Start()
shroom.SetHeaderLinks() shroom.SetHeaderLinks()
// Static files: // Static files:
static.InitFS(cfg.WikiDir + "/static") static.InitFS(files.StaticFiles())
// Network: // Network:
go handleGemini() go handleGemini()

View File

@ -10,8 +10,7 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/bouncepaw/mycorrhiza/cfg" "github.com/bouncepaw/mycorrhiza/files"
"github.com/bouncepaw/mycorrhiza/history" "github.com/bouncepaw/mycorrhiza/history"
"github.com/bouncepaw/mycorrhiza/hyphae" "github.com/bouncepaw/mycorrhiza/hyphae"
"github.com/bouncepaw/mycorrhiza/mimetype" "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 // 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) { func uploadHelp(h *hyphae.Hypha, hop *history.HistoryOp, ext string, data []byte, u *user.User) (*history.HistoryOp, string) {
var ( var (
fullPath = filepath.Join(cfg.WikiDir, h.Name+ext) fullPath = filepath.Join(files.HyphaeDir(), h.Name+ext)
originalFullPath = &h.TextPath 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") err := errors.New("bad path")
return hop.WithErrAbort(err), err.Error() 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() 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() return hop.WithErrAbort(err), err.Error()
} }

View File

@ -16,7 +16,7 @@ import (
func InitUserDatabase() { func InitUserDatabase() {
AuthUsed = cfg.UseFixedAuth || cfg.UseRegistration AuthUsed = cfg.UseFixedAuth || cfg.UseRegistration
if AuthUsed && (cfg.FixedAuthCredentialsPath != "" || cfg.RegistrationCredentialsPath != "") { if AuthUsed {
ReadUsersFromFilesystem() ReadUsersFromFilesystem()
} }
} }

View File

@ -3,6 +3,7 @@ package util
import ( import (
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"github.com/bouncepaw/mycorrhiza/files"
"log" "log"
"net/http" "net/http"
"regexp" "regexp"
@ -17,10 +18,11 @@ func PrepareRq(rq *http.Request) {
rq.URL.Path = strings.TrimSuffix(rq.URL.Path, "/") 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 { func ShorterPath(path string) string {
if strings.HasPrefix(path, cfg.WikiDir) { if strings.HasPrefix(path, files.HyphaeDir()) {
tmp := strings.TrimPrefix(path, cfg.WikiDir) tmp := strings.TrimPrefix(path, files.HyphaeDir())
if tmp == "" { if tmp == "" {
return "" return ""
} }

View File

@ -2,12 +2,14 @@ package web
// stuff.go is used for meta stuff about the wiki or all hyphae at once. // stuff.go is used for meta stuff about the wiki or all hyphae at once.
import ( import (
"github.com/bouncepaw/mycorrhiza/cfg"
"io" "io"
"log" "log"
"math/rand" "math/rand"
"net/http" "net/http"
"github.com/bouncepaw/mycorrhiza/cfg"
"github.com/bouncepaw/mycorrhiza/files"
"github.com/bouncepaw/mycorrhiza/hyphae" "github.com/bouncepaw/mycorrhiza/hyphae"
"github.com/bouncepaw/mycorrhiza/shroom" "github.com/bouncepaw/mycorrhiza/shroom"
"github.com/bouncepaw/mycorrhiza/user" "github.com/bouncepaw/mycorrhiza/user"
@ -41,9 +43,8 @@ func handlerReindex(w http.ResponseWriter, rq *http.Request) {
return return
} }
hyphae.ResetCount() hyphae.ResetCount()
log.Println("Wiki storage directory is", cfg.WikiDir) log.Println("Reindexing hyphae in", files.HyphaeDir())
log.Println("Start indexing hyphae...") hyphae.Index(files.HyphaeDir())
hyphae.Index(cfg.WikiDir)
log.Println("Indexed", hyphae.Count(), "hyphae") log.Println("Indexed", hyphae.Count(), "hyphae")
http.Redirect(w, rq, "/", http.StatusSeeOther) http.Redirect(w, rq, "/", http.StatusSeeOther)
} }