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:
2025-12-20 01:27:11 +08:00
parent 0fb5c8b758
commit f130401ff8
8 changed files with 88 additions and 17 deletions

8
.env.development Normal file
View 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
View File

@@ -0,0 +1 @@
TZ=Asia/Shanghai

View File

@@ -9,32 +9,33 @@ import (
"github.com/joho/godotenv" "github.com/joho/godotenv"
) )
func GetEnv(Key string) string { func GetEnv(key string) string {
_ = godotenv.Load() _ = godotenv.Load()
return os.Getenv(Key) upperKey := strings.ToUpper(key)
return os.Getenv(upperKey)
} }
func SetEnvConf(ConfKey string, ConfSub string) { func SetEnvConf(key string, sub string) {
var Config = []string{ConfKey, ConfSub} envJoin := strings.Join([]string{key, sub}, "_")
var EnvKey = strings.Join(Config, "_") env := GetEnv(envJoin)
env := GetEnv(EnvKey) confJoin := strings.Join([]string{key, sub}, ".")
var orig = Get(ConfKey + "." + ConfSub) orig := Get(confJoin)
if env != "" { if env != "" {
switch orig.(type) { switch orig.(type) {
case string: case string:
Set(ConfKey, env) Set(confJoin, env)
case int: case int:
conv, err := strconv.Atoi(env) conv, err := strconv.Atoi(env)
if err != nil { if err != nil {
log.Panic("[Config] Error converting string to int: ", err) log.Panic("[Config] Error converting string to int: ", err)
} }
Set(ConfKey, conv) Set(confJoin, conv)
case bool: case bool:
switch env { switch env {
case "true": case "true":
Set(ConfKey, true) Set(confJoin, true)
case "false": case "false":
Set(ConfKey, false) Set(confJoin, false)
} }
case []string: case []string:
trim := strings.TrimSpace(env) trim := strings.TrimSpace(env)
@@ -46,7 +47,7 @@ func SetEnvConf(ConfKey string, ConfSub string) {
trimSub = strings.TrimSuffix(trimSub, "\"") trimSub = strings.TrimSuffix(trimSub, "\"")
envArray = append(envArray, trimSub) envArray = append(envArray, trimSub)
} }
Set(ConfKey, envArray) Set(confJoin, envArray)
} }
} }
} }

View File

@@ -11,6 +11,7 @@ type User struct {
Type string `json:"type" gorm:"not null"` Type string `json:"type" gorm:"not null"`
Subtitle string `json:"subtitle" gorm:"not null"` Subtitle string `json:"subtitle" gorm:"not null"`
Avatar string `json:"avatar" gorm:"not null"` Avatar string `json:"avatar" gorm:"not null"`
Checkin bool `json:"checkin" gorm:"not null"`
} }
func (self *User) GetByEmail(email string) error { func (self *User) GetByEmail(email string) error {

View File

@@ -1,12 +1,56 @@
{ pkgs, ... }: { pkgs, config, ... }:
{ {
env.GREET = "devenv"; env.GREET = "devenv";
packages = [ packages = [
pkgs.git pkgs.git
pkgs.bun pkgs.bun
pkgs.just 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";
}
];
};
} }

View File

@@ -9,6 +9,7 @@ import (
func main() { func main() {
config.Init() config.Init()
config.EnvInit()
logger.Init() logger.Init()
data.Init() data.Init()
server.Start() server.Start()

View File

@@ -1,7 +1,13 @@
package server package server
import "github.com/gin-gonic/gin" import (
"nixcn-cms/service/check"
"github.com/gin-gonic/gin"
)
func Router(e *gin.Engine) { 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
View 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"})
})
}