feat:#34 refactored function to intended service, fixed docker files

This commit is contained in:
Elmar Kresse
2024-06-10 16:57:40 +02:00
parent cb76b5c188
commit 2d7701b0c9
96 changed files with 212 additions and 79 deletions

View File

@ -33,33 +33,60 @@ lint-frontend:
- npm i
- npm run lint-no-fix
lint-backend:
lint-data-manager:
stage: lint
image: golangci/golangci-lint:latest
rules:
- changes:
- backend/**/*
- services/data-manager/**/*
script:
- cd backend
- cd services/data-manager
- go mod download
- golangci-lint --version
- golangci-lint run -v --skip-dirs=migrations --timeout=5m
lint-ical:
stage: lint
image: golangci/golangci-lint:latest
rules:
- changes:
- services/ical/**/*
script:
- cd services/ical
- go mod download
- golangci-lint --version
- golangci-lint run -v --skip-dirs=migrations --timeout=5m
build-backend:
build-data-manager:
image: golang:alpine
stage: build
rules:
- changes:
- backend/**/*
- services/data-manager/**/*
script:
- cd backend
- cd services/data-manager
- go build -o htwkalender
artifacts:
paths:
- backend/htwkalender
- backend/go.sum
- backend/go.mod
- data-manager/htwkalender
- data-manager/go.sum
- data-manager/go.mod
build-ical:
image: golang:alpine
stage: build
rules:
- changes:
- services/ical/**/*
script:
- cd services/ical
- go build -o htwkalender-ical
artifacts:
paths:
- data-manager/htwkalender-ical
- data-manager/go.sum
- data-manager/go.mod
build-frontend:
image: node:lts
@ -75,17 +102,29 @@ build-frontend:
paths:
- frontend/build
test-backend:
test-data-manager:
image: golang:alpine
stage: test
rules:
- changes:
- backend/**/*
- services/data-manager/**/*
script:
- cd backend
- cd services/data-manager
- go test -v ./...
dependencies:
- build-backend
- build-data-manager
test-ical:
image: golang:alpine
stage: test
rules:
- changes:
- services/ical/**/*
script:
- cd services/ical
- go test -v ./...
dependencies:
- build-ical
test-frontend:
image: node:lts
@ -100,7 +139,7 @@ test-frontend:
dependencies:
- lint-frontend
build-backend-image:
build-data-manager-image:
stage: oci-build
image: docker:latest
services:
@ -108,7 +147,7 @@ build-backend-image:
tags:
- image
variables:
IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-backend
IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-data-manager
DOCKER_HOST: tcp://docker:2376
DOCKER_TLS_CERTDIR: "/certs"
DOCKER_TLS_VERIFY: 1
@ -116,12 +155,35 @@ build-backend-image:
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
script:
- docker build --pull -t $IMAGE_TAG -f ./backend/Dockerfile --target prod ./backend
- docker build --pull -t $IMAGE_TAG -f ./services/data-manager/Dockerfile --target prod ./services/data-manager
- docker push $IMAGE_TAG
rules:
- if: $CI_COMMIT_BRANCH == "main" || $CI_COMMIT_BRANCH == "development"
changes:
- backend/**/*
- services/data-manager/**/*
build-ical-image:
stage: oci-build
image: docker:latest
services:
- docker:dind
tags:
- image
variables:
IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-ical
DOCKER_HOST: tcp://docker:2376
DOCKER_TLS_CERTDIR: "/certs"
DOCKER_TLS_VERIFY: 1
DOCKER_CERT_PATH: "/certs/client"
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
script:
- docker build --pull -t $IMAGE_TAG -f ./services/ical/Dockerfile --target prod ./services/ical
- docker push $IMAGE_TAG
rules:
- if: $CI_COMMIT_BRANCH == "main" || $CI_COMMIT_BRANCH == "development"
changes:
- services/ical/**/*
build-frontend-image:
stage: oci-build

View File

@ -15,7 +15,7 @@
#along with this program. If not, see <https://www.gnu.org/licenses/>.
services:
htwkalender-backend:
htwkalender-data-manager:
image: DOCKER_REGISTRY_REPO-backend # DOCKER_REGISTRY_REPO will be replaced by CI
command: "--http=0.0.0.0:8090 --dir=/htwkalender/data/pb_data"
pull_policy: always
@ -25,12 +25,21 @@ services:
networks:
- "net"
htwkalender-ical:
image: DOCKER_REGISTRY_REPO-ical # DOCKER_REGISTRY_REPO will be replaced by CI
pull_policy: always
restart: always
target: prod
networks:
- "net"
htwkalender-frontend:
image: DOCKER_REGISTRY_REPO-frontend # DOCKER_REGISTRY_REPO will be replaced by CI
pull_policy: always
restart: always
depends_on:
- htwkalender-backend
- htwkalender-data-manager
- htwkalender-ical
networks:
- "net"
@ -42,8 +51,9 @@ services:
- ./dev_htwkalender_de.pem:/opt/bitnami/nginx/conf/dev_htwkalender_de.pem
- ./dev_htwkalender_de.key.pem:/opt/bitnami/nginx/conf/dev_htwkalender_de.key.pem
depends_on:
- htwkalender-backend
- htwkalender-data-manager
- htwkalender-frontend
- htwkalender-ical
ports:
- "443:443"
- "80:80"

View File

@ -15,7 +15,7 @@
#along with this program. If not, see <https://www.gnu.org/licenses/>.
services:
htwkalender-backend:
htwkalender-data-manager:
image: DOCKER_REGISTRY_REPO-backend # DOCKER_REGISTRY_REPO will be replaced by CI
command: "--http=0.0.0.0:8090 --dir=/htwkalender/data/pb_data"
pull_policy: always
@ -30,7 +30,7 @@ services:
pull_policy: always
restart: always
depends_on:
- htwkalender-backend
- htwkalender-data-manager
networks:
- "net"
@ -44,7 +44,7 @@ services:
- ./cal.htwk-leipzig.de.pem:/opt/bitnami/nginx/conf/cal.htwk-leipzig.de.pem
- ./cal.htwk-leipzig.de.key.pem:/opt/bitnami/nginx/conf/cal.htwk-leipzig.de.key.pem
depends_on:
- htwkalender-backend
- htwkalender-data-manager
- htwkalender-frontend
ports:
- "443:443"

View File

@ -15,17 +15,17 @@
#along with this program. If not, see <https://www.gnu.org/licenses/>.
services:
htwkalender-backend:
htwkalender-data-manager:
build:
dockerfile: Dockerfile
context: ./services/backend
context: services/data-manager
target: dev # prod
command: "--http=0.0.0.0:8090 --dir=/htwkalender/data/pb_data"
ports:
- "8090:8090"
volumes:
- pb_data:/htwkalender/data # for production with volume
# - ./backend:/htwkalender/data # for development with bind mount from project directory
# - ./data-manager:/htwkalender/data # for development with bind mount from project directory
htwkalender-ical:
build:
@ -51,7 +51,7 @@ services:
volumes:
- ./reverseproxy.local.conf:/opt/bitnami/nginx/conf/nginx.conf
depends_on:
- htwkalender-backend
- htwkalender-data-manager
- htwkalender-frontend
ports:
- "80:80"

View File

