chore: move conf package

This commit is contained in:
Noah Hsu
2022-06-25 20:38:02 +08:00
parent 7dadab95b2
commit 306b90399c
18 changed files with 91 additions and 33 deletions

View File

@ -3,7 +3,7 @@ package aria2
import (
"context"
"fmt"
"github.com/alist-org/alist/v3/conf"
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/errs"
"github.com/alist-org/alist/v3/internal/operations"
"github.com/alist-org/alist/v3/pkg/task"

View File

@ -2,8 +2,8 @@ package aria2
import (
"context"
"github.com/alist-org/alist/v3/conf"
_ "github.com/alist-org/alist/v3/drivers"
conf2 "github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/operations"
"github.com/alist-org/alist/v3/internal/store"
@ -16,12 +16,12 @@ import (
)
func init() {
conf.Conf = conf.DefaultConfig()
conf2.Conf = conf2.DefaultConfig()
absPath, err := filepath.Abs("../../data/temp")
if err != nil {
panic(err)
}
conf.Conf.TempDir = absPath
conf2.Conf.TempDir = absPath
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
if err != nil {
panic("failed to connect database")

62
internal/conf/config.go Normal file
View File

@ -0,0 +1,62 @@
package conf
type Database struct {
Type string `json:"type" env:"DB_TYPE"`
Host string `json:"host" env:"DB_HOST"`
Port int `json:"port" env:"DB_PORT"`
User string `json:"user" env:"DB_USER"`
Password string `json:"password" env:"DB_PASS"`
Name string `json:"name" env:"DB_NAME"`
DBFile string `json:"db_file" env:"DB_FILE"`
TablePrefix string `json:"table_prefix" env:"DB_TABLE_PREFIX"`
SslMode string `json:"ssl_mode" env:"DB_SLL_MODE"`
}
type Scheme struct {
Https bool `json:"https" env:"HTTPS"`
CertFile string `json:"cert_file" env:"CERT_FILE"`
KeyFile string `json:"key_file" env:"KEY_FILE"`
}
type LogConfig struct {
Enable bool `json:"enable" env:"log_enable"`
Path string `json:"path" env:"LOG_PATH"`
Name string `json:"name" env:"LOG_NAME"`
RotationTime uint `json:"rotation_time" env:"LOG_TIME"`
RotationCount uint `json:"rotation_count" env:"LOG_COUNT"`
}
type Config struct {
Force bool `json:"force"`
Address string `json:"address" env:"ADDR"`
Port int `json:"port" env:"PORT"`
CaCheExpiration int `json:"cache_expiration" env:"CACHE_EXPIRATION"`
Assets string `json:"assets" env:"ASSETS"`
Database Database `json:"database"`
Scheme Scheme `json:"scheme"`
TempDir string `json:"temp_dir" env:"TEMP_DIR"`
Log LogConfig `json:"log"`
}
func DefaultConfig() *Config {
return &Config{
Address: "0.0.0.0",
Port: 5244,
Assets: "https://npm.elemecdn.com/alist-web@$version/dist",
TempDir: "data/temp",
Database: Database{
Type: "sqlite3",
Port: 0,
TablePrefix: "x_",
DBFile: "data/data.db",
},
CaCheExpiration: 30,
Log: LogConfig{
Enable: true,
Path: "log/%Y-%m-%d-%H:%M.log",
Name: "log/log.log",
RotationTime: 24,
RotationCount: 5,
},
}
}

1
internal/conf/const.go Normal file
View File

@ -0,0 +1 @@
package conf

14
internal/conf/var.go Normal file
View File

@ -0,0 +1,14 @@
package conf
var (
BuiltAt string
GoVersion string
GitAuthor string
GitCommit string
Version string = "dev"
WebVersion string
)
var (
Conf *Config
)

View File

@ -12,5 +12,5 @@ var (
)
func IsObjectNotFound(err error) bool {
return pkgerr.Cause(err) == ObjectNotFound
return errors.Is(pkgerr.Cause(err), ObjectNotFound)
}

View File

@ -7,13 +7,12 @@ const (
)
type User struct {
ID uint `json:"id" gorm:"primaryKey"` // unique key
Name string `json:"name" gorm:"unique"` // username
Password string `json:"password"` // password
BasePath string `json:"base_path"` // base path
AllowUpload bool `json:"allow_upload"` // allow upload
Role int `json:"role"` // user's role
//OfflineDownload bool `json:"offline_download"` // TODO? allow offline download
ID uint `json:"id" gorm:"primaryKey"` // unique key
Name string `json:"name" gorm:"unique"` // username
Password string `json:"password"` // password
BasePath string `json:"base_path"` // base path
ReadOnly bool `json:"read_only"` // allow upload
Role int `json:"role"` // user's role
}
func (u User) IsGuest() bool {

View File

@ -2,13 +2,13 @@ package operations
import (
"context"
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/errs"
log "github.com/sirupsen/logrus"
stdpath "path"
"time"
"github.com/Xhofe/go-cache"
"github.com/alist-org/alist/v3/conf"
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/singleflight"

View File

@ -0,0 +1 @@
package controllers

View File

@ -1 +1,17 @@
package server
import (
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
func Init(r *gin.Engine) {
Cors(r)
}
func Cors(r *gin.Engine) {
config := cors.DefaultConfig()
config.AllowAllOrigins = true
config.AllowHeaders = append(config.AllowHeaders, "Authorization", "range")
r.Use(cors.New(config))
}

View File

@ -2,7 +2,7 @@ package store
import (
"fmt"
"github.com/alist-org/alist/v3/conf"
"github.com/alist-org/alist/v3/internal/conf"
)
func columnName(name string) string {