diff options
| author | Alexander Kavon <hawk@alexkavon.com> | 2023-11-13 23:20:13 -0500 |
|---|---|---|
| committer | Alexander Kavon <hawk@alexkavon.com> | 2023-11-13 23:20:13 -0500 |
| commit | 6cfc9a99c046f2bc52d3a976aff03fdcd4b94180 (patch) | |
| tree | 0607f8f8b6ecadbf43864d1d29cd622e6e25653e | |
router and config loader
| -rw-r--r-- | .newsstandrc.toml.example | 3 | ||||
| -rw-r--r-- | conf/conf.go | 34 | ||||
| -rw-r--r-- | go.mod | 8 | ||||
| -rw-r--r-- | go.sum | 4 | ||||
| -rw-r--r-- | main.go | 19 |
5 files changed, 68 insertions, 0 deletions
diff --git a/.newsstandrc.toml.example b/.newsstandrc.toml.example new file mode 100644 index 0000000..6d20ede --- /dev/null +++ b/.newsstandrc.toml.example @@ -0,0 +1,3 @@ +DB_ADAPTER = "postgresql" +DB_USER = "newsstand" +DB_PASS = "newsstand" diff --git a/conf/conf.go b/conf/conf.go new file mode 100644 index 0000000..b9dadcd --- /dev/null +++ b/conf/conf.go @@ -0,0 +1,34 @@ +package conf + +import ( + "log" + "os" + "github.com/BurntSushi/toml" +) + +type Conf struct { + DbAdapter string `toml:"DB_ADAPTER"` + DbUser string `toml:"DB_USER"` + DbPassword string `toml:"DB_PASS"` +} + +func Load() *Conf { + filepath := os.Getenv("NEWSSTAND_CONFIG_PATH") + if filepath == "" { + workingdir, err := os.Getwd() + if err != nil { + log.Fatal(err) + } + filepath = workingdir + "/.newsstandrc.toml" + } + log.Printf("Config file path: %s", filepath) + + var c Conf + _, err := toml.DecodeFile(filepath, &c) + if err != nil { + log.Fatalln(err) + } + log.Println(c) + + return &c +} @@ -0,0 +1,8 @@ +module gitlab.com/alexkavon/newsstand + +go 1.21.3 + +require ( + github.com/BurntSushi/toml v1.3.2 // indirect + github.com/go-chi/chi/v5 v5.0.10 // indirect +) @@ -0,0 +1,4 @@ +github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= +github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= +github.com/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk= +github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= @@ -0,0 +1,19 @@ +package main + +import ( + "net/http" + "gitlab.com/alexkavon/newsstand/conf" + "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" +) + +func main() { + // load config + conf.Load() + r := chi.NewRouter() + r.Use(middleware.Logger) + r.Get("/", func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("Yello There!")) + }) + http.ListenAndServe(":8080", r) +} |
