From 6cfc9a99c046f2bc52d3a976aff03fdcd4b94180 Mon Sep 17 00:00:00 2001 From: Alexander Kavon Date: Mon, 13 Nov 2023 23:20:13 -0500 Subject: router and config loader --- .newsstandrc.toml.example | 3 +++ conf/conf.go | 34 ++++++++++++++++++++++++++++++++++ go.mod | 8 ++++++++ go.sum | 4 ++++ main.go | 19 +++++++++++++++++++ 5 files changed, 68 insertions(+) create mode 100644 .newsstandrc.toml.example create mode 100644 conf/conf.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go 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 +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ff596ad --- /dev/null +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..361c46c --- /dev/null +++ b/go.sum @@ -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= diff --git a/main.go b/main.go new file mode 100644 index 0000000..3c2fe5e --- /dev/null +++ b/main.go @@ -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) +} -- cgit v1.2.3