HTTP basic auth, make website more configurable

This commit is contained in:
Anthony Wang 2024-05-24 03:23:27 +00:00
parent 6f551cd1d9
commit 0c4865e2e4
Signed by: a
SSH key fingerprint: SHA256:B5ADfMCqd2M7d/jtXDoihAV/yfXOAbWWri9+GdCN4hQ
7 changed files with 38 additions and 30 deletions

View file

@ -10,10 +10,14 @@ import (
)
type Config struct {
Database string
UnixSocket string
Service []scanner.ServiceConfig
Matrix MatrixConfig
SiteName string
IconSrc string
Database string
UnixSocket string
BasicAuthUsername string
BasicAuthPassword string
Service []scanner.ServiceConfig
Matrix MatrixConfig
}
func LoadConfigFromTOML(configPath string) (*Config, error) {

15
main.go
View file

@ -26,7 +26,7 @@ var staticAssets embed.FS
//go:embed all:templates
var templateAssets embed.FS
func parseAndRenderTemplate(templateName string, data interface{}) (string, error) {
func parseAndRenderTemplate(templateName, siteName, iconSrc string, data interface{}) (string, error) {
rawTemplate, err := templateAssets.ReadFile(templateName)
if err != nil {
return "", err
@ -38,12 +38,11 @@ func parseAndRenderTemplate(templateName string, data interface{}) (string, erro
return "", err
}
res, err := tmpl.Render(data)
res, err := tmpl.Render(map[string]interface{}{"SiteName": siteName, "IconSrc": iconSrc, "Data": data})
if err != nil {
log.Println("Failed to render template:", err)
return "", err
}
return res, nil
}
@ -63,7 +62,7 @@ func index(w http.ResponseWriter, r *http.Request) {
timeout := time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
data := scanner.CheckServiceBatch(ctx, config.Service)
data := scanner.CheckServiceBatch(ctx, config.Service, config.BasicAuthUsername, config.BasicAuthPassword)
non_duplicates, err := dbm.StoreServiceStatusBatch(data, time.Now())
if err != nil {
http.Error(w, err.Error(), 500)
@ -88,7 +87,7 @@ func index(w http.ResponseWriter, r *http.Request) {
if (!accept_html || prefer_text) && accept_text {
indexForVT100(w, r, data)
} else if accept_html {
indexForBrowser(w, r, data)
indexForBrowser(w, r, data, config.SiteName, config.IconSrc)
} else { // when no valid content type is requested
// not to-spec. still sends plain text anyway
indexForVT100(w, r, data)
@ -101,8 +100,8 @@ func indexForVT100(w http.ResponseWriter, r *http.Request, data scanner.ServiceS
w.Write([]byte(parsedTemplate))
}
func indexForBrowser(w http.ResponseWriter, r *http.Request, data scanner.ServiceStatusBatch) {
parsedTemplate, err := parseAndRenderTemplate("templates/index.html", data)
func indexForBrowser(w http.ResponseWriter, r *http.Request, data scanner.ServiceStatusBatch, siteName, iconSrc string) {
parsedTemplate, err := parseAndRenderTemplate("templates/index.html", siteName, iconSrc, data)
if err != nil {
http.Error(w, err.Error(), 500)
return
@ -233,7 +232,7 @@ func routineCheck(timeout time.Duration) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
data := scanner.CheckServiceBatch(ctx, config.Service)
data := scanner.CheckServiceBatch(ctx, config.Service, config.BasicAuthUsername, config.BasicAuthPassword)
non_duplicates, err := dbm.StoreServiceStatusBatch(data, time.Now())
if err != nil {
return err

View file

@ -1,5 +1,9 @@
# unixSocket = "/srv/http/pages/status"
siteName = "Status Page"
iconSrc = "https://exozy.me/img/logo.svg"
# unixSocket = "/srv/http/status"
# database = "/opt/status-config/db"
# basicAuthUsername = "guest"
# basicAuthPassword = ""
database = "/tmp/exozyme-status-test-db"
service = [
@ -10,7 +14,7 @@ service = [
]
[matrix]
userId = "@exozymebot76397:matrix.org"
userId = "@bot:exozy.me"
# accessToken = "nil"
roomId = "!fXEYZcqHcbjztwNhhD:exozy.me"
homeserver = "https://chat.exozy.me"

View file

@ -6,7 +6,7 @@ body {
text-align: center;
}
.headerDiv {
header {
display: flex;
flex-direction: column;
align-items: center;
@ -89,7 +89,7 @@ body {
opacity: 1;
}
.footer {
footer {
justify-content: center;
margin-top: 2rem;
}

View file

@ -6,11 +6,12 @@ import (
"net/http"
)
func CheckHTTP200(ctx Context, url string) (bool, string) {
func CheckHTTP200(ctx Context, url, basicAuthUsername, basicAuthPassword string) (bool, string) {
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return false, err.Error()
}
req.SetBasicAuth(basicAuthUsername, basicAuthPassword)
resp, err := http.DefaultClient.Do(req)
if err != nil {
var tlsCertErr x509.CertificateInvalidError

View file

@ -30,14 +30,14 @@ type ServiceStatus struct {
Status string `json:"status"`
}
func CheckService(service ServiceConfig, ctx context.Context) ServiceStatus {
func CheckService(service ServiceConfig, ctx context.Context, basicAuthUsername, basicAuthPassword string) ServiceStatus {
var serviceResult bool
var status string
url := service.URL()
switch url.Scheme {
case "http", "https":
serviceResult, status = CheckHTTP200(ctx, url.String())
serviceResult, status = CheckHTTP200(ctx, url.String(), basicAuthUsername, basicAuthPassword)
case "tcp":
// go includes port in host (url.Host is actually "authority")
serviceResult, status = CheckTCPOpen(ctx, url.Host)
@ -59,7 +59,7 @@ func CheckService(service ServiceConfig, ctx context.Context) ServiceStatus {
type ServiceStatusBatch []ServiceStatus
func CheckServiceBatch(ctx Context, services []ServiceConfig) ServiceStatusBatch {
func CheckServiceBatch(ctx Context, services []ServiceConfig, basicAuthUsername, basicAuthPassword string) ServiceStatusBatch {
var wg sync.WaitGroup
var data []ServiceStatus = make([]ServiceStatus, len(services))
@ -68,7 +68,7 @@ func CheckServiceBatch(ctx Context, services []ServiceConfig) ServiceStatusBatch
wg.Add(1)
go func(i int, config ServiceConfig) {
defer wg.Done()
data[i] = CheckService(config, ctx)
data[i] = CheckService(config, ctx, basicAuthUsername, basicAuthPassword)
}(i, v)
}
wg.Wait()

View file

@ -3,18 +3,18 @@
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>exozyme - status </title>
<title>{{SiteName}}</title>
<link rel="stylesheet" href="/public/index.css">
</head>
<body>
<div class="headerDiv">
<img src="https://exozy.me/img/logo.svg" alt="exozyme logo" width="180">
<h1>exozyme status</h1>
</div>
<header>
<img src="{{IconSrc}}" alt="exozyme logo" width="180">
<h1>{{SiteName}}</h1>
</header>
<ul class="serviceContainer">
{{#.}}
{{#Data}}
<li>
<div class="service {{#Ok}}serviceUp{{/Ok}}{{^Ok}}serviceDown{{/Ok}}">
<div>
@ -30,13 +30,13 @@
</div>
</div>
</li>
{{/.}}
{{/Data}}
</ul>
<div class="footer">
<footer>
<p><a href="https://git.exozy.me/exozyme/status">Source code</a></p>
<p> Made with ❤️ by the exozyme community!</p>
</div>
</footer>
</body>
</html>