Migrate others

This commit is contained in:
Timur Ismagilov 2024-09-07 23:48:45 +03:00
parent 7a33272c59
commit 09764385ac
6 changed files with 61 additions and 43 deletions

41
flag.go
View File

@ -3,19 +3,20 @@ package main
import (
"bufio"
_ "embed"
"errors"
"flag"
"fmt"
"io"
"log"
"log/slog"
"os"
"path/filepath"
"golang.org/x/term"
"github.com/bouncepaw/mycorrhiza/internal/cfg"
"github.com/bouncepaw/mycorrhiza/internal/files"
user2 "github.com/bouncepaw/mycorrhiza/internal/user"
"github.com/bouncepaw/mycorrhiza/internal/user"
"github.com/bouncepaw/mycorrhiza/internal/version"
"golang.org/x/term"
)
// CLI options are read and parsed here.
@ -31,7 +32,7 @@ func printHelp() {
}
// parseCliArgs parses CLI options and sets several important global variables. Call it early.
func parseCliArgs() {
func parseCliArgs() error {
var createAdminName string
var versionFlag bool
@ -42,43 +43,53 @@ func parseCliArgs() {
flag.Parse()
if versionFlag {
fmt.Println("Mycorrhiza Wiki", version.Long)
slog.Info("Running Mycorrhiza Wiki", "version", version.Long)
os.Exit(0)
}
args := flag.Args()
if len(args) == 0 {
log.Fatal("error: pass a wiki directory")
slog.Error("Pass a wiki directory")
return errors.New("wiki directory not passed")
}
wikiDir, err := filepath.Abs(args[0])
if err != nil {
log.Fatal(err)
slog.Error("Failed to take absolute filepath of wiki directory",
"path", args[0], "err", err)
return err
}
cfg.WikiDir = wikiDir
if createAdminName != "" {
createAdminCommand(createAdminName)
if err := createAdminCommand(createAdminName); err != nil {
os.Exit(1)
}
os.Exit(0)
}
return nil
}
func createAdminCommand(name string) {
func createAdminCommand(name string) error {
if err := files.PrepareWikiRoot(); err != nil {
log.Fatal(err)
slog.Error("Failed to prepare wiki root", "err", err)
return err
}
cfg.UseAuth = true
cfg.AllowRegistration = true
user2.InitUserDatabase()
user.InitUserDatabase()
password, err := askPass("Password")
if err != nil {
log.Fatal(err)
slog.Error("Failed to prompt password", "err", err)
return err
}
if err := user2.Register(name, password, "admin", "local", true); err != nil {
log.Fatal(err)
if err := user.Register(name, password, "admin", "local", true); err != nil {
slog.Error("Failed to register admin", "err", err)
return err
}
return nil
}
func askPass(prompt string) (string, error) {

View File

@ -3,7 +3,7 @@ package user
import (
"encoding/json"
"errors"
"log"
"log/slog"
"os"
"github.com/bouncepaw/mycorrhiza/internal/cfg"
@ -32,19 +32,23 @@ func usersFromFile() []*User {
return users
}
if err != nil {
log.Fatal(err)
slog.Error("Failed to read users.json", "err", err)
os.Exit(1)
}
err = json.Unmarshal(contents, &users)
if err != nil {
log.Fatal(err)
slog.Error("Failed to unmarshal users.json contents", "err", err)
os.Exit(1)
}
for _, u := range users {
u.Name = util.CanonicalName(u.Name)
if u.Source == "" {
u.Source = "local"
}
}
log.Println("Found", len(users), "users")
slog.Info("Indexed users", "n", len(users))
return users
}
@ -63,20 +67,22 @@ func readTokensToUsers() {
return
}
if err != nil {
log.Fatal(err)
slog.Error("Failed to read tokens.json", "err", err)
os.Exit(1)
}
var tmp map[string]string
err = json.Unmarshal(contents, &tmp)
if err != nil {
log.Fatal(err)
slog.Error("Failed to unmarshal tokens.json contents", "err", err)
os.Exit(1)
}
for token, username := range tmp {
tokens.Store(token, username)
// commenceSession(username, token)
}
log.Println("Found", len(tmp), "active sessions")
slog.Info("Indexed active sessions", "n", len(tmp))
}
// SaveUserDatabase stores current user credentials into JSON file by configured path.
@ -94,13 +100,13 @@ func dumpUserCredentials() error {
blob, err := json.MarshalIndent(userList, "", "\t")
if err != nil {
log.Println(err)
slog.Error("Failed to marshal users.json", "err", err)
return err
}
err = os.WriteFile(files.UserCredentialsJSON(), blob, 0666)
if err != nil {
log.Println(err)
slog.Error("Failed to write users.json", "err", err)
return err
}
@ -119,11 +125,11 @@ func dumpTokens() {
blob, err := json.MarshalIndent(tmp, "", "\t")
if err != nil {
log.Println(err)
slog.Error("Failed to marshal tokens.json", "err", err)
return
}
err = os.WriteFile(files.TokensJSON(), blob, 0666)
if err != nil {
log.Println("an error occurred in dumpTokens function:", err)
slog.Error("Failed to write tokens.json", "err", err)
}
}

View File

@ -21,7 +21,7 @@ import (
"encoding/json"
"fmt"
"io/fs"
"log"
"log/slog"
"net/http"
"path/filepath"
"strings"
@ -78,7 +78,7 @@ func init() {
var strings map[string]string
if err := json.Unmarshal(contents, &strings); err != nil {
log.Fatalf("error while parsing %s: %v", path, err)
slog.Error("Failed to unmarshal localization file", "path", path, "err", err)
}
for key, value := range strings {

View File

@ -24,7 +24,9 @@ import (
)
func main() {
parseCliArgs()
if err := parseCliArgs(); err != nil {
os.Exit(1)
}
if err := files.PrepareWikiRoot(); err != nil {
log.Fatal(err)

View File

@ -3,7 +3,6 @@ package misc
import (
"io"
"log"
"log/slog"
"math/rand"
"mime"
@ -74,11 +73,11 @@ func handlerReindex(w http.ResponseWriter, rq *http.Request) {
if ok := user.CanProceed(rq, "reindex"); !ok {
var lc = l18n.FromRequest(rq)
viewutil.HttpErr(viewutil.MetaFrom(w, rq), http.StatusForbidden, cfg.HomeHypha, lc.Get("ui.reindex_no_rights"))
log.Println("Rejected", rq.URL)
slog.Info("No rights to reindex")
return
}
hyphae.ResetCount()
log.Println("Reindexing hyphae in", files.HyphaeDir())
slog.Info("Reindexing hyphae", "hyphaeDir", files.HyphaeDir())
hyphae.Index(files.HyphaeDir())
backlinks.IndexBacklinks()
http.Redirect(w, rq, "/", http.StatusSeeOther)
@ -90,9 +89,10 @@ func handlerUpdateHeaderLinks(w http.ResponseWriter, rq *http.Request) {
if ok := user.CanProceed(rq, "update-header-links"); !ok {
var lc = l18n.FromRequest(rq)
viewutil.HttpErr(viewutil.MetaFrom(w, rq), http.StatusForbidden, cfg.HomeHypha, lc.Get("ui.header_no_rights"))
log.Println("Rejected", rq.URL)
slog.Info("No rights to update header links")
return
}
slog.Info("Updated header links")
shroom.SetHeaderLinks()
http.Redirect(w, rq, "/", http.StatusSeeOther)
}
@ -149,7 +149,7 @@ func handlerStyle(w http.ResponseWriter, rq *http.Request) {
}
_, err = io.Copy(w, file)
if err != nil {
log.Println(err)
slog.Error("Failed to write stylesheet; proceeding anyway", "err", err)
}
_ = file.Close()
}
@ -164,7 +164,7 @@ func handlerRobotsTxt(w http.ResponseWriter, rq *http.Request) {
}
_, err = io.Copy(w, file)
if err != nil {
log.Println()
slog.Error("Failed to write robots.txt; proceeding anyway", "err", err)
}
_ = file.Close()
}

View File

@ -4,12 +4,7 @@ package web
import (
"errors"
"fmt"
"github.com/bouncepaw/mycorrhiza/internal/cfg"
"github.com/bouncepaw/mycorrhiza/internal/user"
"github.com/bouncepaw/mycorrhiza/l18n"
"github.com/bouncepaw/mycorrhiza/web/viewutil"
"io"
"log"
"log/slog"
"mime"
"net/http"
@ -19,11 +14,15 @@ import (
"github.com/bouncepaw/mycorrhiza/help"
"github.com/bouncepaw/mycorrhiza/history/histweb"
"github.com/bouncepaw/mycorrhiza/hypview"
"github.com/bouncepaw/mycorrhiza/internal/cfg"
"github.com/bouncepaw/mycorrhiza/internal/user"
"github.com/bouncepaw/mycorrhiza/interwiki"
"github.com/bouncepaw/mycorrhiza/l18n"
"github.com/bouncepaw/mycorrhiza/misc"
"github.com/gorilla/mux"
"github.com/bouncepaw/mycorrhiza/util"
"github.com/bouncepaw/mycorrhiza/web/viewutil"
"github.com/gorilla/mux"
)
// Handler initializes and returns the HTTP router based on the configuration.
@ -308,7 +307,7 @@ func handlerTelegramLogin(w http.ResponseWriter, rq *http.Request) {
errmsg := user.LoginDataHTTP(w, username, "")
if errmsg != nil {
log.Printf("Failed to login %s using Telegram: %s", username, err.Error())
slog.Error("Failed to login using Telegram", "err", err, "username", username)
w.WriteHeader(http.StatusBadRequest)
_, _ = io.WriteString(
w,