forked from nixcn/nixcn-cms
Add dev services
- Development dotenvs - Caddy service - Redis service - Postgres service - Fix env parser error Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
8
.env.development
Normal file
8
.env.development
Normal file
@@ -0,0 +1,8 @@
|
||||
SERVER_ADDRESS=:8000
|
||||
SERVER_DEBUG_MODE=true
|
||||
SERVER_FILE_LOGGER=false
|
||||
DATABASE_TYPE=postgres
|
||||
DATABASE_HOST=127.0.0.1
|
||||
DATABASE_NAME=postgres
|
||||
DATABASE_USERNAME=postgres
|
||||
DATABASE_PASSWORD=postgres
|
||||
1
.env.production
Normal file
1
.env.production
Normal file
@@ -0,0 +1 @@
|
||||
TZ=Asia/Shanghai
|
||||
@@ -9,32 +9,33 @@ import (
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
func GetEnv(Key string) string {
|
||||
func GetEnv(key string) string {
|
||||
_ = godotenv.Load()
|
||||
return os.Getenv(Key)
|
||||
upperKey := strings.ToUpper(key)
|
||||
return os.Getenv(upperKey)
|
||||
}
|
||||
|
||||
func SetEnvConf(ConfKey string, ConfSub string) {
|
||||
var Config = []string{ConfKey, ConfSub}
|
||||
var EnvKey = strings.Join(Config, "_")
|
||||
env := GetEnv(EnvKey)
|
||||
var orig = Get(ConfKey + "." + ConfSub)
|
||||
func SetEnvConf(key string, sub string) {
|
||||
envJoin := strings.Join([]string{key, sub}, "_")
|
||||
env := GetEnv(envJoin)
|
||||
confJoin := strings.Join([]string{key, sub}, ".")
|
||||
orig := Get(confJoin)
|
||||
if env != "" {
|
||||
switch orig.(type) {
|
||||
case string:
|
||||
Set(ConfKey, env)
|
||||
Set(confJoin, env)
|
||||
case int:
|
||||
conv, err := strconv.Atoi(env)
|
||||
if err != nil {
|
||||
log.Panic("[Config] Error converting string to int: ", err)
|
||||
}
|
||||
Set(ConfKey, conv)
|
||||
Set(confJoin, conv)
|
||||
case bool:
|
||||
switch env {
|
||||
case "true":
|
||||
Set(ConfKey, true)
|
||||
Set(confJoin, true)
|
||||
case "false":
|
||||
Set(ConfKey, false)
|
||||
Set(confJoin, false)
|
||||
}
|
||||
case []string:
|
||||
trim := strings.TrimSpace(env)
|
||||
@@ -46,7 +47,7 @@ func SetEnvConf(ConfKey string, ConfSub string) {
|
||||
trimSub = strings.TrimSuffix(trimSub, "\"")
|
||||
envArray = append(envArray, trimSub)
|
||||
}
|
||||
Set(ConfKey, envArray)
|
||||
Set(confJoin, envArray)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ type User struct {
|
||||
Type string `json:"type" gorm:"not null"`
|
||||
Subtitle string `json:"subtitle" gorm:"not null"`
|
||||
Avatar string `json:"avatar" gorm:"not null"`
|
||||
Checkin bool `json:"checkin" gorm:"not null"`
|
||||
}
|
||||
|
||||
func (self *User) GetByEmail(email string) error {
|
||||
|
||||
50
devenv.nix
50
devenv.nix
@@ -1,12 +1,56 @@
|
||||
{ pkgs, ... }:
|
||||
{ pkgs, config, ... }:
|
||||
|
||||
{
|
||||
env.GREET = "devenv";
|
||||
|
||||
packages = [
|
||||
pkgs.git
|
||||
pkgs.bun
|
||||
pkgs.just
|
||||
];
|
||||
languages.go.enable = true;
|
||||
languages.go.version = "1.25.5";
|
||||
|
||||
dotenv = {
|
||||
enable = true;
|
||||
filename = [
|
||||
".env.production"
|
||||
".env.development"
|
||||
];
|
||||
};
|
||||
|
||||
languages.go = {
|
||||
enable = true;
|
||||
version = "1.25.5";
|
||||
};
|
||||
|
||||
services.caddy = {
|
||||
enable = true;
|
||||
dataDir = "${config.env.DEVENV_STATE}/caddy";
|
||||
config = ''
|
||||
{
|
||||
debug
|
||||
}
|
||||
:8080 {
|
||||
root * ${config.env.DEVENV_ROOT}/.outputs/static
|
||||
file_server
|
||||
reverse_proxy /api/v1/* http://127.0.0.1:8000
|
||||
}
|
||||
'';
|
||||
};
|
||||
|
||||
services.redis = {
|
||||
enable = true;
|
||||
};
|
||||
|
||||
services.postgres = {
|
||||
enable = true;
|
||||
createDatabase = true;
|
||||
listen_addresses = "127.0.0.1";
|
||||
initialDatabases = [
|
||||
{
|
||||
name = "postgres";
|
||||
user = "postgres";
|
||||
pass = "postgres";
|
||||
}
|
||||
];
|
||||
};
|
||||
}
|
||||
|
||||
1
main.go
1
main.go
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
func main() {
|
||||
config.Init()
|
||||
config.EnvInit()
|
||||
logger.Init()
|
||||
data.Init()
|
||||
server.Start()
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
package server
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"nixcn-cms/service/check"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Router(e *gin.Engine) {
|
||||
e.Static("/", "./static")
|
||||
// API Services
|
||||
api := e.Group("/api/v1")
|
||||
check.Handler(api.Group("/check"))
|
||||
}
|
||||
|
||||
9
service/check/handler.go
Normal file
9
service/check/handler.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package check
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func Handler(r *gin.RouterGroup) {
|
||||
r.GET("/test", func(ctx *gin.Context) {
|
||||
ctx.JSON(200, gin.H{"Test": "Test"})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user