Initialize basic API

This commit is contained in:
Kirby 2024-03-14 16:11:58 +01:00
parent 2b08ca3d3f
commit a2109224c2
6 changed files with 109 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.idea/*
*.swp
*.log
infra-dashboard

15
Http/utils.go Normal file
View File

@ -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")
}

26
Tools/utils.go Normal file
View File

@ -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))
}

11
go.mod Normal file
View File

@ -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

20
go.sum Normal file
View File

@ -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=

33
main.go Normal file
View File

@ -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))
}