diff --git a/.env.development b/.env.development new file mode 100644 index 0000000..f14e924 --- /dev/null +++ b/.env.development @@ -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 diff --git a/.env.production b/.env.production new file mode 100644 index 0000000..c5f9a92 --- /dev/null +++ b/.env.production @@ -0,0 +1 @@ +TZ=Asia/Shanghai diff --git a/config/env.go b/config/env.go index 0c9972b..890a948 100644 --- a/config/env.go +++ b/config/env.go @@ -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) } } } diff --git a/data/user.go b/data/user.go index 316b2e5..cff1d2a 100644 --- a/data/user.go +++ b/data/user.go @@ -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 { diff --git a/devenv.nix b/devenv.nix index 1aadef3..fd2fb07 100644 --- a/devenv.nix +++ b/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"; + } + ]; + }; } diff --git a/main.go b/main.go index f16e285..2a1cf9f 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( func main() { config.Init() + config.EnvInit() logger.Init() data.Init() server.Start() diff --git a/server/router.go b/server/router.go index a2c77c0..236faac 100644 --- a/server/router.go +++ b/server/router.go @@ -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")) } diff --git a/service/check/handler.go b/service/check/handler.go new file mode 100644 index 0000000..445c9d2 --- /dev/null +++ b/service/check/handler.go @@ -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"}) + }) +}