Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

added blog

Blue Mouse084c6c3883a7
diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 000000000..73e1f8bf6
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "blog-posts"]
+ path = blog-posts
+ url = https://github.com/Swetrix/blog-posts.git
diff --git a/apps/production/src/app.module.ts b/apps/production/src/app.module.ts
index 14dae1b43..7d089de94 100644
--- a/apps/production/src/app.module.ts
+++ b/apps/production/src/app.module.ts
@@ -12,6 +12,7 @@ import { MailerModule } from './mailer/mailer.module'
import { ActionTokensModule } from './action-tokens/action-tokens.module'
import { TwoFactorAuthModule } from './twoFactorAuth/twoFactorAuth.module'
import { TaskManagerModule } from './task-manager/task-manager.module'
+import { BlogModule } from './blog/blog.module'
import { WebhookModule } from './webhook/webhook.module'
import { PingModule } from './ping/ping.module'
import { MarketplaceModule } from './marketplace/marketplace.module'
@@ -43,6 +44,7 @@ const modules = [
ScheduleModule.forRoot(),
NestjsFormDataModule.config({ isGlobal: true }),
TaskManagerModule,
+ BlogModule,
UserModule,
MailerModule,
ActionTokensModule,
diff --git a/apps/production/src/blog/blog.controller.ts b/apps/production/src/blog/blog.controller.ts
new file mode 100644
index 000000000..e34eb17c4
--- /dev/null
+++ b/apps/production/src/blog/blog.controller.ts
@@ -0,0 +1,48 @@
+import { Controller, Param, Get, NotFoundException } from '@nestjs/common'
+import { ApiTags, ApiResponse } from '@nestjs/swagger'
+import { BlogService } from './blog.service'
+
+@ApiTags('Blog')
+@Controller('v1/blog')
+export class BlogController {
+ constructor(private readonly blogService: BlogService) {}
+
+ @Get('/')
+ @ApiResponse({ status: 200 })
+ async getAll(): Promise<any> {
+ return this.blogService.getAll()
+ }
+
+ @Get('/sitemap')
+ @ApiResponse({ status: 200 })
+ async getSitemap(): Promise<any> {
+ return this.blogService.getSitemapFileNames(undefined, true)
+ }
+
+ @Get('/:slug')
+ @ApiResponse({ status: 200 })
+ async getSlug(@Param('slug') slug: string): Promise<any> {
+ const post = this.blogService.getArticleBySlug(slug)
+
+ if (!post) {
+ throw new NotFoundException()
+ }
+
+ return post
+ }
+
+ @Get('/:category/:slug')
+ @ApiResponse({ status: 200 })
+ async getCategoryAlug(
+ @Param('category') category: string,
+ @Param('slug') slug: string,
+ ): Promise<any> {
+ const post = this.blogService.getArticleBySlug(slug, category)
+
+ if (!post) {
+ throw new NotFoundException()
+ }
+
+ return post
+ }
+}
diff --git a/apps/production/src/blog/blog.module.ts b/apps/production/src/blog/blog.module.ts
new file mode 100644
index 000000000..8a6ce1d03
--- /dev/null
+++ b/apps/production/src/blog/blog.module.ts
@@ -0,0 +1,13 @@
+import { Module } from '@nestjs/common'
+
+import { AppLoggerModule } from '../logger/logger.module'
+import { BlogController } from './blog.controller'
+import { BlogService } from './blog.service'
+
+@Module({
+ imports: [AppLoggerModule],
+ providers: [BlogService],
+ exports: [BlogService],
+ controllers: [BlogController],
+})
+export class BlogModule {}
diff --git a/apps/production/src/blog/blog.service.ts b/apps/production/src/blog/blog.service.ts
new file mode 100644
index 000000000..4f8fdde3f
--- /dev/null
+++ b/apps/production/src/blog/blog.service.ts
@@ -0,0 +1,161 @@
+import { Injectable } from '@nestjs/common'
+import * as _find from 'lodash/find'
+import * as _includes from 'lodash/includes'
+import * as _endsWith from 'lodash/endsWith'
+import * as _filter from 'lodash/filter'
+import * as _map from 'lodash/map'
+import * as _replace from 'lodash/replace'
+import * as _isEmpty from 'lodash/isEmpty'
+import * as _size from 'lodash/size'
+import * as path from 'path'
+import * as fs from 'fs/promises'
+import { AppLoggerService } from '../logger/logger.service'
+import { BLOG_POSTS_PATH } from '../common/constants'
+
+// eslint-disable-next-line
+const parseFrontMatter = require('front-matter')
+
+// Removes first 10 characters from the string (i.e. 2023-10-07-)
+const getSlugFromFilename = (filename: string) => filename.substring(11)
+
+const findFilenameBySlug = (list: string[], handle: string) => {
+ return _find(list, item => _includes(item, handle))
+}
+
+interface IParseFontMatter {
+ attributes: any
+ body: string
+}
+
+const getFileNames = async (category?: string): Promise<string[]> => {
+ let files
+
+ try {
+ if (category) {
+ files = (await fs.readdir(
+ path.join(BLOG_POSTS_PATH, category),
+ )) as string[]
+ } else {
+ files = (await fs.readdir(BLOG_POSTS_PATH)) as string[]
+ }
+ } catch (reason) {
+ console.error('[getFileNames]', reason)
+ return []
+ }
+
+ return _filter(files, file => _endsWith(file, '.md'))
+}
+
+const getPost = async (
+ slug: string,
+ category?: string,
+): Promise<IParseFontMatter> => {
+ const files = await getFileNames(category)
+ const filename = findFilenameBySlug(files, slug)
+
+ if (!filename) {
+ return null
+ }
+
+ const filepath = category
+ ? path.join(BLOG_POSTS_PATH, category, filename)
+ : path.join(BLOG_POSTS_PATH, filename)
+
+ let file: Buffer
+
+ try {
+ file = await fs.readFile(filepath)
+ } catch (reason) {
+ console.error('[getPost]', reason)
+ return null
+ }
+
+ const { attributes, body }: IParseFontMatter = parseFrontMatter(
+ file.toString(),
+ )
+ return { attributes, body }
+}
+
+const getArticlesMetaData = async () => {
+ let dir: string[]
+
+ try {
+ dir = await fs.readdir(BLOG_POSTS_PATH)
+ } catch (reason) {
+ console.error('[getArticlesMetaData]', reason)
+ return null
+ }
+
+ const filtered = _filter(dir, (file: string) => _endsWith(file, '.md'))
+
+ return Promise.all(
+ _map(filtered, async filename => {
+ const file = await fs.readFile(path.join(BLOG_POSTS_PATH, filename))
+ const { attributes }: IParseFontMatter = parseFrontMatter(file.toString())
+ return {
+ slug: _replace(getSlugFromFilename(filename), /\.md$/, ''),
+ title: attributes.title,
+ hidden: attributes.hidden,
+ intro: attributes.intro,
+ date: attributes.date,
+ author: attributes.author,
+ nickname: attributes.nickname,
+ }
+ }),
+ )
+}
+
+@Injectable()
+export class BlogService {
+ constructor(private readonly logger: AppLoggerService) {}
+
+ async getArticleBySlug(slug: string, category?: string) {
+ if (!slug) {
+ return null
+ }
+
+ const post = await getPost(slug, category)
+
+ if (!post) {
+ return null
+ }
+
+ return post
+ }
+
+ async getSitemapFileNames(
+ category?: string,
+ infiniteRecursive?: boolean,
+ ): Promise<any> {
+ const allFiles = (await fs.readdir(
+ category ? path.join(BLOG_POSTS_PATH, category) : BLOG_POSTS_PATH,
+ )) as string[]
+ const directories = _filter(allFiles, file => !_endsWith(file, '.md'))
+
+ if (_isEmpty(directories)) {
+ return allFiles
+ }
+
+ const files = _filter(allFiles, file => _endsWith(file, '.md'))
+
+ let filesInDirectories: any[] = []
+
+ for (let i = 0; i < _size(directories); ++i) {
+ const directory = directories[i]
+
+ // set to true for infinite depth directory look-up
+ // eslint-disable-next-line
+ const _files = await this.getSitemapFileNames(directory, infiniteRecursive)
+ filesInDirectories = [
+ ...filesInDirectories,
+ ..._map(_files, file => [directory, _replace(file, /\.md$/, '')]),
+ ]
+ }
+
+ return [...files, ...filesInDirectories]
+ }
+
+ async getAll() {
+ return getArticlesMetaData()
+ }
+}
diff --git a/apps/production/src/common/constants.ts b/apps/production/src/common/constants.ts
index a26e4cc03..b9768ff95 100644
--- a/apps/production/src/common/constants.ts
+++ b/apps/production/src/common/constants.ts
@@ -1,5 +1,6 @@
import { ClickHouse } from 'clickhouse'
import Redis from 'ioredis'
+import * as path from 'path'
import { hash } from 'blake3'
import * as _toNumber from 'lodash/toNumber'
@@ -110,6 +111,10 @@ const CAPTCHA_COOKIE_KEY = 'swetrix-captcha-token'
const CAPTCHA_TOKEN_LIFETIME = 300 // seconds (5 minutes).
const CAPTCHA_SECRET_KEY_LENGTH = 50
+const BLOG_POSTS_PATH = isDevelopment
+ ? path.join(__dirname, '../../../..', 'blog-posts', 'posts')
+ : path.join(__dirname, '../..', 'blog-posts', 'posts')
+
const TRAFFIC_COLUMNS = [
'cc',
'rg',
@@ -210,4 +215,5 @@ export {
isProxiedByCloudflare,
PAYPAL_CLIENT_ID,
PAYPAL_CLIENT_SECRET,
+ BLOG_POSTS_PATH,
}
diff --git a/apps/production/src/project/project.controller.ts b/apps/production/src/project/project.controller.ts
index 2c934d13b..091a74ded 100644
--- a/apps/production/src/project/project.controller.ts
+++ b/apps/production/src/project/project.controller.ts
@@ -46,7 +46,7 @@ import {
deleteProjectRedis,
generateProjectId,
} from './project.service'
-import { UserType, ACCOUNT_PLANS, PlanCode } from '../user/entities/user.entity'
+import { UserType, PlanCode } from '../user/entities/user.entity'
import { ActionTokenType } from '../action-tokens/action-token.entity'
import { ActionTokensService } from '../action-tokens/action-tokens.service'
import { MailerService } from '../mailer/mailer.service'
diff --git a/blog-posts b/blog-posts
new file mode 160000
index 000000000..24e9a99bc
--- /dev/null
+++ b/blog-posts
@@ -0,0 +1 @@
+Subproject commit 24e9a99bca44d3193c81998abfbd62e7410126fa
diff --git a/package.json b/package.json
index 1ec307f2b..e084f6750 100644
--- a/package.json
+++ b/package.json
@@ -55,6 +55,7 @@
"eslint-plugin-lodash": "^7.4.0",
"express": "^4.18.2",
"form-data": "^4.0.0",
+ "front-matter": "^4.0.2",
"googleapis": "^126.0.1",
"handlebars": "^4.7.8",
"ioredis": "^5.3.2",