Skip to content
Open
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
25 changes: 12 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
.DEFAULT_GOAL := build

.PHONY: fmt lint run build clear
fmt:
@go fmt ./...
BINARYNAME := xkcd-server
SERVERPATH := ./cmd/http

lint: fmt
.PHONY: style test/race build run
style:
@go fmt ./...
@golangci-lint run ./...

build: lint
@go build -race -o xkcd ./cmd/xkcd/

run: lint
@go run -race ./cmd/xkcd/
test/race:
@go build -race -o ${BINARYNAME} ${SERVERPATH}
@rm ./${BINARYNAME}

clear:
@rm ./xkcd
build:
@go build -o ${BINARYNAME} ${SERVERPATH}

clearModel:
@rm ./internal/resources/spellchecker/savedModel
run:
@go run ${SERVERPATH}
76 changes: 76 additions & 0 deletions cmd/http/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main

import (
"context"
"github.com/PavlushaSource/yadro-practice-course/internal/adapter/config"
"github.com/PavlushaSource/yadro-practice-course/internal/adapter/handler/http"
"github.com/PavlushaSource/yadro-practice-course/internal/adapter/logger"
"github.com/PavlushaSource/yadro-practice-course/internal/adapter/storage/json/repository"
"github.com/PavlushaSource/yadro-practice-course/internal/core/service"
"github.com/PavlushaSource/yadro-practice-course/pkg/words/spellcheck"
"github.com/PavlushaSource/yadro-practice-course/pkg/words/stemmer"
"log"
"log/slog"
"os"
"os/signal"
)

func main() {

// Read cfg
cfg, err := config.New()
if err != nil {
log.Fatal(err)
}

// Set Log
slog.SetDefault(logger.SetupLogger(cfg.App.Env))

// Init DB

DB := repository.NewComicRepository(cfg.JSONFlat.DBFilepath)
indexDB := repository.NewIndexRepository(cfg.JSONFlat.IndexFilepath)

// ctx with interruption
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()

// Dependency injection

// Normalize
slog.Info("Initialize stemmer")
st, err := stemmer.NewSnowballStemmer(cfg.Normalize.StopwordsPath)
if err != nil {
slog.Error("Initialize stemmer failed", "error", err)
os.Exit(1)
}
ch := spellcheck.NewFuzzyChecker(
cfg.Spellchecker.ModelPath,
cfg.Spellchecker.AllWordsPath,
[]string{cfg.Spellchecker.DictPathEn, cfg.Spellchecker.DictPathRus},
)
normalizeService := service.NewNormalizeService(st, ch)

// Comic
slog.Info("Initialize and try download comics")
comicService := service.NewComicsService(indexDB, DB, normalizeService, cfg)
comicHandler := http.NewComicHandler(comicService)

_, err = comicService.DownloadAll(ctx)
if err != nil {
slog.Error("Download failed", "error", err)
os.Exit(1)
}

// Run server
srv, err := http.NewRouter(cfg, comicHandler, ctx)
if err != nil {
slog.Error("Initialize server failed", "error", err)
os.Exit(1)
}
err = srv.Serve(cfg.HTTP.UpdateInterval, ctx, comicService)
if err != nil {
slog.Error("Run server failed", "error", err)
os.Exit(1)
}
}
160 changes: 0 additions & 160 deletions cmd/xkcd/main.go

This file was deleted.

38 changes: 33 additions & 5 deletions configs/config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
env: "local" # local, dev, prod
db_file: "database.json"
source_url: "https://xkcd.com"
parallel: 50 # TODO провести исследование на число worker'ов
batch_size: 100
App:
env: local

HTTP:
host: localhost
port: 8080
update_interval: 1m

DB:
host: localhost
port: 5432
username: sqlite
password: sqlite
db_name: xkcd
connections: 1

JSONFlat:
index_path: index.json
file_path: database.json

ComicsSource:
url: https://xkcd.com
parallel: 50
batch_size: 100

Spellchecker:
model_path: pkg/assets/resources/spellchecker/savedModel
dict_path_en: bpkg/assets/resources/words/dictionaries/10000-english.txt
dict_path_rus: pkg/assets/resources/words/dictionaries/10000-russian.txt
all_words: pkg/assets/resources/words/dictionaries/all-words.txt

Normalize:
stopword_path: pkg/assets/resources/words/stopwords/stopwords-iso.json
Loading