Skip to content

Commit

Permalink
feat: dynamic root directory and path
Browse files Browse the repository at this point in the history
  • Loading branch information
小滋润 committed Nov 12, 2024
1 parent b76ff92 commit 4a4ed4b
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 46 deletions.
7 changes: 4 additions & 3 deletions docs/beforeDocsMake/renameModel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ package main

import (
"bufio"
"github.com/ZiRunHua/LeapLedger/global/constant"
"go/ast"
"go/parser"
"go/token"
"io/fs"
"os"
"path/filepath"
"strings"

"github.com/ZiRunHua/LeapLedger/global/constant"
)

func main() {
handleDir(constant.WORK_PATH + "/api/request/")
handleDir(constant.WORK_PATH + "/api/response/")
handleDir(filepath.Clean(constant.RootDir + "/api/request/"))
handleDir(filepath.Clean(constant.RootDir + "/api/response/"))
}
func handleDir(path string) {
_ = filepath.Walk(
Expand Down
50 changes: 41 additions & 9 deletions global/constant/constant.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package constant

type ServerMode string

var Debug, Production ServerMode = "debug", "production"
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)

const WORK_PATH = "/go/LeapLedger"
const RUNTIME_DATA_PATH = WORK_PATH + "/runtime/data"
type ServerMode string

const LOG_PATH = WORK_PATH + "/log"
const DATA_PATH = WORK_PATH + "/data"
const Debug, Production ServerMode = "debug", "production"

var ExampleAccountJsonPath = DATA_PATH + "/template/account/example.json"
var (
RootDir = getRootDir()
LogPath = filepath.Join(RootDir, "log")
DataPath = filepath.Join(RootDir, "data")
ExampleAccountJsonPath = filepath.Clean(DataPath + "/template/account/example.json")
)

// IncomeExpense 收支类型
type IncomeExpense string //@name IncomeExpense `example:"expense" enums:"income,expense" swaggertype:"string"`
type IncomeExpense string // @name IncomeExpense `example:"expense" enums:"income,expense" swaggertype:"string"`

const (
Income IncomeExpense = "income"
Expand Down Expand Up @@ -82,3 +89,28 @@ const (
// nats

type Subject string

func getRootDir() string {
// `os.Getwd()` is avoided here because, during tests, the working directory is set to the test file’s directory.
// This command retrieves the module's root directory instead.
// Source of `go list` usage: https://stackoverflow.com/a/75943840/23658318
rootDir, err := exec.Command("go", "list", "-m", "-f", "{{.Dir}}").Output()
if err == nil {
return strings.TrimSpace(string(rootDir))
}
// If `go list` fails, it may indicate the absence of a Go environment.
// In such cases, this suggests we are not in a test environment, so fall back to `os.Getwd()` to set `RootDir`.
workDir, err := os.Getwd()
if err != nil {
panic(err)
}
// Validate that the directory exists
_, err = os.Stat(workDir)
if err != nil {
if os.IsNotExist(err) {
panic(fmt.Sprintf("Path:%s does not exists", workDir))
}
panic(err)
}
return workDir
}
6 changes: 3 additions & 3 deletions global/cron/enter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cron

import (
"context"
"path/filepath"

"github.com/ZiRunHua/LeapLedger/global"
"github.com/ZiRunHua/LeapLedger/global/constant"
Expand All @@ -10,10 +11,9 @@ import (
"go.uber.org/zap"
)

const logPath = constant.LOG_PATH + "/cron.log"

var (
logger *zap.Logger
logPath = filepath.Join(constant.LogPath, "cron.log")
logger *zap.Logger

Scheduler = initialize.Scheduler
)
Expand Down
12 changes: 9 additions & 3 deletions global/nats/manager/dlq.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package manager
import (
"errors"
"fmt"
"path/filepath"

"github.com/ZiRunHua/LeapLedger/util/dataTool"
natsServer "github.com/nats-io/nats-server/v2/server"
Expand All @@ -25,9 +26,14 @@ type DlqManager interface {
RepublishBatch(batch int, ctx context.Context) (int, error)
}

const dlqName = "dlq"
const dlqPrefix = "dlq"
const dlqLogPath = natsLogPath + "dlq.log"
const (
dlqName = "dlq"
dlqPrefix = "dlq"
)

var (
dlqLogPath = filepath.Join(natsLogPath, "dlq.log")
)

type dlqManager struct {
DlqManager
Expand Down
3 changes: 2 additions & 1 deletion global/nats/manager/enter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package manager

import (
"path/filepath"
"runtime/debug"

"github.com/ZiRunHua/LeapLedger/global"
Expand All @@ -25,7 +26,7 @@ var (
DlqManage DlqManager
)

const natsLogPath = constant.LOG_PATH + "/nats/"
var natsLogPath = filepath.Join(constant.LogPath, "nats")

var (
taskLogger *zap.Logger
Expand Down
10 changes: 7 additions & 3 deletions global/nats/manager/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"path/filepath"
"runtime"
"time"

Expand All @@ -26,9 +27,12 @@ type EventManager interface {
}

const (
natsEventName = "event"
natsEventPrefix = "event"
natsEventLogPath = natsLogPath + "event.log"
natsEventName = "event"
natsEventPrefix = "event"
)

var (
natsEventLogPath = filepath.Join(natsLogPath, "event.log")
)

type Event string
Expand Down
10 changes: 7 additions & 3 deletions global/nats/manager/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package manager

import (
"fmt"
"path/filepath"
"runtime"
"time"

Expand All @@ -20,9 +21,12 @@ type TaskManager interface {
}

const (
natsTaskName = "task"
natsTaskPrefix = "task"
natsTaskLogPath = natsLogPath + "task.log"
natsTaskName = "task"
natsTaskPrefix = "task"
)

var (
natsTaskLogPath = filepath.Join(natsLogPath, "task.log")
)

type Task string
Expand Down
5 changes: 2 additions & 3 deletions initialize/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package initialize
import (
"context"
"os"
"path/filepath"
"time"

"github.com/ZiRunHua/LeapLedger/global/constant"
Expand Down Expand Up @@ -61,14 +62,12 @@ func init() {
}
}

const _configDirectoryPath = constant.WORK_PATH

func initConfig() error {
configFileName := os.Getenv("CONFIG_FILE_NAME")
if len(configFileName) == 0 {
configFileName = "config.yaml"
}
configPath := _configDirectoryPath + "/" + configFileName
configPath := filepath.Join(constant.RootDir, configFileName)
yamlFile, err := os.ReadFile(configPath)
if err != nil {
return err
Expand Down
8 changes: 4 additions & 4 deletions initialize/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ type _logger struct {
encoder zapcore.Encoder
}

const (
_requestLogPath = constant.WORK_PATH + "/log/request.log"
_errorLogPath = constant.WORK_PATH + "/log/error.log"
_panicLogPath = constant.WORK_PATH + "/log/panic.log"
var (
_requestLogPath = filepath.Join(constant.RootDir, "log", "request.log")
_errorLogPath = filepath.Join(constant.RootDir, "log", "error.log")
_panicLogPath = filepath.Join(constant.RootDir, "log", "panic.log")
)

func (l *_logger) do() error {
Expand Down
2 changes: 0 additions & 2 deletions initialize/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ type _nats struct {
// NatsDb is used to record and retry failure messages
// Enabled in consumer server

const nastStoreDir = constant.RUNTIME_DATA_PATH + "/nats"

func (n *_nats) do() error {
err := n.init()
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions model/product/enter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ package productModel

import (
"context"
"os"
"path/filepath"

"github.com/ZiRunHua/LeapLedger/global/constant"
"github.com/ZiRunHua/LeapLedger/global/cus"
"github.com/ZiRunHua/LeapLedger/global/db"
"github.com/ZiRunHua/LeapLedger/util/fileTool"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"os"
)

var initSqlFile = constant.DATA_PATH + "/database/product.sql"
var initSqlFile = filepath.Clean(constant.DataPath + "/database/product.sql")

func init() {
// table
Expand Down
8 changes: 5 additions & 3 deletions service/product/bill/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package bill

import (
"context"
"path/filepath"
"strings"

"github.com/ZiRunHua/LeapLedger/global"
Expand All @@ -22,9 +23,10 @@ import (
"go.uber.org/zap"
)

const logPath = constant.LOG_PATH + "/service/product/bill.log"

var logger *zap.Logger
var (
logPath = filepath.Clean(constant.LogPath + "/service/product/bill.log")
logger *zap.Logger
)

func init() {
var err error
Expand Down
4 changes: 3 additions & 1 deletion service/template/enter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package templateService
import (
"context"
"errors"
"path/filepath"

"github.com/ZiRunHua/LeapLedger/global"
"github.com/ZiRunHua/LeapLedger/global/constant"
Expand Down Expand Up @@ -48,7 +49,8 @@ var (

func init() {
var err error
if errorLog, err = global.Config.Logger.New(constant.LOG_PATH + "/service/template/error.log"); err != nil {
logPath := filepath.Clean(constant.LogPath + "/service/template/error.log")
if errorLog, err = global.Config.Logger.New(logPath); err != nil {
panic(err)
}

Expand Down
14 changes: 8 additions & 6 deletions service/thirdparty/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,30 @@ package thirdpartyService
import (
"bytes"
"fmt"
"os"
"path/filepath"
"time"

"github.com/ZiRunHua/LeapLedger/global"
"github.com/ZiRunHua/LeapLedger/global/constant"
userModel "github.com/ZiRunHua/LeapLedger/model/user"
commonService "github.com/ZiRunHua/LeapLedger/service/common"
"github.com/ZiRunHua/LeapLedger/util/rand"
"github.com/pkg/errors"
"os"
"time"
)

var emailTemplate map[constant.Notification][]byte
var emailTemplateFilePath = map[constant.Notification]string{
constant.NotificationOfCaptcha: "/template/email/captcha.html",
constant.NotificationOfRegistrationSuccess: "/template/email/registerSuccess.html",
constant.NotificationOfUpdatePassword: "/template/email/updatePassword.html",
constant.NotificationOfCaptcha: filepath.Clean("/template/email/captcha.html"),
constant.NotificationOfRegistrationSuccess: filepath.Clean("/template/email/registerSuccess.html"),
constant.NotificationOfUpdatePassword: filepath.Clean("/template/email/updatePassword.html"),
}

func init() {
emailTemplate = make(map[constant.Notification][]byte, len(emailTemplateFilePath))
var err error
for notification, path := range emailTemplateFilePath {
if emailTemplate[notification], err = os.ReadFile(constant.DATA_PATH + path); err != nil {
if emailTemplate[notification], err = os.ReadFile(filepath.Clean(constant.DataPath + path)); err != nil {
panic(err)
}
}
Expand Down

0 comments on commit 4a4ed4b

Please sign in to comment.