siteviewcounter/README.md

100 lines
2.4 KiB
Markdown
Raw Permalink Normal View History

2020-06-04 02:37:59 +00:00
# siteviewcounter
2020-06-04 02:40:13 +00:00
[![Build Status](https://drone.deadbeef.codes/api/badges/steven/siteviewcounter/status.svg)](https://drone.deadbeef.codes/steven/siteviewcounter)
A simple view counter for a website
### Requirements
* Go
* Docker
* Docker Compose (Optional) or Kubernetes (Optional)
### Build Application
```bash
go build -a -ldflags '-w'
```
2020-06-21 02:30:09 +00:00
### Build Container
2020-06-21 02:50:44 +00:00
Disclaimer! If you use this, you'll need to build the container yourself. I have a CICD pipeline setup, but my registry is used for my internal infrastructure only and is not publicly available.
Because this is a staticly linked binary with no external runtime dependancies, the container literally only contains the binary file, keeping it clean and low in size (6.3MB). I never did understand why people include operating systems in containers.
2020-06-21 02:50:44 +00:00
```bash
docker build -t siteviewconter:latest .
```
2020-06-21 02:30:09 +00:00
2020-06-04 02:40:13 +00:00
### Example docker-compose.yml
2020-06-21 02:30:09 +00:00
Create a docker-compose file
2020-06-04 02:40:13 +00:00
```yaml
version: '3.7'
services:
counter:
2020-06-21 02:50:44 +00:00
image: siteviewcounter:latest
2020-06-04 02:40:13 +00:00
restart: always
expose:
- "8080"
environment:
- dbname=counter
- dbhostname=counter-db
- dbusername=root
- dbpassword=CHANGEME
- timezone=America/Edmonton
counter-db:
image: mariadb:10
restart: always
expose:
- "3306"
volumes:
- /data/counter-db:/var/lib/mysql
environment:
- MYSQL_RANDOM_ROOT_PASSWORD=yes
- MYSQL_DATABASE=counter
- TZ=America/Edmonton
2020-06-21 02:42:03 +00:00
```
### 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>
```