Skip to content

Commit

Permalink
Merge branch 'bugCount'
Browse files Browse the repository at this point in the history
  • Loading branch information
gernotstarke committed Nov 24, 2023
2 parents eed2ba4 + 50d84f1 commit dbe26f5
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/fly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ jobs:
- run: flyctl deploy --remote-only
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
GITHUB_API_KEY: ${{ secrets.GITHUB_API_KEY }}
GITHUB_API_KEY: ${{ secrets.GRAPHQL_API_TOKEN }}
32 changes: 24 additions & 8 deletions internal/api/apiGateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const PortNr = ":8043"
// HomeIP is needed to deploy on fly.io
const homeIP = "0.0.0.0"

const TemplatesDir = "./web"
const TemplatesDir = ""
const HtmlTableTmpl = "arc42statistics.gohtml"
const PingTmpl = "ping.gohtml"

Expand Down Expand Up @@ -106,29 +106,38 @@ func executeTemplate(w http.ResponseWriter, templatePath string, data any) {

tpl, err := template.ParseFS(embeddedTemplatesFolder, templatePath)
if err != nil {
log.Printf("parsing template: %v", err)
http.Error(w, "There was an error parsing the template {#err}.", http.StatusInternalServerError)
log.Printf("Error parsing template: %v", err)
http.Error(w, "There was an error parsing the template "+err.Error(), http.StatusInternalServerError)
return
}
err = tpl.Execute(w, data)
if err != nil {
log.Printf("executing template: %v", err)
http.Error(w, "There was an error executing the template {#err}.", http.StatusInternalServerError)
log.Printf("Error executing template: %v", err)
http.Error(w, "There was an error executing the template "+err.Error(), http.StatusInternalServerError)
return
}
}

func logRequestHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
h.ServeHTTP(w, r)
log.Printf("%s %s %v", r.Method, r.URL, time.Since(start))
})
}

// PrintServerDetails displays a few details about this program,
// mainly to give admins some idea what version is currently running
// and where in the fly.io cloud the service is deployed.
func PrintServerDetails(appVersion string) {

fmt.Printf("Started API server, version %s on Port %s at %s\n", appVersion, getPort(), time.Now().Format("2. January 2006, 15:04h"))
fmt.Printf("Starting API server, version %s on Port %s at %s\n", appVersion, getPort(), time.Now().Format("2. January 2006, 15:04h"))

// assumes we're running this program within the fly.io cloud.
// There, the env variable FLY_REGION should be set.
// If this variable is empty, we assume we're running locally
fly.RegionAndLocation()
region, location := fly.RegionAndLocation()
fmt.Printf("Server region is %s/%s", region, location)
}

// StartAPIServer creates an http ServeMux with a few predefined routes.
Expand All @@ -142,7 +151,14 @@ func StartAPIServer() {
mux.HandleFunc("/stats", statsHTMLTableHandler)
mux.HandleFunc("/ping", pingHandler)

// wrap ServeMux with logging
loggedMux := logRequestHandler(mux)

// TODO why are we setting HomeIP?
log.Fatal(http.ListenAndServe(homeIP+getPort(), mux))
err := http.ListenAndServe(homeIP+getPort(), loggedMux)

if err != nil {
log.Fatalf("API server failed to start: %v", err)
}

}
6 changes: 3 additions & 3 deletions internal/api/arc42statistics.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
<br>
<div style="font-size: 12px; padding-bottom: 14px;">
Data collected in {{ .HowLongDidItTake }} msecs by arc42 statistics service
(v. {{.AppVersion}}) at {{.LastUpdatedString }} running on
<a href="https://fly.io" target="_blank"> fly.io </a>
(v. {{.AppVersion}}) at {{.LastUpdatedString }}
{{if .FlyRegion}}
in {{.WhereDoesItRun}} (region {{.FlyRegion}})
running on <a href="https://fly.io" target="_blank"> fly.io </a>
in {{.WhereDoesItRun}} (region {{.FlyRegion}})
{{end }}
</div>
2 changes: 1 addition & 1 deletion badge/badges.go → internal/badge/badges.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package badges
package badge

const ShieldsGithubIssuesURL = "https://img.shields.io/github/issues-raw/arc42/"

Expand Down
6 changes: 3 additions & 3 deletions internal/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package domain

import (
"fmt"
badges "site-usage-statistics/badge"
"site-usage-statistics/internal/badge"
"site-usage-statistics/internal/github"
"site-usage-statistics/internal/plausible"
"site-usage-statistics/internal/types"
Expand Down Expand Up @@ -60,8 +60,8 @@ func setURLsForSite(stats *types.SiteStats) {
stats.Repo = github.GithubArc42URL + stats.Site + "-site"

// shields.io issues URLS look like that: https://img.shields.io/github/issues-raw/arc42/arc42.org-site
stats.IssueBadgeURL = badges.ShieldsGithubIssuesURL + stats.Site + "-site"
stats.IssueBadgeURL = badge.ShieldsGithubIssuesURL + stats.Site + "-site"

// shields.io bug URLS look like that:https://img.shields.io/github/issues-search/arc42/quality.arc42.org-site?query=label%3Abug%20is%3Aopen&label=bugs&color=red
stats.BugBadgeURL = badges.ShieldsGithubBugsURLPrefix + stats.Site + "-site" + badges.ShieldsBugSuffix
stats.BugBadgeURL = badge.ShieldsGithubBugsURLPrefix + stats.Site + "-site" + badge.ShieldsBugSuffix
}
2 changes: 1 addition & 1 deletion internal/fly/flyHosting.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func RegionAndLocation() (string, string) {
return region, flyRegionCodeToLocation(region)
}

// FlyRegion retrieves the fly.io regiono from the envirionment variable FLY_REGION
// FlyRegion retrieves the fly.io region from the environment variable FLY_REGION
func flyRegion() string {
region := os.Getenv("FLY_REGION")
if region == "" {
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func main() {

// Start a server which runs in background and waits for http requests to arrive
// on predefined routes.
api.StartAPIServer()
// THIS IS A BLOCKING CALL!
api.PrintServerDetails(AppVersion)
api.StartAPIServer()
}

0 comments on commit dbe26f5

Please sign in to comment.