Skip to content

Commit

Permalink
Merge pull request #4 from thedmdim/refactor
Browse files Browse the repository at this point in the history
refactor and telegram bot
  • Loading branch information
thedmdim authored May 15, 2024
2 parents 9d35f8b + 5ab09ec commit ffccacd
Show file tree
Hide file tree
Showing 22 changed files with 315 additions and 96 deletions.
4 changes: 2 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DSN=mydbname.db
DB_POOL_SIZE=100
YT_API_KEY=ALzaSyAWJfmC4-ZSNd0ntT3even_tryMA9B7tBQ
LETSENCRYPT_EMAIL=me@email.com
YT_API_KEYS=ALzaSyAWJfmC4-ZSNd0ntT3even_tryMA9B7tBQ,AIzaSyAUwQwQsherenxjjLTSVtheH0d1vOsam0o
LETSENCRYPT_EMAIL=me@email.com
56 changes: 56 additions & 0 deletions .github/workflows/build-tele.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: build image, publish and deploy

on:
push:
branches:
- master
paths:
- 'cmd/tele/**'

jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- uses: actions/checkout@v3
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- uses: docker/build-push-action@v5
with:
context: .
file: Dockerfile.tele
push: true
tags: ghcr.io/thedmdim/ytstalker/tele:latest
cache-from: type=gha
cache-to: type=gha,mode=max

deploy:
needs: build
name: deploy image
runs-on: ubuntu-latest

steps:
- name: prepare ssh
run: |
mkdir -p ~/.ssh
echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan -H "$SERVER_IP" >> ~/.ssh/known_hosts
env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
SERVER_IP: ${{ secrets.SERVER_IP }}

- name: SSH into the server and update the code
run: |
ssh $SERVER_USERNAME@$SERVER_IP "wget -qO - https://raw.githubusercontent.com/thedmdim/ytstalker/master/docker-compose.yml > /root/ytstalker/docker-compose.yml && docker compose -f /root/ytstalker/docker-compose.yml up --pull always -d tele"
env:
SERVER_USERNAME: ${{ secrets.SERVER_USERNAME }}
SERVER_IP: ${{ secrets.SERVER_IP }}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ on:
push:
branches:
- master
paths-ignore:
- README.md
paths:
- 'cmd/app/**'

jobs:
publish:
build:
runs-on: ubuntu-latest
permissions:
contents: read
Expand All @@ -27,12 +27,12 @@ jobs:
with:
context: .
push: true
tags: ghcr.io/thedmdim/ytstalker:latest
tags: ghcr.io/thedmdim/ytstalker/app:latest
cache-from: type=gha
cache-to: type=gha,mode=max

deploy:
needs: publish
needs: build
name: deploy image
runs-on: ubuntu-latest

Expand All @@ -49,7 +49,7 @@ jobs:

- name: SSH into the server and update the code
run: |
ssh $SERVER_USERNAME@$SERVER_IP "wget -qO - https://raw.githubusercontent.com/thedmdim/ytstalker/master/docker-compose.yml > /root/ytstalker/docker-compose.yml && docker compose -f /root/ytstalker/docker-compose.yml up --pull always -d ytstalker"
ssh $SERVER_USERNAME@$SERVER_IP "wget -qO - https://raw.githubusercontent.com/thedmdim/ytstalker/master/docker-compose.yml > /root/ytstalker/docker-compose.yml && docker compose -f /root/ytstalker/docker-compose.yml up --pull always -d app"
env:
SERVER_USERNAME: ${{ secrets.SERVER_USERNAME }}
SERVER_IP: ${{ secrets.SERVER_IP }}
7 changes: 3 additions & 4 deletions Dockerfile → Dockerfile.app
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ FROM golang:1.21 as builder
WORKDIR /usr/src/ytstalker
COPY go.mod go.sum .
RUN go mod download
COPY app app/
RUN ls
RUN CGO_ENABLED=0 go build -v -o /usr/bin/ytstalker/app ./app
COPY cmd cmd/
RUN CGO_ENABLED=0 go build -v -o /usr/bin/ytstalker/app ./cmd/app

FROM gcr.io/distroless/static-debian11
WORKDIR /usr/bin/ytstalker
COPY --from=builder /usr/bin/ytstalker/app .
COPY web /usr/bin/ytstalker/web/
COPY --from=builder /usr/bin/ytstalker/app .
EXPOSE 80
ENTRYPOINT ["/usr/bin/ytstalker/app"]
11 changes: 11 additions & 0 deletions Dockerfile.tele
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM golang:1.21 as builder
WORKDIR /usr/src/ytstalker
COPY go.mod go.sum .
RUN go mod download
COPY cmd cmd/
RUN CGO_ENABLED=0 go build -v -o /usr/bin/ytstalker/tele ./cmd/tele

FROM gcr.io/distroless/static-debian11
WORKDIR /usr/bin/ytstalker
COPY --from=builder /usr/bin/ytstalker/tele .
ENTRYPOINT ["/usr/bin/ytstalker/tele"]
16 changes: 0 additions & 16 deletions README.md

This file was deleted.

35 changes: 0 additions & 35 deletions app/conf/conf.go

This file was deleted.

32 changes: 20 additions & 12 deletions app/handlers/random_hander.go → cmd/app/handlers/random_hander.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handlers

import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
Expand Down Expand Up @@ -47,6 +48,8 @@ type SearchCriteria struct {
Musiconly bool
}

var ErrNoVideoFound = errors.New("no video found")

