forked from AlistGo/alist
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d68a404
Showing
10 changed files
with
521 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
.idea/ | ||
.DS_Store | ||
output/ | ||
dist/ | ||
|
||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
*.db | ||
*.bin | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
*.yml | ||
bin/* | ||
alist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package conf | ||
|
||
type Database struct { | ||
Type string `json:"type"` | ||
User string `json:"user"` | ||
Password string `json:"password"` | ||
Host string `json:"host"` | ||
Port int `json:"port"` | ||
Name string `json:"name"` | ||
TablePrefix string `json:"table_prefix"` | ||
DBFile string `json:"db_file"` | ||
} | ||
type Config struct { | ||
Address string `json:"address"` | ||
Port int `json:"port"` | ||
Database Database `json:"database"` | ||
} | ||
|
||
func DefaultConfig() *Config { | ||
return &Config{ | ||
Address: "0.0.0.0", | ||
Port: 5244, | ||
Database: Database{ | ||
Type: "sqlite3", | ||
User: "", | ||
Password: "", | ||
Host: "", | ||
Port: 0, | ||
Name: "", | ||
TablePrefix: "x_", | ||
DBFile: "data.db", | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package conf | ||
|
||
import "gorm.io/gorm" | ||
|
||
var ( | ||
ConfigFile string // config file | ||
Conf *Config | ||
Debug bool | ||
|
||
DB *gorm.DB | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"address": "0.0.0.0", | ||
"port": 5244, | ||
"database": { | ||
"type": "sqlite3", | ||
"user": "", | ||
"password": "", | ||
"host": "", | ||
"port": 0, | ||
"name": "", | ||
"table_prefix": "a_list_", | ||
"db_file": "data.db" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module github.com/Xhofe/alist | ||
|
||
go 1.17 | ||
|
||
require github.com/gofiber/fiber/v2 v2.20.2 | ||
|
||
require ( | ||
github.com/andybalholm/brotli v1.0.3 // indirect | ||
github.com/go-sql-driver/mysql v1.6.0 // indirect | ||
github.com/jackc/chunkreader/v2 v2.0.1 // indirect | ||
github.com/jackc/pgconn v1.10.0 // indirect | ||
github.com/jackc/pgio v1.0.0 // indirect | ||
github.com/jackc/pgpassfile v1.0.0 // indirect | ||
github.com/jackc/pgproto3/v2 v2.1.1 // indirect | ||
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect | ||
github.com/jackc/pgtype v1.8.1 // indirect | ||
github.com/jackc/pgx/v4 v4.13.0 // indirect | ||
github.com/jinzhu/inflection v1.0.0 // indirect | ||
github.com/jinzhu/now v1.1.2 // indirect | ||
github.com/klauspost/compress v1.13.6 // indirect | ||
github.com/mattn/go-sqlite3 v1.14.9 // indirect | ||
github.com/sirupsen/logrus v1.8.1 // indirect | ||
github.com/valyala/bytebufferpool v1.0.0 // indirect | ||
github.com/valyala/fasthttp v1.31.0 // indirect | ||
github.com/valyala/tcplisten v1.0.0 // indirect | ||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect | ||
golang.org/x/sys v0.0.0-20211023085530-d6a326fbbf70 // indirect | ||
golang.org/x/text v0.3.7 // indirect | ||
gorm.io/driver/mysql v1.1.2 // indirect | ||
gorm.io/driver/postgres v1.1.2 // indirect | ||
gorm.io/driver/sqlite v1.1.6 // indirect | ||
gorm.io/gorm v1.21.16 // indirect | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"github.com/Xhofe/alist/conf" | ||
"github.com/Xhofe/alist/model" | ||
"github.com/Xhofe/alist/utils" | ||
"github.com/gofiber/fiber/v2" | ||
log "github.com/sirupsen/logrus" | ||
"io/ioutil" | ||
) | ||
|
||
// initConf init config | ||
func initConf() { | ||
log.Infof("reading config file: %s", conf.ConfigFile) | ||
if !utils.Exists(conf.ConfigFile) { | ||
log.Infof("config file not exists, creating default config file") | ||
conf.Conf = conf.DefaultConfig() | ||
if !utils.WriteToJson(conf.ConfigFile, conf.Conf) { | ||
log.Fatalf("failed to create default config file") | ||
} | ||
return | ||
} | ||
config, err := ioutil.ReadFile(conf.ConfigFile) | ||
if err != nil { | ||
log.Fatalf("reading config file error:%s", err.Error()) | ||
} | ||
conf.Conf = new(conf.Config) | ||
err = json.Unmarshal(config, conf.Conf) | ||
if err != nil { | ||
log.Fatalf("load config error: %s", err.Error()) | ||
} | ||
log.Debugf("config:%+v", conf.Conf) | ||
} | ||
|
||
// initLog init log | ||
func initLog() { | ||
if conf.Debug { | ||
log.SetLevel(log.DebugLevel) | ||
log.SetReportCaller(true) | ||
} | ||
log.SetFormatter(&log.TextFormatter{ | ||
//DisableColors: true, | ||
ForceColors: true, | ||
EnvironmentOverrideColors: true, | ||
TimestampFormat: "2006-01-02 15:04:05", | ||
FullTimestamp: true, | ||
}) | ||
} | ||
|
||
func init() { | ||
flag.StringVar(&conf.ConfigFile, "conf", "config.json", "config file") | ||
flag.BoolVar(&conf.Debug,"debug",false,"start with debug mode") | ||
flag.Parse() | ||
initLog() | ||
initConf() | ||
model.InitModel() | ||
} | ||
|
||
func main() { | ||
app := fiber.New() | ||
app.Get("/", func(ctx *fiber.Ctx) error { | ||
return ctx.SendString("Hello, World 👋!") | ||
}) | ||
log.Info("starting server") | ||
_ = app.Listen(fmt.Sprintf(":%d", conf.Conf.Port)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package model | ||
|
||
type ConfigItem struct { | ||
Key string `json:"key"` | ||
Value string `json:"value"` | ||
Type int `json:"type"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package model | ||
|
||
import ( | ||
"fmt" | ||
"github.com/Xhofe/alist/conf" | ||
log "github.com/sirupsen/logrus" | ||
"gorm.io/driver/mysql" | ||
"gorm.io/driver/postgres" | ||
"gorm.io/driver/sqlite" | ||
"gorm.io/gorm" | ||
"gorm.io/gorm/schema" | ||
"strings" | ||
) | ||
|
||
func InitModel() { | ||
log.Infof("init model...") | ||
config := conf.Conf.Database | ||
switch config.Type { | ||
case "sqlite3": | ||
{ | ||
if !(strings.HasSuffix(config.DBFile, ".db") && len(config.DBFile) > 3) { | ||
log.Fatalf("db name error.") | ||
} | ||
db, err := gorm.Open(sqlite.Open(config.DBFile), &gorm.Config{ | ||
NamingStrategy: schema.NamingStrategy{ | ||
TablePrefix: config.TablePrefix, | ||
}, | ||
}) | ||
if err != nil { | ||
log.Fatalf("failed to connect database:%s", err.Error()) | ||
} | ||
conf.DB = db | ||
} | ||
case "mysql": | ||
{ | ||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", | ||
config.User, config.Password, config.Host, config.Port, config.Name) | ||
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{ | ||
NamingStrategy: schema.NamingStrategy{ | ||
TablePrefix: config.TablePrefix, | ||
}, | ||
}) | ||
if err != nil { | ||
log.Fatalf("failed to connect database:%s", err.Error()) | ||
} | ||
conf.DB = db | ||
} | ||
case "postgres": | ||
{ | ||
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Asia/Shanghai", | ||
config.Host, config.User, config.Password, config.Name, config.Port) | ||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{ | ||
NamingStrategy: schema.NamingStrategy{ | ||
TablePrefix: config.TablePrefix, | ||
}, | ||
}) | ||
if err != nil { | ||
log.Errorf("failed to connect database:%s", err.Error()) | ||
} | ||
conf.DB = db | ||
|
||
} | ||
default: | ||
log.Fatalf("not supported database type: %s", config.Type) | ||
} | ||
log.Infof("auto migrate model") | ||
err := conf.DB.AutoMigrate(&ConfigItem{}) | ||
if err != nil { | ||
log.Fatalf("failed to auto migrate") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package utils | ||
|
||
import ( | ||
"encoding/json" | ||
log "github.com/sirupsen/logrus" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// Exists determine whether the file exists | ||
func Exists(name string) bool { | ||
if _, err := os.Stat(name); err != nil { | ||
if os.IsNotExist(err) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// CreatNestedFile create nested file | ||
func CreatNestedFile(path string) (*os.File, error) { | ||
basePath := filepath.Dir(path) | ||
if !Exists(basePath) { | ||
err := os.MkdirAll(basePath, 0700) | ||
if err != nil { | ||
log.Errorf("can't create foler,%s", err) | ||
return nil, err | ||
} | ||
} | ||
return os.Create(path) | ||
} | ||
|
||
// WriteToJson write struct to json file | ||
func WriteToJson(src string, conf interface{}) bool { | ||
data, err := json.MarshalIndent(conf,""," ") | ||
if err != nil { | ||
log.Errorf("failed convert Conf to []byte:%s", err.Error()) | ||
return false | ||
} | ||
err = ioutil.WriteFile(src, data, 0777) | ||
if err != nil { | ||
log.Errorf("failed to write json file:%s", err.Error()) | ||
return false | ||
} | ||
return true | ||
} |