up
This commit is contained in:
+20
-4
@@ -14,7 +14,7 @@ import (
|
|||||||
// Config holds all application configuration.
|
// Config holds all application configuration.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Database DatabaseConfig `yaml:"database"`
|
Database DatabaseConfig `yaml:"database"`
|
||||||
Port string `yaml:"port"`
|
Web WebConfig `yaml:"web"`
|
||||||
Path string `yaml:"path"`
|
Path string `yaml:"path"`
|
||||||
Secret string `yaml:"secret"`
|
Secret string `yaml:"secret"`
|
||||||
}
|
}
|
||||||
@@ -25,6 +25,12 @@ type DatabaseConfig struct {
|
|||||||
DSN string `yaml:"dsn"` // MySQL connection string (required when type is "mysql")
|
DSN string `yaml:"dsn"` // MySQL connection string (required when type is "mysql")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WebConfig holds web-server listening configuration.
|
||||||
|
type WebConfig struct {
|
||||||
|
Port string `yaml:"port"` // TCP port, "" or "0" to disable
|
||||||
|
Socket string `yaml:"socket"` // Unix socket path, "" to disable
|
||||||
|
}
|
||||||
|
|
||||||
const defaultPort = "8080"
|
const defaultPort = "8080"
|
||||||
|
|
||||||
// mysqlExampleDSN is written into new config files as a reference.
|
// mysqlExampleDSN is written into new config files as a reference.
|
||||||
@@ -64,6 +70,13 @@ func generateSecret() string {
|
|||||||
return fmt.Sprintf("%x", hash)
|
return fmt.Sprintf("%x", hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getDefaultSocketPath returns the OS-aware default unix socket path.
|
||||||
|
func getDefaultSocketPath() string {
|
||||||
|
if runtime.GOOS == "linux" {
|
||||||
|
return "/opt/blog_go/web.sock"
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
// LoadConfig reads the config file or creates one with defaults.
|
// LoadConfig reads the config file or creates one with defaults.
|
||||||
func LoadConfig() *Config {
|
func LoadConfig() *Config {
|
||||||
configDir, configFile := getConfigPath()
|
configDir, configFile := getConfigPath()
|
||||||
@@ -81,7 +94,10 @@ func LoadConfig() *Config {
|
|||||||
Type: "sqlite",
|
Type: "sqlite",
|
||||||
DSN: mysqlExampleDSN,
|
DSN: mysqlExampleDSN,
|
||||||
},
|
},
|
||||||
Port: defaultPort,
|
Web: WebConfig{
|
||||||
|
Port: defaultPort,
|
||||||
|
Socket: getDefaultSocketPath(),
|
||||||
|
},
|
||||||
Path: defaultPath,
|
Path: defaultPath,
|
||||||
Secret: generateSecret(),
|
Secret: generateSecret(),
|
||||||
}
|
}
|
||||||
@@ -116,8 +132,8 @@ func LoadConfig() *Config {
|
|||||||
|
|
||||||
// applyDefaults fills zero-value fields with sensible defaults.
|
// applyDefaults fills zero-value fields with sensible defaults.
|
||||||
func applyDefaults(cfg *Config, defaultPath string) *Config {
|
func applyDefaults(cfg *Config, defaultPath string) *Config {
|
||||||
if cfg.Port == "" {
|
if cfg.Web.Port == "" {
|
||||||
cfg.Port = defaultPort
|
cfg.Web.Port = defaultPort
|
||||||
}
|
}
|
||||||
if cfg.Database.Type == "" {
|
if cfg.Database.Type == "" {
|
||||||
cfg.Database.Type = "sqlite"
|
cfg.Database.Type = "sqlite"
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/gin-contrib/sessions"
|
"github.com/gin-contrib/sessions"
|
||||||
"github.com/gin-contrib/sessions/cookie"
|
"github.com/gin-contrib/sessions/cookie"
|
||||||
@@ -161,9 +163,39 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 9. Start the server.
|
// 9. Start the server.
|
||||||
addr := fmt.Sprintf(":%s", cfg.Port)
|
webPort := cfg.Web.Port
|
||||||
log.Printf("Go Blog starting on http://localhost%s", addr)
|
socketPath := cfg.Web.Socket
|
||||||
if err := router.Run(addr); err != nil {
|
usePort := webPort != "" && webPort != "0"
|
||||||
log.Fatalf("Failed to start server: %v", err)
|
useSocket := socketPath != ""
|
||||||
|
|
||||||
|
if !usePort && !useSocket {
|
||||||
|
log.Fatalf("Neither port nor socket is configured — at least one must be enabled")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if usePort {
|
||||||
|
go func() {
|
||||||
|
addr := fmt.Sprintf(":%s", webPort)
|
||||||
|
log.Printf("Go Blog starting on http://localhost%s", addr)
|
||||||
|
if err := router.Run(addr); err != nil {
|
||||||
|
log.Fatalf("Failed to start HTTP server: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
if useSocket {
|
||||||
|
go func() {
|
||||||
|
os.Remove(socketPath) // remove stale socket file if exists
|
||||||
|
listener, err := net.Listen("unix", socketPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to listen on unix socket %s: %v", socketPath, err)
|
||||||
|
}
|
||||||
|
log.Printf("Go Blog starting on unix socket %s", socketPath)
|
||||||
|
if err := router.RunListener(listener); err != nil {
|
||||||
|
log.Fatalf("Failed to serve on unix socket: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Block forever.
|
||||||
|
select {}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user