Basic REST app

This commit is contained in:
2026-03-30 18:26:45 +02:00
commit 42f9b974b2
17 changed files with 309 additions and 0 deletions
+80
View File
@@ -0,0 +1,80 @@
package config
import (
"os"
"gopkg.in/yaml.v3"
)
type GlobalConfig struct {
Token string `yaml:"token"`
GithubPat string `yaml:"github_pat"`
StoragePath string `yaml:"storage_path"`
}
type SiteConfig struct {
Repo string `yaml:"repo"`
Owner string `yaml:"owner"`
Branch string `yaml:"branch"`
Domain string `yaml:"domain"`
}
type Config struct {
Global GlobalConfig `yaml:"global"`
Sites []SiteConfig `yaml:"sites"`
}
type Error struct {
Field string
Message string
}
func (c Error) Error() string {
return "Config error: " + c.Field + " - " + c.Message
}
func validateConfig(config *Config) error {
if config.Global.Token == "" {
return &Error{Field: "global.token", Message: "Token is required"}
}
if config.Global.GithubPat == "" {
return &Error{Field: "global.github_pat", Message: "GitHub PAT is required"}
}
if config.Global.StoragePath == "" {
return &Error{Field: "global.storage_path", Message: "Storage path is required"}
}
for i, site := range config.Sites {
if site.Repo == "" {
return &Error{Field: "sites[" + string(i) + "].repo", Message: "Repo is required"}
}
if site.Owner == "" {
return &Error{Field: "sites[" + string(i) + "].owner", Message: "Owner is required"}
}
if site.Branch == "" {
return &Error{Field: "sites[" + string(i) + "].branch", Message: "Branch is required"}
}
if site.Domain == "" {
return &Error{Field: "sites[" + string(i) + "].domain", Message: "Domain is required"}
}
}
return nil
}
func Load(path string) (*Config, error) {
f, err := os.ReadFile(path)
if err != nil {
return nil, err
}
expanded := os.Expand(string(f), os.Getenv)
var config Config
err = yaml.Unmarshal([]byte(expanded), &config)
if err != nil {
return nil, err
}
err = validateConfig(&config)
if err != nil {
return nil, err
}
return &config, nil
}
+18
View File
@@ -0,0 +1,18 @@
package envconfig
import "os"
type EnvConfig struct {
Port string
}
func Load() EnvConfig {
port := os.Getenv("PORT")
if port == "" {
port = "4321"
}
return EnvConfig{
Port: port,
}
}
+18
View File
@@ -0,0 +1,18 @@
package fiberconfig
import (
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/logger"
)
func Setup(app *fiber.App) {
app.Use(func(c fiber.Ctx) error {
c.Set("Content-Type", "application/json")
return c.Next()
})
app.Use(logger.New(logger.Config{
Format: "[${time}] ${status} - ${method} ${path} (${latency}) - ${ip}\n",
TimeFormat: "2006-01-02 15:04:05",
}))
}