-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
149 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
import { builtin } from '../ansible/tasks/index.js' | ||
|
||
export function create_directory(path: string) { | ||
return builtin.file( | ||
`Create directory ${path}`, | ||
{ path, state: 'directory', owner: '{{ansible_user}}', group: '{{ansible_user}}', mode: '0755' }, | ||
{ become: true }, | ||
) | ||
type Config = { | ||
owner?: string | ||
group?: string | ||
mode?: string | ||
} | ||
|
||
export function create_directory(path: string, config: Config = {}) { | ||
config.owner ||= '{{ansible_user}}' | ||
config.group ||= '{{ansible_user}}' | ||
config.mode ||= '0755' | ||
return builtin.file(`Create directory ${path}`, { path, state: 'directory', ...config }, { become: true }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
import { fileURLToPath } from 'url' | ||
|
||
type FileData = { | ||
server_caddyfile: { | ||
log_path: string | ||
service_caddyfiles_pattern: string | ||
} | ||
service_caddyfile: { | ||
domain: string | ||
local_urls: string | ||
} | ||
} | ||
|
||
const __dirname = path.dirname(fileURLToPath(import.meta.url)) | ||
const files_dir = path.join(__dirname, 'files') | ||
|
||
export function get_file<Name extends keyof FileData>(name: Name, data: FileData[Name]) { | ||
let content = fs.readFileSync(path.join(files_dir, name), 'utf-8') | ||
for (const [key, value] of Object.entries(data)) { | ||
content = content.replace(new RegExp(`\\{\\{${key}\\}\\}`, 'g'), value) | ||
} | ||
return content | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
log { | ||
format json | ||
output file {{log_path}} { | ||
roll_size 10mb | ||
roll_keep 10 | ||
} | ||
} | ||
} | ||
|
||
import {{service_caddyfiles_pattern}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{{domain}} { | ||
reverse_proxy {{local_urls}} { | ||
lb_policy client_ip_hash | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import path from 'path' | ||
import { get_file } from '../files.js' | ||
import * as blocks from '../blocks/index.js' | ||
import { CaddyConfig, ReverseProxy, ReverseProxyConfig, Server } from '../types.js' | ||
|
||
const default_config: Required<CaddyConfig> = { | ||
get_server_caddyfile: (server) => | ||
get_file('server_caddyfile', { | ||
log_path: `${server.logs_dir}/caddy.log`, | ||
service_caddyfiles_pattern: `${server.services_dir}/*/Caddyfile`, | ||
}), | ||
get_service_caddyfile: (server, config) => get_file('service_caddyfile', config), | ||
} | ||
|
||
export function caddy(config: CaddyConfig = {}): ReverseProxy { | ||
const normalized_config = { ...default_config, ...config } | ||
return { | ||
get_log_path: (server) => `${server.logs_dir}/caddy.log`, | ||
get_server_tasks: (server) => get_server_tasks(normalized_config, server), | ||
get_service_tasks: (server, reverse_proxy_config) => get_service_tasks(normalized_config, server, reverse_proxy_config), | ||
} | ||
} | ||
|
||
function get_server_tasks(config: Required<CaddyConfig>, server: Server) { | ||
return [blocks.install_caddy({ caddyfile_content: config.get_server_caddyfile(server) })] | ||
} | ||
|
||
function get_service_tasks(config: Required<CaddyConfig>, server: Server, reverse_proxy_config: ReverseProxyConfig) { | ||
return [ | ||
blocks.create_caddy_domain({ | ||
domain: reverse_proxy_config.domain, | ||
caddyfile_path: path.join(server.get_service_dir(reverse_proxy_config.service_name), 'Caddyfile'), | ||
caddyfile_content: config.get_service_caddyfile(server, reverse_proxy_config), | ||
}), | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './caddy.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters