Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stats Saver service #1

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ Each service uses environment variables to e.g. to get the connection data of th
* `JWT_SECRET` The secret key for JWT.
* This should be secure and must be identical in all URL-Shorter services.

### Stats Saver
* `POSTGRES_HOST` The hostname with port for the PostgreSQL database (e.g., `127.0.0.1:5432`)
* `POSTGRES_USERNAME` The username for the PostgreSQL database authentication
* Write access is required!
* `POSTGRES_PASSWORD` The password for the PostgreSQL database authentication
* `POSTGRES_DATABASE` The database name where the statistics are stored.
* `HTTP_PORT` This is the port on which the HTTP server is running.
* If this environment variable is not passed, port `3000` is used.
* **This service does not have to be publicly accessible, only the Query Services need access!**

## Kubernetes

If you want to run URL Shorter in a Kubernetes cluster, you can use the provided [manifest files](manifests/).
Expand Down
27 changes: 27 additions & 0 deletions stats-saver/database/postgres.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package database

import (
"database/sql"
"fmt"
"log"
"os"
)

var db *sql.DB

func ConnectToPostgres() {
log.Println("Connecting to PostgreSQL...")
connectionString := fmt.Sprintf(
"postgresql://%s:%s@%s/%s",
os.Getenv("POSTGRES_USERNAME"),
os.Getenv("POSTGRES_PASSWORD"),
os.Getenv("POSTGRES_HOST"),
os.Getenv("POSTGRES_DATABASE"),
)
var err error
db, err = sql.Open("postgres", connectionString)
if err != nil {
log.Fatalln(err)
}
log.Println("Connected to PostgreSQL!")
}
25 changes: 25 additions & 0 deletions stats-saver/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
package main

import (
"fmt"
"log"
"net/http"
"os"
"strconv"

"github.com/Dominik48N/url-shorter/stats-saver/database"
"github.com/julienschmidt/httprouter"
)

const apiVersion = "v1"

func main() {
database.ConnectToPostgres()

router := httprouter.New()

log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", getHttpPort()), router))
}

func getHttpPort() int {
port, err := strconv.Atoi(os.Getenv("HTTP_PORT"))
if err != nil {
return 3000
}
return port
}
23 changes: 20 additions & 3 deletions users/database/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,38 @@ func ConnectToPostgres() {
}

func InsertUser(username string, password string) error {
_, err := db.Exec("INSERT INTO users (username, password) VALUES ($1, $2)", username, password)
stmt, err := db.Prepare("INSERT INTO users (username, password) VALUES ($1, $2)")
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(username, password)
return err
}

func GetPassword(username string) (string, error) {
stmt, err := db.Prepare("SELECT password FROM users WHERE username = $1 LIMIT 1")
if err != nil {
return "", err
}
defer stmt.Close()

var password string
err := db.QueryRow("SELECT password FROM users WHERE username = $1 LIMIT 1", username).Scan(&password)
err = stmt.QueryRow(username).Scan(&password)
if err != nil {
return "", err
}
return password, nil
}

func IsUserNameExists(username string) (bool, error) {
err := db.QueryRow("SELECT username FROM users WHERE username = $1 LIMIT 1", username).Scan()
stmt, err := db.Prepare("SELECT username FROM users WHERE username = $1 LIMIT 1")
if err != nil {
return false, err
}
defer stmt.Close()

err = stmt.QueryRow(username).Scan()
if err == nil {
return true, nil
}
Expand Down