-
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.
Merge pull request #1 from AAStarCommunity/chao/infra-init
Chao/infra init
- Loading branch information
Showing
37 changed files
with
1,616 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,27 @@ | ||
name: Test and Coverage | ||
|
||
on: | ||
pull_request: | ||
branches: | ||
- "main" | ||
push: | ||
branches: | ||
- "*" | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-go@v5 | ||
with: | ||
go-version: '1.22.1' | ||
- name: Go Module Downloads | ||
run: go mod download | ||
- name: Run coverage | ||
run: go test -test.short -race -coverprofile=coverage.txt -covermode=atomic ./... | ||
- name: Upload coverage reports to Codecov | ||
uses: codecov/codecov-action@v4 | ||
with: | ||
token: ${{secrets.CODECOV_TOKEN}} | ||
slug: AAStarCommunity/AnotherAirAccountCommunityNode |
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 |
---|---|---|
|
@@ -19,3 +19,8 @@ | |
|
||
# Go workspace file | ||
go.work | ||
|
||
appsettings.*.yaml | ||
|
||
.idea | ||
.vscode |
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 |
---|---|---|
@@ -1,2 +1,7 @@ | ||
[![codecov](https://codecov.io/gh/AAStarCommunity/AnotherAirAccountCommunityNode/graph/badge.svg?token=G741C0D6SR)](https://codecov.io/gh/AAStarCommunity/AnotherAirAccountCommunityNode) | ||
|
||
# AnotherAirAccountCommunityNode | ||
|
||
A decentration community node for AirAccount | ||
|
||
## What's Community Node |
Empty file.
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 main | ||
|
||
import "another_node/internal/community/storage/migrations" | ||
|
||
func main() { | ||
migrations.Rollback() | ||
} |
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,18 @@ | ||
web: | ||
port: 8080 | ||
db: | ||
user: # change it | ||
password: #change it | ||
host: #change it | ||
schema: aastar # db schema | ||
jwt: | ||
security: thisisunsafeuntilyouchangit | ||
realm: AASTAR | ||
idkey: id | ||
node: | ||
genesis: false | ||
globalName: genesis-01 | ||
externalAddr: 0.0.0.0 | ||
externalPort: 7946 | ||
bindAddr: 0.0.0.0 | ||
bindPort: 7946 |
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,154 @@ | ||
package conf | ||
|
||
import ( | ||
"os" | ||
"strconv" | ||
"sync" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
var once sync.Once | ||
|
||
type Conf struct { | ||
Web Web | ||
Db DB | ||
Jwt JWT | ||
Node Node | ||
} | ||
|
||
var conf *Conf | ||
|
||
// getConf Read configuration | ||
func getConf() *Conf { | ||
once.Do(func() { | ||
if conf == nil { | ||
filePath := getConfFilePath() | ||
conf = getConfiguration(filePath) | ||
} | ||
}) | ||
return conf | ||
} | ||
|
||
// getConfiguration represent get the config from env or file | ||
// env will overwrite the file | ||
func getConfiguration(filePath *string) *Conf { | ||
if file, err := os.ReadFile(*filePath); err != nil { | ||
return mappingEnvToConf(nil) | ||
} else { | ||
c := Conf{} | ||
err := yaml.Unmarshal(file, &c) | ||
if err != nil { | ||
return mappingEnvToConf(&c) | ||
} | ||
|
||
return &c | ||
} | ||
} | ||
|
||
func mappingEnvToConf(fileConf *Conf) *Conf { | ||
envConf := &Conf{ | ||
Web: Web{}, | ||
Db: DB{}, | ||
Jwt: JWT{}, | ||
Node: Node{}, | ||
} | ||
if web__port := os.Getenv("web__port"); len(web__port) > 0 { | ||
if port, err := strconv.Atoi(web__port); err == nil && port > 0 { | ||
envConf.Web.Port = port | ||
} else { | ||
panic("web__port is invalid") | ||
} | ||
} else if fileConf != nil { | ||
if fileConf.Web.Port > 0 { | ||
envConf.Web.Port = fileConf.Web.Port | ||
} else { | ||
panic("web.port is invalid") | ||
} | ||
} | ||
if db__user := os.Getenv("db__user"); len(db__user) > 0 { | ||
envConf.Db.User = db__user | ||
} else if fileConf != nil { | ||
envConf.Db.User = fileConf.Db.User | ||
} | ||
if db__password := os.Getenv("db__password"); len(db__password) > 0 { | ||
envConf.Db.Password = db__password | ||
} else if fileConf != nil { | ||
envConf.Db.Password = fileConf.Db.Password | ||
} | ||
if db__host := os.Getenv("db__host"); len(db__host) > 0 { | ||
envConf.Db.Host = db__host | ||
} else if fileConf != nil { | ||
envConf.Db.Host = fileConf.Db.Host | ||
} | ||
if db__schema := os.Getenv("db__schema"); len(db__schema) > 0 { | ||
envConf.Db.Schema = db__schema | ||
} else if fileConf != nil { | ||
envConf.Db.Schema = fileConf.Db.Schema | ||
} | ||
|
||
if jwt__security := os.Getenv("jwt__security"); len(jwt__security) > 0 { | ||
envConf.Jwt.Security = jwt__security | ||
} else if fileConf != nil { | ||
envConf.Jwt.Security = fileConf.Jwt.Security | ||
} | ||
if jwt__realm := os.Getenv("jwt__realm"); len(jwt__realm) > 0 { | ||
envConf.Jwt.Security = jwt__realm | ||
} else if fileConf != nil { | ||
envConf.Jwt.Realm = fileConf.Jwt.Realm | ||
} | ||
if jwt__idkey := os.Getenv("jwt__idkey"); len(jwt__idkey) > 0 { | ||
envConf.Jwt.Security = jwt__idkey | ||
} else if fileConf != nil { | ||
envConf.Jwt.IdKey = fileConf.Jwt.IdKey | ||
} | ||
|
||
if node__genesis := os.Getenv("node__genesis"); len(node__genesis) > 0 { | ||
envConf.Node.Genesis = node__genesis == "true" | ||
} else if fileConf != nil { | ||
envConf.Node.Genesis = fileConf.Node.Genesis | ||
} | ||
if node__globalname := os.Getenv("node__globalname"); len(node__globalname) > 0 { | ||
envConf.Node.GlobalName = node__globalname | ||
} else if fileConf != nil { | ||
envConf.Node.GlobalName = fileConf.Node.GlobalName | ||
} | ||
if node__externaladdr := os.Getenv("node__externaladdr"); len(node__externaladdr) > 0 { | ||
envConf.Node.ExternalAddr = node__externaladdr | ||
} else if fileConf != nil { | ||
envConf.Node.ExternalAddr = fileConf.Node.ExternalAddr | ||
} | ||
if node__externalport := os.Getenv("node__externalport"); len(node__externalport) > 0 { | ||
if port, err := strconv.Atoi(node__externalport); err == nil && port > 0 { | ||
envConf.Node.ExternalPort = port | ||
} else { | ||
panic("node__externalport is invalid") | ||
} | ||
} else if fileConf != nil { | ||
if fileConf.Node.ExternalPort > 0 { | ||
envConf.Node.ExternalPort = fileConf.Node.ExternalPort | ||
} else { | ||
panic("node.externalport is invalid") | ||
} | ||
} | ||
if node__bindaddr := os.Getenv("node__bindaddr"); len(node__bindaddr) > 0 { | ||
envConf.Node.BindAddr = node__bindaddr | ||
} else if fileConf != nil { | ||
envConf.Node.BindAddr = fileConf.Node.BindAddr | ||
} | ||
if node__bindport := os.Getenv("node__bindport"); len(node__bindport) > 0 { | ||
if port, err := strconv.Atoi(node__bindport); err == nil && port > 0 { | ||
envConf.Node.BindPort = port | ||
} else { | ||
panic("node__bindport is invalid") | ||
} | ||
} else if fileConf != nil { | ||
if fileConf.Node.BindPort > 0 { | ||
envConf.Node.BindPort = fileConf.Node.BindPort | ||
} else { | ||
panic("node.bindport is invalid") | ||
} | ||
} | ||
|
||
return envConf | ||
} |
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,61 @@ | ||
package conf | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"sync" | ||
|
||
"gorm.io/driver/postgres" | ||
"gorm.io/driver/sqlite" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type DB struct { | ||
User string | ||
Password string | ||
Host string | ||
Schema string | ||
} | ||
|
||
var db *gorm.DB | ||
|
||
func getDbConnectionString(c *Conf) *string { | ||
// | ||
dsn := fmt.Sprintf("postgres://%s:%s@%s/%s", c.Db.User, c.Db.Password, c.Db.Host, c.Db.Schema) | ||
return &dsn | ||
} | ||
|
||
var onceDb sync.Once | ||
|
||
// GetDbClient 获取数据库连接对象 | ||
func GetDbClient() *gorm.DB { | ||
onceDb.Do(func() { | ||
if os.Getenv("UnitTestEnv") == "1" { | ||
db, _ = getInMemoryDbClient() | ||
db = db.Debug() | ||
} else { | ||
dsn := getDbConnectionString(getConf()) | ||
_db, err := gorm.Open(postgres.Open(*dsn), &gorm.Config{ | ||
DisableForeignKeyConstraintWhenMigrating: true, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if Environment.IsDevelopment() { | ||
_db = _db.Debug() | ||
} | ||
db = _db | ||
} | ||
}) | ||
return db | ||
} | ||
|
||
// getInMemoryDbClient 获取内存数据库对象,仅限单元测试使用 | ||
func getInMemoryDbClient() (*gorm.DB, error) { | ||
if client, err := gorm.Open(sqlite.Open("file::memory:?cache=private"), &gorm.Config{}); err != nil { | ||
return nil, err | ||
} else { | ||
return client, nil | ||
} | ||
} |
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,45 @@ | ||
package conf | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
) | ||
|
||
type Env struct { | ||
Name string // 环境名称 | ||
Debugger bool // 是否调试模式 | ||
} | ||
|
||
func (env *Env) IsDevelopment() bool { | ||
return strings.EqualFold("dev", env.Name) | ||
} | ||
|
||
func (env *Env) IsProduction() bool { | ||
return strings.EqualFold("prod", env.Name) | ||
} | ||
|
||
func (env *Env) GetEnvName() *string { | ||
return &env.Name | ||
} | ||
|
||
func getConfFilePath() *string { | ||
path := fmt.Sprintf("conf/appsettings.%s.yaml", strings.ToLower(Environment.Name)) | ||
if _, err := os.Stat(path); err != nil && os.IsNotExist(err) { | ||
path = "conf/appsettings.yaml" | ||
} | ||
return &path | ||
} | ||
|
||
var Environment *Env | ||
|
||
func init() { | ||
envName := "prod" | ||
if len(os.Getenv("Env")) > 0 { | ||
envName = os.Getenv("Env") | ||
} | ||
Environment = &Env{ | ||
Name: envName, | ||
Debugger: strings.EqualFold("dev", envName), | ||
} | ||
} |
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,28 @@ | ||
package conf | ||
|
||
import "sync" | ||
|
||
type JWT struct { | ||
Security string | ||
Realm string | ||
IdKey string | ||
} | ||
|
||
var jwt *JWT | ||
|
||
var onceJwt sync.Once | ||
|
||
// GetJwtKey 获取JWT私钥 | ||
func GetJwtKey() *JWT { | ||
onceJwt.Do(func() { | ||
if jwt == nil { | ||
j := getConf().Jwt | ||
jwt = &JWT{ | ||
Security: j.Security, | ||
Realm: j.Realm, | ||
} | ||
} | ||
}) | ||
|
||
return jwt | ||
} |
Oops, something went wrong.