Compare commits
No commits in common. "d432521dae5a7d415ae4fab8894f692d6aa4d6d6" and "2658b187974efc42ca22b3ab7e3399d466d61510" have entirely different histories.
d432521dae
...
2658b18797
35
README.md
35
README.md
@ -43,6 +43,8 @@ services:
|
|||||||
counter:
|
counter:
|
||||||
image: registry.deadbeef.codes/siteviewcounter:latest
|
image: registry.deadbeef.codes/siteviewcounter:latest
|
||||||
restart: always
|
restart: always
|
||||||
|
depends_on:
|
||||||
|
- traefik
|
||||||
expose:
|
expose:
|
||||||
- "8080"
|
- "8080"
|
||||||
environment:
|
environment:
|
||||||
@ -65,35 +67,4 @@ services:
|
|||||||
- MYSQL_DATABASE=counter
|
- MYSQL_DATABASE=counter
|
||||||
- TZ=America/Edmonton
|
- TZ=America/Edmonton
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Example front end usage
|
|
||||||
|
|
||||||
You can pretty much implement this in your front end however you want, you just need to make a GET request to whatever endpoint the counter container is running at. This is how I use it though...
|
|
||||||
|
|
||||||
```html
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<script>
|
|
||||||
var counterReq = new XMLHttpRequest();
|
|
||||||
counterReq.onreadystatechange = function() {
|
|
||||||
console.log("counterReq ready state is " + this.readyState);
|
|
||||||
if (this.readyState == 4) {
|
|
||||||
console.log("counterReq status is " + this.status);
|
|
||||||
if (this.status == 200) {
|
|
||||||
document.getElementById("counter").innerHTML = this.responseText + " unique visitors"
|
|
||||||
} else { // failed to load
|
|
||||||
console.log("failed to load counter module")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
counterReq.open("GET", "https://counter.example.com", true);
|
|
||||||
counterReq.send();
|
|
||||||
</script>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<div id="counter"></div>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
```
|
|
||||||
|
|
4
main.go
4
main.go
@ -78,9 +78,7 @@ func main() {
|
|||||||
// HTTP handler function
|
// HTTP handler function
|
||||||
func countHandler(w http.ResponseWriter, r *http.Request) {
|
func countHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == "GET" {
|
if r.Method == "GET" {
|
||||||
// CORS header change required.
|
// CORS header change required
|
||||||
//TBD wildcard is bad because it could allow illegitmate visits to be recorded if someone was nefarious and embedded
|
|
||||||
// front end code on a different website than your own. Need to implement environment variable to set allowed origin.
|
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
w.Write([]byte(strconv.Itoa(uniqueVisits)))
|
w.Write([]byte(strconv.Itoa(uniqueVisits)))
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user