From a2109224c2adc243bcbdeaaf54e6045ebbd160af Mon Sep 17 00:00:00 2001 From: Kirby Date: Thu, 14 Mar 2024 16:11:58 +0100 Subject: [PATCH] Initialize basic API --- .gitignore | 4 ++++ Http/utils.go | 15 +++++++++++++++ Tools/utils.go | 26 ++++++++++++++++++++++++++ go.mod | 11 +++++++++++ go.sum | 20 ++++++++++++++++++++ main.go | 33 +++++++++++++++++++++++++++++++++ 6 files changed, 109 insertions(+) create mode 100644 .gitignore create mode 100644 Http/utils.go create mode 100644 Tools/utils.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cc6bb6a --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea/* +*.swp +*.log +infra-dashboard diff --git a/Http/utils.go b/Http/utils.go new file mode 100644 index 0000000..706adce --- /dev/null +++ b/Http/utils.go @@ -0,0 +1,15 @@ +package http + +import ( + "fmt" + "io" + "net/http" +) + +func RequestHandler(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, "Hello, World!") +} + +func HealthHandler(w http.ResponseWriter, _ *http.Request) { + _, _ = io.WriteString(w, "OK") +} diff --git a/Tools/utils.go b/Tools/utils.go new file mode 100644 index 0000000..7afc7b6 --- /dev/null +++ b/Tools/utils.go @@ -0,0 +1,26 @@ +package Tools + +import ( + "log" + "os" + + "github.com/joho/godotenv" +) + +// ReadDotEnvFile reads environment variables from .env file +func ReadDotEnvFile(f string) { + err := godotenv.Load(f) + if err != nil { + log.Fatal("Error loading .env file") + } +} + +// InitLog ensure log file exists and set appropriate flags (remove timestamp at start of line). +func InitLog(p string) { + logFile, err := os.OpenFile(p, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + panic(err) + } + log.SetOutput(logFile) + log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime)) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..823ca4b --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module infra-dashboard + +go 1.20 + +require ( + github.com/alexflint/go-arg v1.4.3 + github.com/gorilla/mux v1.8.1 + github.com/joho/godotenv v1.5.1 +) + +require github.com/alexflint/go-scalar v1.1.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..97aa0af --- /dev/null +++ b/go.sum @@ -0,0 +1,20 @@ +github.com/alexflint/go-arg v1.4.3 h1:9rwwEBpMXfKQKceuZfYcwuc/7YY7tWJbFsgG5cAU/uo= +github.com/alexflint/go-arg v1.4.3/go.mod h1:3PZ/wp/8HuqRZMUUgu7I+e1qcpUbvmS258mRXkFH4IA= +github.com/alexflint/go-scalar v1.1.0 h1:aaAouLLzI9TChcPXotr6gUhq+Scr8rl0P9P4PnltbhM= +github.com/alexflint/go-scalar v1.1.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..e6078eb --- /dev/null +++ b/main.go @@ -0,0 +1,33 @@ +// Receive and send informations about servers +package main + +import ( + h "infra-dashboard/Http" + tools "infra-dashboard/Tools" + "log" + "net/http" + + "github.com/alexflint/go-arg" + "github.com/gorilla/mux" +) + +var ( + args struct { + Log string `arg:"-l,--logfile" help:"location of output logfile." default:"/app/http-broadcaster.log"` + EnvFile string `arg:"-e,--envfile" help:"location of file containing environment variables." default:".env"` + Metrics bool `arg:"--metrics" help:"enable prometheus exporter on /metrics." default:"false"` + } +) + +func main() { + arg.MustParse(&args) + tools.InitLog(args.Log) + tools.ReadDotEnvFile(args.EnvFile) + + router := mux.NewRouter() + + router.HandleFunc("/", h.RequestHandler) + router.HandleFunc("/healthcheck", h.HealthHandler) + + log.Fatal(http.ListenAndServe(":8080", router)) +}