Initial work on parsing config file

This commit is contained in:
Dan Jones 2023-03-23 13:03:16 -05:00
commit 996f59c8af
4 changed files with 51 additions and 4 deletions

39
config/config.go Normal file
View file

@ -0,0 +1,39 @@
package config
import (
"github.com/BurntSushi/toml"
"github.com/kirsle/configdir"
"path/filepath"
)
type Config struct {
Domain string `toml:"domain"`
Username string `toml:"username"`
Password string `toml:"admin_password"`
Name string `toml:"name"`
Summary string `toml:"summary"`
Https bool `toml:"https"`
IconUrl string `toml:"icon_url""`
ImageUrl string `toml:"image_url"`
Secret string `toml:"secret"`
TrustedHosts []string `toml:"trusted_hosts"`
ManuallyApproveFollers bool `toml:"manually_approves_followers"`
}
var config Config
func init() {
configPath := configdir.LocalConfig("gopub")
if err := configdir.MakePath(configPath); err != nil {
panic(err)
}
configFile := filepath.Join(configPath, "profile.toml")
if _, err := toml.DecodeFile(configFile, &config); err != nil {
panic(err)
}
}
func GetConfig() Config {
return config
}

6
go.mod
View file

@ -2,7 +2,11 @@ module codeberg.org/danjones000/gopub
go 1.19 go 1.19
require github.com/gofiber/fiber/v2 v2.42.0 require (
github.com/BurntSushi/toml v1.2.1
github.com/gofiber/fiber/v2 v2.42.0
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
)
require ( require (
github.com/andybalholm/brotli v1.0.4 // indirect github.com/andybalholm/brotli v1.0.4 // indirect

4
go.sum
View file

@ -1,9 +1,13 @@
github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY= github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/gofiber/fiber/v2 v2.42.0 h1:Fnp7ybWvS+sjNQsFvkhf4G8OhXswvB6Vee8hM/LyS+8= github.com/gofiber/fiber/v2 v2.42.0 h1:Fnp7ybWvS+sjNQsFvkhf4G8OhXswvB6Vee8hM/LyS+8=
github.com/gofiber/fiber/v2 v2.42.0/go.mod h1:3+SGNjqMh5VQH5Vz2Wdi43zTIV16ktlFd3x3R6O1Zlc= github.com/gofiber/fiber/v2 v2.42.0/go.mod h1:3+SGNjqMh5VQH5Vz2Wdi43zTIV16ktlFd3x3R6O1Zlc=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f h1:dKccXx7xA56UNqOcFIbuqFjAWPVtP688j5QMgmo6OHU=
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f/go.mod h1:4rEELDSfUAlBSyUjPG0JnaNGjf13JySHFeRdD/3dLP0=
github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY= github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY=
github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU= github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=

View file

@ -3,16 +3,16 @@ package main
import ( import (
"log" "log"
"codeberg.org/danjones000/gopub/config"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
func main() { func main() {
app := fiber.New() app := fiber.New()
config := config.GetConfig()
app.Get("/", func(c *fiber.Ctx) error { app.Get("/", func(c *fiber.Ctx) error {
return c.JSON(&fiber.Map{ return c.JSON(&config)
"Hello": "World",
})
}) })
log.Fatal(app.Listen(":3000")) log.Fatal(app.Listen(":3000"))