func ParseQueryParams(params url.Values) *SearchCriteria {

sc := &SearchCriteria{}
Expand Down Expand Up @@ -132,11 +135,18 @@ func (sc *SearchCriteria) CheckVideo(video *Video) bool {
return true
}

func (s *Router) TakeFirstUnseen(conn *sqlite.Conn, sc *SearchCriteria, visitor string) (*Video, error) {
func TakeFirstUnseen(conn *sqlite.Conn, visitor string, sc *SearchCriteria) (*Video, error) {

video := &Video{}

stmt, _, err := conn.PrepareTransient(fmt.Sprintf(`
var where string
if sc != nil {
where = sc.MakeWhere()
}

visitor = strings.ReplaceAll(visitor, " ", "")

stmt, err := conn.Prepare(fmt.Sprintf(`
SELECT id, uploaded, title, views, vertical, category
FROM videos
WHERE id NOT IN (
Expand All @@ -147,7 +157,7 @@ func (s *Router) TakeFirstUnseen(conn *sqlite.Conn, sc *SearchCriteria, visitor
ORDER BY random()
LIMIT 1`,
visitor,
sc.MakeWhere(),
where,
))
if err != nil {
return nil, fmt.Errorf("error preparing query: %w", err)
Expand All @@ -157,7 +167,7 @@ func (s *Router) TakeFirstUnseen(conn *sqlite.Conn, sc *SearchCriteria, visitor
return nil, err
}
if !rows {
return nil, nil
return nil, ErrNoVideoFound
}

video.ID = stmt.GetText("id")
Expand All @@ -175,7 +185,7 @@ func (s *Router) TakeFirstUnseen(conn *sqlite.Conn, sc *SearchCriteria, visitor
return video, nil
}

func (s *Router) RememberSeen(conn *sqlite.Conn, visitorId string, videoId string) error {
func RememberSeen(conn *sqlite.Conn, visitorId string, videoId string) error {

endFn, err := sqlitex.ImmediateTransaction(conn)
if err != nil {
Expand All @@ -193,8 +203,9 @@ func (s *Router) RememberSeen(conn *sqlite.Conn, visitorId string, videoId strin
if _, err = stmt.Step(); err != nil {
return err
}
stmt.ClearBindings(); stmt.Reset()

stmt, err = conn.Prepare(`INSERT INTO videos_visitors (visitor_id, video_id) VALUES (?, ?);`)
stmt = conn.Prep(`INSERT INTO videos_visitors (visitor_id, video_id) VALUES (?, ?);`)
if err != nil {
return fmt.Errorf("error preparing query: %w", err)
}
Expand All @@ -203,10 +214,7 @@ func (s *Router) RememberSeen(conn *sqlite.Conn, visitorId string, videoId strin
if _, err = stmt.Step(); err != nil {
return err
}
err = stmt.Reset()
if err != nil {
return err
}
stmt.ClearBindings(); stmt.Reset()

return nil
}
Expand All @@ -223,7 +231,7 @@ func (s *Router) GetRandom(w http.ResponseWriter, r *http.Request) {
defer s.db.Put(conn)

searchCriteria := ParseQueryParams(params)
video, err := s.TakeFirstUnseen(conn, searchCriteria, visitor)
video, err := TakeFirstUnseen(conn, visitor, searchCriteria)
if err != nil {
log.Println("take first unseen failed:", err.Error())
}
Expand All @@ -240,7 +248,7 @@ func (s *Router) GetRandom(w http.ResponseWriter, r *http.Request) {
response.Reactions = reactions

encoder.Encode(response)
err = s.RememberSeen(conn, visitor, video.ID)
err = RememberSeen(conn, visitor, video.ID)
if err != nil {
log.Println("error remembering seen:", err.Error())
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
20 changes: 13 additions & 7 deletions app/main.go → cmd/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@ import (
"syscall"
"time"

"ytstalker/app/conf"
"ytstalker/app/handlers"
"ytstalker/app/youtube"
"ytstalker/cmd/app/handlers"
"ytstalker/cmd/app/youtube"

"zombiezen.com/go/sqlite"
"zombiezen.com/go/sqlite/sqlitex"
)

func main() {
config := conf.ParseConfig()

// prepare db
db, err := sqlitex.NewPool(config.DSN, sqlitex.PoolOptions{PoolSize: config.DbPoolSize})
dsn := os.Getenv("DSN")
if dsn == "" {
dsn = "server.db"
}

db, err := sqlitex.NewPool(dsn, sqlitex.PoolOptions{PoolSize: 100})
if err != nil {
log.Fatal("cannot open db", err)
}
Expand All @@ -35,7 +37,11 @@ func main() {
log.Println("database ready")

// init youtube api requester
ytr := youtube.NewYouTubeRequester(config)
ytApiKey := os.Getenv("YT_API_KEY")
if ytApiKey == "" {
log.Fatal("You forgot to provide YouTube API key!")
}
ytr := youtube.NewYouTubeRequester(ytApiKey)

// make router
handler := handlers.NewRouter(db)
Expand Down
File renamed without changes.
8 changes: 3 additions & 5 deletions app/youtube/api.go → cmd/app/youtube/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@ import (
"net/http"
"net/url"
"strings"

"ytstalker/app/conf"
)

type YouTubeRequester struct {
noRedirectClient *http.Client
conf *conf.Config
token string
baseUrl string
}

func NewYouTubeRequester(conf *conf.Config) *YouTubeRequester {
func NewYouTubeRequester(token string) *YouTubeRequester {
return &YouTubeRequester{
baseUrl: "https://www.googleapis.com/youtube/v3",
conf: conf,
token: token,
noRedirectClient: &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion app/youtube/utils.go → cmd/app/youtube/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Video struct {
func (y *YouTubeRequester) Request(req *http.Request) (*http.Response, error) {

q := req.URL.Query()
q.Add("key", y.conf.YtApiKey)
q.Add("key", y.token)
req.URL.RawQuery = q.Encode()
res, err := http.DefaultClient.Do(req)
if err != nil {
Expand Down
Loading

0 comments on commit ffccacd

Please sign in to comment.