Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fb4862c078 | ||
|
|
f3dbd4505f | ||
|
|
2462c6057e | ||
|
|
cda95aa4f2 | ||
|
|
b7e448df8d |
38
.github/workflows/docker-image.yml
vendored
38
.github/workflows/docker-image.yml
vendored
@@ -26,26 +26,32 @@ jobs:
|
||||
id: timestamp
|
||||
run: echo "TIMESTAMP=$(date +%Y%m%d%H%M%S)" >> $GITHUB_ENV
|
||||
|
||||
- name: Get the commit tag
|
||||
id: get-tag
|
||||
run: echo "GIT_TAG=$(git describe --tags --exact-match 2>/dev/null || echo 'no-tag')" >> $GITHUB_ENV
|
||||
- name: Get commit version
|
||||
id: commit-version
|
||||
run: |
|
||||
COMMIT_MSG=$(git log -1 --pretty=%B)
|
||||
echo "Commit message: $COMMIT_MSG" # Debugging output
|
||||
# Updated regex to handle both vX.Y, vX.Y.Z, and vX.Y-pre-release formats
|
||||
if [[ "$COMMIT_MSG" =~ ^v[0-9]+\.[0-9]+(\.[0-9]+)?(-[a-zA-Z0-9._-]+)?$ ]]; then
|
||||
echo "Version match: $COMMIT_MSG"
|
||||
echo "VERSION=${COMMIT_MSG}" >> $GITHUB_ENV
|
||||
else
|
||||
echo "No version match, defaulting to 'dev'"
|
||||
echo "VERSION=dev" >> $GITHUB_ENV
|
||||
fi
|
||||
|
||||
- name: Build Docker image
|
||||
run: |
|
||||
if [ "${{ env.GIT_TAG }}" == "no-tag" ]; then
|
||||
docker build -t ti1:dev-${{ env.TIMESTAMP }} .
|
||||
else
|
||||
docker build -t ti1:latest -t ti1:${{ env.GIT_TAG }} .
|
||||
fi
|
||||
docker build -t ti1:${{ env.VERSION }} .
|
||||
|
||||
- name: Push Docker image
|
||||
run: |
|
||||
if [ "${{ env.GIT_TAG }}" == "no-tag" ]; then
|
||||
docker tag ti1:dev-${{ env.TIMESTAMP }} ${{ secrets.DOCKER_USERNAME }}/ti1:dev-${{ env.TIMESTAMP }}
|
||||
docker push ${{ secrets.DOCKER_USERNAME }}/ti1:dev-${{ env.TIMESTAMP }}
|
||||
else
|
||||
docker tag ti1:latest ${{ secrets.DOCKER_USERNAME }}/ti1:latest
|
||||
docker tag ti1:${{ env.GIT_TAG }} ${{ secrets.DOCKER_USERNAME }}/ti1:${{ env.GIT_TAG }}
|
||||
docker push ${{ secrets.DOCKER_USERNAME }}/ti1:latest
|
||||
docker push ${{ secrets.DOCKER_USERNAME }}/ti1:${{ env.GIT_TAG }}
|
||||
# Always push to 'dev' tag
|
||||
docker tag ti1:${{ env.VERSION }} ${{ secrets.DOCKER_USERNAME }}/ti1:dev
|
||||
docker push ${{ secrets.DOCKER_USERNAME }}/ti1:dev
|
||||
|
||||
# If the version is valid, also push that specific version tag
|
||||
if [[ "${{ env.VERSION }}" != "dev" ]]; then
|
||||
docker tag ti1:${{ env.VERSION }} ${{ secrets.DOCKER_USERNAME }}/ti1:${{ env.VERSION }}
|
||||
docker push ${{ secrets.DOCKER_USERNAME }}/ti1:${{ env.VERSION }}
|
||||
fi
|
||||
|
||||
138
README.md
138
README.md
@@ -4,19 +4,141 @@ The best thing to happen since yesterday at 3 pm
|
||||
|
||||
## Usage
|
||||
|
||||
To use this project, you can pull the Docker image from Docker Hub and run it using the following commands:
|
||||
Start with getting Docker then do the following:
|
||||
|
||||
### Pull the Docker Image
|
||||
### Create the setup files
|
||||
Create a `docker-compose.yml`
|
||||
```yaml
|
||||
services:
|
||||
db:
|
||||
image: postgres:17.2
|
||||
container_name: postgres-db
|
||||
environment:
|
||||
POSTGRES_USER: postgres
|
||||
POSTGRES_PASSWORD: Root Password
|
||||
POSTGRES_DB: ti1
|
||||
ports:
|
||||
- "5432:5432"
|
||||
volumes:
|
||||
- ./postgres_data:/var/lib/postgresql/data # Store data in the current directory
|
||||
- ./init.sql:/docker-entrypoint-initdb.d/init.sql:ro
|
||||
networks:
|
||||
- app-network
|
||||
healthcheck:
|
||||
test: ["CMD", "pg_isready", "-U", "postgres", "-d", "ti1", "-h", "db"]
|
||||
interval: 10s
|
||||
retries: 5
|
||||
restart: always # Ensure the container always restarts
|
||||
|
||||
```sh
|
||||
docker pull pigwin1/ti1:latest
|
||||
ti1-container:
|
||||
image: pigwin1/ti1:v0.1.1
|
||||
container_name: ti1-container
|
||||
environment:
|
||||
DB_HOST: db
|
||||
DB_PORT: 5432
|
||||
DB_USER: ti1
|
||||
DB_PASSWORD: ti1 password
|
||||
DB_NAME: ti1
|
||||
DB_SSLMODE: disable
|
||||
depends_on:
|
||||
db:
|
||||
condition: service_healthy # Wait until the db service is healthy
|
||||
networks:
|
||||
- app-network
|
||||
restart: always # Ensure the container always restarts
|
||||
|
||||
networks:
|
||||
app-network:
|
||||
driver: bridge
|
||||
|
||||
volumes:
|
||||
postgres_data:
|
||||
driver: local
|
||||
```
|
||||
|
||||
### Run the Docker Container
|
||||
```sh
|
||||
docker run -d --name ti1-container -e DB_HOST=<your_db_host> -e DB_PORT=<your_db_port> -e DB_USER=<your_db_user> -e DB_PASSWORD=<your_db_password> -e DB_NAME=<your_db_name> -e DB_SSLMODE=<your_db_sslmode> pigwin1/ti1:latest
|
||||
Create `init.sql`
|
||||
```sql
|
||||
-- Check if 'post' user exists; create if not
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'post') THEN
|
||||
CREATE ROLE post WITH LOGIN PASSWORD 'post password';
|
||||
GRANT ALL PRIVILEGES ON DATABASE ti1 TO post;
|
||||
ALTER ROLE post WITH SUPERUSER;
|
||||
END IF;
|
||||
END
|
||||
$$;
|
||||
|
||||
-- Check if 'ti1' user exists; create if not
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'ti1') THEN
|
||||
CREATE ROLE ti1 WITH LOGIN PASSWORD 'ti1 password';
|
||||
GRANT ALL PRIVILEGES ON DATABASE ti1 TO ti1;
|
||||
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO ti1;
|
||||
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO ti1;
|
||||
GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public TO ti1;
|
||||
-- Grant the ti1 user the necessary permissions on the public schema
|
||||
GRANT USAGE, CREATE ON SCHEMA public TO ti1;
|
||||
|
||||
-- Grant all permissions (SELECT, INSERT, UPDATE, DELETE, etc.) on all existing tables in the public schema
|
||||
GRANT ALL ON ALL TABLES IN SCHEMA public TO ti1;
|
||||
|
||||
-- Grant all permissions on all existing sequences in the public schema
|
||||
GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO ti1;
|
||||
|
||||
-- Grant all permissions on all functions in the public schema
|
||||
GRANT ALL ON ALL FUNCTIONS IN SCHEMA public TO ti1;
|
||||
|
||||
-- Ensure that the ti1 user will have access to new tables, sequences, and functions created in the public schema
|
||||
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO ti1;
|
||||
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO ti1;
|
||||
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON FUNCTIONS TO ti1;
|
||||
|
||||
-- Optionally, grant full permissions on the entire database to ti1 (if needed)
|
||||
-- GRANT ALL PRIVILEGES ON DATABASE ti1 TO ti1;
|
||||
|
||||
END IF;
|
||||
END
|
||||
$$;
|
||||
```
|
||||
|
||||
Remember to change the password values
|
||||
|
||||
### Run the Docker Containers
|
||||
```sh
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
### edit the postgress config (optinal)
|
||||
open the config file
|
||||
```sh
|
||||
nano postgres_data/postgresql.conf
|
||||
```
|
||||
Change the following values
|
||||
```conf
|
||||
listen_addresses = '*'
|
||||
max_connections = 100
|
||||
shared_buffers = 16GB
|
||||
work_mem = 256MB
|
||||
maintenance_work_mem = 2GB
|
||||
dynamic_shared_memory_type = posix
|
||||
max_wal_size = 1GB
|
||||
min_wal_size = 80MB
|
||||
```
|
||||
set these to what makes most sense for you
|
||||
|
||||
These values should also be set bet not necessarily changed
|
||||
```conf
|
||||
log_timezone = 'Etc/UTC'
|
||||
datestyle = 'iso, mdy'
|
||||
timezone = 'Etc/UTC'
|
||||
lc_messages = 'en_US.utf8'
|
||||
lc_monetary = 'en_US.utf8'
|
||||
lc_numeric = 'en_US.utf8'
|
||||
lc_time = 'en_US.utf8'
|
||||
default_text_search_config = 'pg_catalog.english'
|
||||
```
|
||||
Replace `<your_db_host>`, `<your_db_port>`, `<your_db_user>`, `<your_db_password>`, `<your_db_name>`, and `<your_db_sslmode>` with your actual database configuration values.
|
||||
|
||||
### Docker Hub Repository
|
||||
You can find the Docker image on Docker Hub at the following link:
|
||||
|
||||
@@ -125,10 +125,12 @@ type Data struct {
|
||||
} `xml:"ServiceDelivery"`
|
||||
}
|
||||
|
||||
func FetchData() (*Data, error) {
|
||||
func FetchData(timestamp string) (*Data, error) {
|
||||
client := &http.Client{}
|
||||
requestorId := "ti1-" + timestamp
|
||||
|
||||
resp, err := client.Get("https://api.entur.io/realtime/v1/rest/et?useOriginalId=true&maxSize=100000")
|
||||
url := "https://api.entur.io/realtime/v1/rest/et?useOriginalId=true&maxSize=100000&requestorId=" + requestorId
|
||||
resp, err := client.Get(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
8
main.go
8
main.go
@@ -17,10 +17,14 @@ func main() {
|
||||
log.Fatalf("Database setup failed: %v", err)
|
||||
}
|
||||
|
||||
// Get the current timestamp
|
||||
starttimestamp := time.Now().Format("20060102T150405")
|
||||
log.Printf("Starting timestamp: %s", starttimestamp)
|
||||
|
||||
for {
|
||||
start := time.Now()
|
||||
|
||||
data, err := data.FetchData()
|
||||
data, err := data.FetchData(starttimestamp)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@@ -31,7 +35,7 @@ func main() {
|
||||
elapsed := time.Since(start)
|
||||
if elapsed < 5*time.Minute {
|
||||
log.Printf("starting again in %v", 5*time.Minute-elapsed)
|
||||
time.Sleep(1*time.Minute - elapsed)
|
||||
time.Sleep(5*time.Minute - elapsed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user