@ -17,7 +17,7 @@
import { Module } from "../model/module";
export async function fetchModule(module: Module): Promise<Module> {
// request to the backend on /api/module with query parameters name as the module name
// request to the data-manager on /api/module with query parameters name as the module name
const request = new Request("/api/module?uuid=" + module.uuid);
return await fetch(request)

View File

@ -14,7 +14,7 @@
//You should have received a copy of the GNU Affero General Public License
//along with this program. If not, see <https://www.gnu.org/licenses/>.
// load free rooms as a list of strings form the backend
// load free rooms as a list of strings form the data-manager
export async function requestFreeRooms(
from: string,
to: string,

View File

@ -116,7 +116,7 @@ http {
server_name cal.htwk-leipzig.de;
location /api/feed {
proxy_pass http://htwkalender-backend:8090;
proxy_pass http://htwkalender-ical:8091;
client_max_body_size 2m;
proxy_connect_timeout 600s;
proxy_read_timeout 600s;
@ -136,7 +136,7 @@ http {
server_name htwkalender.de;
location /api/feed {
proxy_pass http://htwkalender-backend:8090;
proxy_pass http://htwkalender-ical:8091;
client_max_body_size 2m;
proxy_connect_timeout 600s;
proxy_read_timeout 600s;
@ -169,7 +169,7 @@ http {
ssl_certificate_key cal.htwk-leipzig.de.key.pem;
location /api {
proxy_pass http://htwkalender-backend:8090;
proxy_pass http://htwkalender-data-manager:8090;
client_max_body_size 20m;
proxy_connect_timeout 600s;
proxy_read_timeout 600s;
@ -179,7 +179,7 @@ http {
# Cache only specific URI
location /api/modules {
proxy_pass http://htwkalender-backend:8090;
proxy_pass http://htwkalender-data-manager:8090;
client_max_body_size 20m;
proxy_connect_timeout 600s;
proxy_read_timeout 600s;
@ -197,7 +197,7 @@ http {
}
location /api/events/types {
proxy_pass http://htwkalender-backend:8090;
proxy_pass http://htwkalender-data-manager:8090;
client_max_body_size 20m;
proxy_connect_timeout 600s;
proxy_read_timeout 600s;
@ -215,7 +215,7 @@ http {
}
location /api/rooms {
proxy_pass http://htwkalender-backend:8090;
proxy_pass http://htwkalender-data-manager:8090;
client_max_body_size 20m;
proxy_connect_timeout 600s;
proxy_read_timeout 600s;
@ -233,7 +233,7 @@ http {
}
location /api/schedule {
proxy_pass http://htwkalender-backend:8090;
proxy_pass http://htwkalender-data-manager:8090;
client_max_body_size 20m;
proxy_connect_timeout 600s;
proxy_read_timeout 600s;
@ -251,7 +251,7 @@ http {
}
location /api/courses {
proxy_pass http://htwkalender-backend:8090;
proxy_pass http://htwkalender-data-manager:8090;
client_max_body_size 20m;
proxy_connect_timeout 600s;
proxy_read_timeout 600s;
@ -269,7 +269,7 @@ http {
}
location /api/feed {
proxy_pass http://htwkalender-backend:8090;
proxy_pass http://htwkalender-ical:8091;
client_max_body_size 2m;
proxy_connect_timeout 600s;
proxy_read_timeout 600s;
@ -278,20 +278,8 @@ http {
limit_req zone=feed burst=10 nodelay;
}
location /api/createFeed {
limit_req zone=createFeed nodelay;
# return limit request error
limit_req_status 429;
proxy_pass http://htwkalender-backend:8090;
client_max_body_size 2m;
proxy_connect_timeout 600s;
proxy_read_timeout 600s;
proxy_send_timeout 600s;
send_timeout 600s;
}
location /_ {
proxy_pass http://htwkalender-backend:8090;
proxy_pass http://htwkalender-data-manager:8090;
# if user is not 0 in admin list, return 404
if ($admin) {
return 404 "Not Found";

View File

@ -125,7 +125,7 @@ http {
ssl_certificate_key dev_htwkalender_de.key.pem;
location /api/feed {
proxy_pass http://htwkalender-backend:8090;
proxy_pass http://htwkalender-ical:8091;
client_max_body_size 2m;
proxy_connect_timeout 600s;
proxy_read_timeout 600s;
@ -135,7 +135,7 @@ http {
}
location /api {
proxy_pass http://htwkalender-backend:8090;
proxy_pass http://htwkalender-data-manager:8090;
client_max_body_size 20m;
proxy_connect_timeout 600s;
proxy_read_timeout 600s;
@ -145,7 +145,7 @@ http {
# Cache only specific URI
location /api/modules {
proxy_pass http://htwkalender-backend:8090;
proxy_pass http://htwkalender-data-manager:8090;
client_max_body_size 20m;
proxy_connect_timeout 600s;
proxy_read_timeout 600s;
@ -163,7 +163,7 @@ http {
}
location /api/rooms {
proxy_pass http://htwkalender-backend:8090;
proxy_pass http://htwkalender-data-manager:8090;
client_max_body_size 20m;
proxy_connect_timeout 600s;
proxy_read_timeout 600s;
@ -181,7 +181,7 @@ http {
}
location /api/schedule {
proxy_pass http://htwkalender-backend:8090;
proxy_pass http://htwkalender-data-manager:8090;
client_max_body_size 20m;
proxy_connect_timeout 600s;
proxy_read_timeout 600s;
@ -199,7 +199,7 @@ http {
}
location /api/courses {
proxy_pass http://htwkalender-backend:8090;
proxy_pass http://htwkalender-data-manager:8090;
client_max_body_size 20m;
proxy_connect_timeout 600s;
proxy_read_timeout 600s;
@ -216,20 +216,8 @@ http {
limit_req zone=modules burst=5 nodelay;
}
location /api/createFeed {
limit_req zone=createFeed nodelay;
# return limit request error
limit_req_status 429;
proxy_pass http://htwkalender-backend:8090;
client_max_body_size 2m;
proxy_connect_timeout 600s;
proxy_read_timeout 600s;
proxy_send_timeout 600s;
send_timeout 600s;
}
location /_ {
proxy_pass http://htwkalender-backend:8090;
proxy_pass http://htwkalender-data-manager:8090;
# if user is not 0 in admin list, return 404
if ($admin) {
return 404 "Not Found";

View File

@ -62,7 +62,7 @@ http {
}
location /api {
proxy_pass http://htwkalender-backend:8090;
proxy_pass http://htwkalender-data-manager:8090;
client_max_body_size 20m;
proxy_connect_timeout 600s;
proxy_read_timeout 600s;
@ -71,7 +71,7 @@ http {
}
location /_ {
proxy_pass http://htwkalender-backend:8090;
proxy_pass http://htwkalender-data-manager:8090;
# Increase upload file size
client_max_body_size 100m;
}

View File

@ -24,6 +24,7 @@ import (
"htwkalender/service/db"
"htwkalender/service/ical"
"io"
"log/slog"
"net/http"
)
@ -36,6 +37,7 @@ func addFeedRoutes(app *pocketbase.PocketBase) {
requestBody, _ := io.ReadAll(c.Request().Body)
result, err := ical.CreateIndividualFeed(requestBody, app)
if err != nil {
slog.Error("Failed to create individual feed", "error", err)
return c.JSON(http.StatusInternalServerError, "Failed to create individual feed")
}
return c.JSON(http.StatusOK, result)

View File

@ -19,11 +19,12 @@ package main
import (
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/log"
"github.com/gofiber/fiber/v3/middleware/logger"
"htwkalender-ical/service"
)
// main function for the ical service
// uses rest api to get the data from the backend
// uses rest api to get the data from the data-manager
// exposes rest api endpoints with fiber to serve the data for clients
func main() {
@ -38,7 +39,9 @@ func main() {
RequestMethods: append(fiber.DefaultMethods[:], webdavRequestMethods...),
})
// Add routes to the app instance for the backend ical service
app.Use(logger.New())
// Add routes to the app instance for the data-manager ical service
service.AddFeedRoutes(app)
log.Fatal(app.Listen(":8091"))

View File

@ -2,6 +2,7 @@ package connector
import (
"encoding/json"
"errors"
"htwkalender-ical/model"
"log/slog"
)
@ -77,3 +78,24 @@ func parseModuleResponse(body []byte) (model.Module, error) {
return module, nil
}
func SaveFeedRecord(modules []model.FeedCollection) (string, error) {
var token string
response, err := PostRequestApi("/api/feed", modules)
if err != nil {
return "", err
}
if response.StatusCode() != 200 {
return "", errors.New("failed to save feed")
}
// parse the response body json to string
err = json.Unmarshal(response.Body(), &token)
if err != nil {
return "", err
}
return token, nil
}

View File

@ -2,12 +2,13 @@ package connector
import (
"github.com/gofiber/fiber/v3/client"
"htwkalender-ical/model"
"time"
)
func RequestApi(path string) (*client.Response, error) {
const host = "http://127.0.0.1:8090"
var host = "http://htwkalender-backend:8090"
func RequestApi(path string) (*client.Response, error) {
cc := client.New()
cc.SetTimeout(5 * time.Second)
@ -23,8 +24,6 @@ func RequestApi(path string) (*client.Response, error) {
func DeleteRequestApi(path string) error {
var host = "http://htwkalender-backend:8090"
cc := client.New()
cc.SetTimeout(5 * time.Second)
@ -36,3 +35,25 @@ func DeleteRequestApi(path string) error {
return nil
}
func PostRequestApi(path string, body []model.FeedCollection) (*client.Response, error) {
cc := client.New()
cc.SetTimeout(5 * time.Second)
config := client.Config{
Body: body,
Header: map[string]string{
"Content-Type": "application/json",
"Accept": "*/*",
},
}
// set retry to 0
response, err := cc.Post(host+path, config)
if err != nil {
return nil, err
}
return response, nil
}

View File

@ -5,6 +5,7 @@ import (
"github.com/jordic/goics"
"htwkalender-ical/model"
"htwkalender-ical/service/connector"
"log/slog"
"time"
)
@ -61,3 +62,15 @@ func DeleteFeedRecord(token string) error {
}
return nil
}
func CreateFeed(modules []model.FeedCollection) (string, error) {
// Save feed
token, err := connector.SaveFeedRecord(modules)
if err != nil {
slog.Error("Failed to save feed", "error", err)
return "", err
}
return token, nil
}

View File

@ -1,13 +1,16 @@
package service
import (
"encoding/json"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/log"
"htwkalender-ical/model"
"htwkalender-ical/service/ical"
"log/slog"
"net/http"
)
// add routes to the app instance for the backend ical service
// add routes to the app instance for the data-manager ical service
// with golang fiber
func AddFeedRoutes(app *fiber.App) {
@ -29,6 +32,27 @@ func AddFeedRoutes(app *fiber.App) {
return c.SendString(results)
})
// Define a route for the POST method on the root path '/api/feed'
app.Post("/api/feed", func(c fiber.Ctx) error {
modules := []model.FeedCollection{}
//obtain the body of the request
err := json.Unmarshal(c.Body(), &modules)
if err != nil {
log.Error("Failed to unmarshal request body", "error", err)
return c.SendStatus(fiber.StatusBadRequest)
}
//create a new feed
token, err := ical.CreateFeed(modules)
if err != nil {
println(err)
log.Error("Failed to create feed", "error", err)
return c.SendStatus(fiber.StatusInternalServerError)
}
return c.JSON(token)
})
// Define a route for the GET method on the root path '/'
app.Get("/api/collections/feeds/records/:token", func(c fiber.Ctx) error {