feat(backend,docker,docs): implement Go/Gin/SQLite REST API server, Docker containerization, and architecture specifications
- Implement standalone Go backend API server using Gin and pure-Go SQLite (modernc.org/sqlite) - Add 11-table schema DDL migrations (000001_init.up.sql) based on DATA_TABLE.md - Add seed data ingestion tool (cmd/seed) and static TypeScript content exporter (cmd/export-content) - Add configuration loader (internal/config) supporting environment variables and .env files (HOST, PORT, DB_PATH, GIN_MODE, CORS_ALLOW_ORIGINS, AUTO_MIGRATE) - Add lightweight multi-stage Dockerfile and docker-compose.yml with persistent SQLite volume - Add comprehensive architecture specification (ARCHITECTURE.md) and REST API interface specification (API_INTERFACE.md) - Harmonize frontend publications page to consume isHighlight flag from database-synced content
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Config holds all server configuration derived from environment variables and flags.
|
||||
type Config struct {
|
||||
Port int
|
||||
Host string
|
||||
DBPath string
|
||||
GinMode string
|
||||
CORSAllowOrigins string
|
||||
AutoMigrate bool
|
||||
}
|
||||
|
||||
// LoadEnvFile reads a simple .env file if it exists and loads variables into environment without overriding existing ones.
|
||||
func LoadEnvFile(filenames ...string) {
|
||||
if len(filenames) == 0 {
|
||||
filenames = []string{".env", "../.env"}
|
||||
}
|
||||
|
||||
for _, filename := range filenames {
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
|
||||
parts := strings.SplitN(line, "=", 2)
|
||||
if len(parts) != 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
key := strings.TrimSpace(parts[0])
|
||||
val := strings.TrimSpace(parts[1])
|
||||
// Strip optional surrounding quotes
|
||||
val = strings.Trim(val, `"'`)
|
||||
|
||||
// Only set if not already present in environment
|
||||
if _, exists := os.LookupEnv(key); !exists {
|
||||
_ = os.Setenv(key, val)
|
||||
}
|
||||
}
|
||||
_ = file.Close()
|
||||
}
|
||||
}
|
||||
|
||||
// Load reads and parses all configuration settings from the environment.
|
||||
func Load() *Config {
|
||||
LoadEnvFile()
|
||||
|
||||
cfg := &Config{
|
||||
Port: 8080,
|
||||
Host: "0.0.0.0",
|
||||
DBPath: "anl.db",
|
||||
GinMode: "release",
|
||||
CORSAllowOrigins: "*",
|
||||
AutoMigrate: true,
|
||||
}
|
||||
|
||||
if portStr := os.Getenv("PORT"); portStr != "" {
|
||||
if p, err := strconv.Atoi(portStr); err == nil && p > 0 {
|
||||
cfg.Port = p
|
||||
}
|
||||
}
|
||||
|
||||
if hostStr := os.Getenv("HOST"); hostStr != "" {
|
||||
cfg.Host = hostStr
|
||||
}
|
||||
|
||||
if dbPathStr := os.Getenv("DB_PATH"); dbPathStr != "" {
|
||||
cfg.DBPath = dbPathStr
|
||||
}
|
||||
|
||||
if ginModeStr := os.Getenv("GIN_MODE"); ginModeStr != "" {
|
||||
cfg.GinMode = ginModeStr
|
||||
}
|
||||
|
||||
if corsStr := os.Getenv("CORS_ALLOW_ORIGINS"); corsStr != "" {
|
||||
cfg.CORSAllowOrigins = corsStr
|
||||
}
|
||||
|
||||
if autoMigrateStr := os.Getenv("AUTO_MIGRATE"); autoMigrateStr != "" {
|
||||
cfg.AutoMigrate = strings.ToLower(autoMigrateStr) != "false" && autoMigrateStr != "0"
|
||||
}
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
// Address returns the combined host:port address string.
|
||||
func (c *Config) Address() string {
|
||||
return fmt.Sprintf("%s:%d", c.Host, c.Port)
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package config_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"git.godopu.com/lab/landing_page/backend/internal/config"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestConfigDefaults(t *testing.T) {
|
||||
// Clear env
|
||||
_ = os.Unsetenv("PORT")
|
||||
_ = os.Unsetenv("HOST")
|
||||
_ = os.Unsetenv("DB_PATH")
|
||||
_ = os.Unsetenv("GIN_MODE")
|
||||
_ = os.Unsetenv("CORS_ALLOW_ORIGINS")
|
||||
_ = os.Unsetenv("AUTO_MIGRATE")
|
||||
|
||||
cfg := config.Load()
|
||||
assert.Equal(t, 8080, cfg.Port)
|
||||
assert.Equal(t, "0.0.0.0", cfg.Host)
|
||||
assert.Equal(t, "anl.db", cfg.DBPath)
|
||||
assert.Equal(t, "release", cfg.GinMode)
|
||||
assert.Equal(t, "*", cfg.CORSAllowOrigins)
|
||||
assert.True(t, cfg.AutoMigrate)
|
||||
assert.Equal(t, "0.0.0.0:8080", cfg.Address())
|
||||
}
|
||||
|
||||
func TestConfigEnvOverrides(t *testing.T) {
|
||||
_ = os.Setenv("PORT", "9090")
|
||||
_ = os.Setenv("HOST", "127.0.0.1")
|
||||
_ = os.Setenv("DB_PATH", "/tmp/custom.db")
|
||||
_ = os.Setenv("GIN_MODE", "debug")
|
||||
_ = os.Setenv("CORS_ALLOW_ORIGINS", "https://anl.knu.ac.kr")
|
||||
_ = os.Setenv("AUTO_MIGRATE", "false")
|
||||
defer func() {
|
||||
_ = os.Unsetenv("PORT")
|
||||
_ = os.Unsetenv("HOST")
|
||||
_ = os.Unsetenv("DB_PATH")
|
||||
_ = os.Unsetenv("GIN_MODE")
|
||||
_ = os.Unsetenv("CORS_ALLOW_ORIGINS")
|
||||
_ = os.Unsetenv("AUTO_MIGRATE")
|
||||
}()
|
||||
|
||||
cfg := config.Load()
|
||||
assert.Equal(t, 9090, cfg.Port)
|
||||
assert.Equal(t, "127.0.0.1", cfg.Host)
|
||||
assert.Equal(t, "/tmp/custom.db", cfg.DBPath)
|
||||
assert.Equal(t, "debug", cfg.GinMode)
|
||||
assert.Equal(t, "https://anl.knu.ac.kr", cfg.CORSAllowOrigins)
|
||||
assert.False(t, cfg.AutoMigrate)
|
||||
assert.Equal(t, "127.0.0.1:9090", cfg.Address())
|
||||
}
|
||||
|
||||
func TestLoadEnvFile(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
envPath := filepath.Join(tmpDir, ".env")
|
||||
envContent := `
|
||||
# Sample comment
|
||||
TEST_CUSTOM_KEY="hello_world"
|
||||
TEST_PORT_KEY=8888
|
||||
INVALID_LINE_WITHOUT_EQUALS
|
||||
`
|
||||
err := os.WriteFile(envPath, []byte(envContent), 0644)
|
||||
require.NoError(t, err)
|
||||
|
||||
_ = os.Unsetenv("TEST_CUSTOM_KEY")
|
||||
_ = os.Unsetenv("TEST_PORT_KEY")
|
||||
defer func() {
|
||||
_ = os.Unsetenv("TEST_CUSTOM_KEY")
|
||||
_ = os.Unsetenv("TEST_PORT_KEY")
|
||||
}()
|
||||
|
||||
config.LoadEnvFile(envPath)
|
||||
assert.Equal(t, "hello_world", os.Getenv("TEST_CUSTOM_KEY"))
|
||||
assert.Equal(t, "8888", os.Getenv("TEST_PORT_KEY"))
|
||||
}
|
||||
Reference in New Issue
Block a user