Use a config file instead of env vars
This commit is contained in:
parent
2f60c81690
commit
954de8b120
4
auth.go
4
auth.go
|
@ -19,9 +19,9 @@ func (u userList) lookup(t string) (username string){
|
|||
}
|
||||
|
||||
func (u userList) loadFromFile(f string) {
|
||||
uf,err := os.ReadFile("users.txt")
|
||||
uf,err := os.ReadFile(f)
|
||||
if err != nil {
|
||||
fmt.Println("Could not find or open users.txt")
|
||||
fmt.Printf("Could not find or open %s", f)
|
||||
os.Exit(1)
|
||||
}
|
||||
b := strings.Split(string(uf),"\n")
|
||||
|
|
1
go.mod
1
go.mod
|
@ -16,4 +16,5 @@ require (
|
|||
github.com/pkg/errors v0.9.1 // indirect
|
||||
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
|
||||
golang.org/x/text v0.3.7 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
|
2
go.sum
2
go.sum
|
@ -170,4 +170,6 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
|||
gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
|
||||
|
|
|
@ -5,7 +5,6 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
@ -43,7 +42,7 @@ func (mt MessageText) Scan(src interface{}) error {
|
|||
func logsHandler (w http.ResponseWriter, r *http.Request){
|
||||
rr := rq{w,r}
|
||||
if isAuthed,user := testAuth(rr,users); isAuthed {
|
||||
con,err := pgx.Connect(context.Background(), os.Getenv("QL_DB_STRING"))
|
||||
con,err := pgx.Connect(context.Background(), config.Database)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
|
31
main.go
31
main.go
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type rq struct {
|
||||
|
@ -11,21 +12,31 @@ type rq struct {
|
|||
r *http.Request
|
||||
}
|
||||
|
||||
type conf struct {
|
||||
Database string
|
||||
Listenport string
|
||||
Userlist string
|
||||
}
|
||||
|
||||
var users userList
|
||||
var config conf
|
||||
|
||||
func main() {
|
||||
users = make(userList)
|
||||
users.loadFromFile("users.txt")
|
||||
http.HandleFunc("/logs/", logsHandler)
|
||||
listenPort := "8087"
|
||||
if os.Getenv("QL_LISTEN_PORT") != "" {
|
||||
listenPort = os.Getenv("QL_LISTEN_PORT")
|
||||
}
|
||||
if os.Getenv("QL_DB_STRING") == ""{
|
||||
fmt.Println("require environment variable for postgres connection string: QL_DB_STRING")
|
||||
|
||||
confPath := os.Args[1]
|
||||
|
||||
rawconf, err := os.ReadFile(confPath)
|
||||
if err != nil {
|
||||
fmt.Println("Could not read config file")
|
||||
os.Exit(1)
|
||||
}
|
||||
if err := http.ListenAndServe(":"+listenPort, nil); err != nil {
|
||||
|
||||
yaml.Unmarshal(rawconf,&config)
|
||||
|
||||
users = make(userList)
|
||||
users.loadFromFile(config.Userlist)
|
||||
http.HandleFunc("/logs/", logsHandler)
|
||||
if err := http.ListenAndServe(":"+config.Listenport, nil); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue