Skip to content

Commit

Permalink
✂️ Her app'e trim Makefile hedefi ekle
Browse files Browse the repository at this point in the history
  • Loading branch information
KimlikDAO-bot committed Nov 30, 2023
1 parent 2e56d13 commit 3022be1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 67 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ cf-deployment: build build/sunucu/prod.js build/sitemap sunucu/prod.toml
--config sunucu/prod.toml \
--compatibility-date $(shell date -v -1d +%Y-%m-%d)

.PHONY: cf-deployment clean dev staging
cf-trim: cf-deployment
tools/cftrimmer.py sunucu/prod.toml sunucu/Makefile

.PHONY: cf-deployment cf-trim clean dev staging
66 changes: 33 additions & 33 deletions tools/cftrimmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,54 +10,51 @@
import requests
import toml

DIL = {
'en', 'tr'
}
DIL = {"en", "tr"}

NAMED_ASSET = {
'sitemap.txt',
'TCKT.svg'
# 'favicon.ico',
}
EXT = ["", ".br", ".gz"]

EXT = ['', '.br', '.gz']

CF_CONFIG = toml.load('sunucu/prod.toml')
CF_CONFIG = toml.load(sys.argv[-2])
ROUTE = f"https://{CF_CONFIG['route']['pattern']}/"
ACCOUNT_ID = CF_CONFIG['account_id']
ZONE_ID = CF_CONFIG['zone_id']
NAMESPACE_ID = CF_CONFIG['kv_namespaces'][0]['id']
CREDENTIALS = toml.load('.gizli')
CF_UPLOADER_TOKEN = CREDENTIALS['CF_UPLOADER_TOKEN']
CF_TRIMMER_TOKEN = CREDENTIALS['CF_TRIMMER_TOKEN']
ACCOUNT_ID = CF_CONFIG["account_id"]
ZONE_ID = CF_CONFIG["zone_id"]
NAMESPACE_ID = CF_CONFIG["kv_namespaces"][0]["id"]
CREDENTIALS = toml.load(".gizli")
CF_UPLOADER_TOKEN = CREDENTIALS["CF_UPLOADER_TOKEN"]
CF_TRIMMER_TOKEN = CREDENTIALS["CF_TRIMMER_TOKEN"]


URL = "https://api.cloudflare.com/client/v4"
ACCOUNTS_URL = f"{URL}/accounts/{ACCOUNT_ID}/storage/kv/namespaces/{NAMESPACE_ID}/"
ZONES_URL = f"{URL}/zones/{ZONE_ID}/"


def read_pages() -> str:
for line in open('sunucu/Makefile'):
if line.startswith('PAGES :='):
return line[8:].split()
assert False, "Sayfalar bulunamadı"
def read_variable(prefix) -> list[str]:
for line in open(sys.argv[-1]):
if line.startswith(prefix):
return line[len(prefix):].split()
print(f"{prefix} bulunamadı")
return []


SAYFALAR = read_variable("PAGES :=")

SAYFALAR = read_pages()
subprocess.run(["make", "cf-deployment"])
NAMED_ASSETS = read_variable("NAMED_ASSETS :=")


def get_existing(namespace_id: str) -> set[str]:
res = requests.get(
ACCOUNTS_URL + 'keys', headers={'authorization': 'Bearer ' + CF_UPLOADER_TOKEN})
return {x['name'] for x in res.json()['result']}
ACCOUNTS_URL + "keys", headers={"authorization": "Bearer " + CF_UPLOADER_TOKEN}
)
return {x["name"] for x in res.json()["result"]}


existing = get_existing(NAMESPACE_ID)
keep = ([f'{sayfa}-{dil}.html{ext}' for sayfa in SAYFALAR for ext in EXT for dil in DIL] +
[named + ext for named in NAMED_ASSET for ext in EXT] +
list(next(os.walk('build'))[2]))
keep = (
[f"{sayfa}-{dil}.html{ext}" for sayfa in SAYFALAR for ext in EXT for dil in DIL]
+ [named + ext for named in NAMED_ASSETS for ext in EXT]
+ list(next(os.walk("build"))[2])
)
keep = set(keep)
remove = [name for name in existing if name not in keep]

Expand All @@ -68,9 +65,12 @@ def get_existing(namespace_id: str) -> set[str]:
print(json.dumps(remove))

res = requests.delete(
ACCOUNTS_URL + 'bulk', data=json.dumps(remove), headers={
'content-type': 'application/json',
'authorization': 'Bearer ' + CF_TRIMMER_TOKEN,
})
ACCOUNTS_URL + "bulk",
data=json.dumps(remove),
headers={
"content-type": "application/json",
"authorization": "Bearer " + CF_TRIMMER_TOKEN,
},
)

print(res.text)
69 changes: 36 additions & 33 deletions tools/cfuploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@
import requests
import toml

DIL = {
'en', 'tr'
}
DIL = {"en", "tr"}

EXT = ['', '.br', '.gz']
EXT = ["", ".br", ".gz"]

CF_CONFIG = toml.load(sys.argv[-2])
ROUTE = f"https://{CF_CONFIG['route']['pattern']}/"
ACCOUNT_ID = CF_CONFIG['account_id']
ZONE_ID = CF_CONFIG['zone_id']
NAMESPACE_ID = CF_CONFIG['kv_namespaces'][0]['id']
CF_UPLOADER_TOKEN = toml.load('.gizli')['CF_UPLOADER_TOKEN']
ACCOUNT_ID = CF_CONFIG["account_id"]
ZONE_ID = CF_CONFIG["zone_id"]
NAMESPACE_ID = CF_CONFIG["kv_namespaces"][0]["id"]
CF_UPLOADER_TOKEN = toml.load(".gizli")["CF_UPLOADER_TOKEN"]

URL = "https://api.cloudflare.com/client/v4"
ACCOUNTS_URL = f"{URL}/accounts/{ACCOUNT_ID}/storage/kv/namespaces/{NAMESPACE_ID}/"
Expand All @@ -41,44 +39,48 @@ def read_variable(prefix) -> list[str]:


def chunks(xs, n):
return (xs[i:i+n] for i in range(0, len(xs), n))
return (xs[i: i + n] for i in range(0, len(xs), n))


def get_existing(namespace_id: str) -> set[str]:
res = requests.get(
ACCOUNTS_URL + 'keys', headers={'authorization': 'Bearer ' + CF_UPLOADER_TOKEN})
return {x['name'] for x in res.json()['result']}
ACCOUNTS_URL + "keys", headers={"authorization": "Bearer " + CF_UPLOADER_TOKEN}
)
return {x["name"] for x in res.json()["result"]}


def batch_upload(names: list[str]):
to_upload = []
for name in names:
b = {
'key': name,
'base64': True
}
b['value'] = base64.b64encode(
open('build/' + name, 'rb').read()).decode()
b = {"key": name, "base64": True}
b["value"] = base64.b64encode(
open("build/" + name, "rb").read()).decode()
to_upload.append(b)

to_upload = json.dumps(to_upload, separators=(',', ':'))
return requests.put(ACCOUNTS_URL + 'bulk', data=to_upload, headers={
'content-type': 'application/json',
'authorization': 'Bearer ' + CF_UPLOADER_TOKEN
})
to_upload = json.dumps(to_upload, separators=(",", ":"))
return requests.put(
ACCOUNTS_URL + "bulk",
data=to_upload,
headers={
"content-type": "application/json",
"authorization": "Bearer " + CF_UPLOADER_TOKEN,
},
)


def purge_cache(assets):
batches = chunks(assets, 20)
for i, batch in enumerate(batches):
to_purge = {
'files': [ROUTE + asset for asset in batch]
}
to_purge = json.dumps(to_purge, separators=(',', ':'))
res = requests.post(ZONES_URL + 'purge_cache', data=to_purge, headers={
'content-type': 'application/json',
'authorization': 'Bearer ' + CF_UPLOADER_TOKEN
})
to_purge = {"files": [ROUTE + asset for asset in batch]}
to_purge = json.dumps(to_purge, separators=(",", ":"))
res = requests.post(
ZONES_URL + "purge_cache",
data=to_purge,
headers={
"content-type": "application/json",
"authorization": "Bearer " + CF_UPLOADER_TOKEN,
},
)
print(f"Batch {i} result: {res.status_code}")


Expand All @@ -90,9 +92,10 @@ def is_static_upload(name: str) -> bool:


existing = get_existing(NAMESPACE_ID)
named_upload = ([f'{sayfa}-{dil}.html{ext}' for sayfa in SAYFALAR for ext in EXT for dil in DIL] +
[named + ext for named in NAMED_ASSETS for ext in EXT])
static_upload = list(filter(is_static_upload, next(os.walk('build'))[2]))
named_upload = [
f"{sayfa}-{dil}.html{ext}" for sayfa in SAYFALAR for ext in EXT for dil in DIL
] + [named + ext for named in NAMED_ASSETS for ext in EXT]
static_upload = list(filter(is_static_upload, next(os.walk("build"))[2]))

# (1) Statik asset'leri yükle
if static_upload:
Expand Down

0 comments on commit 3022be1

Please sign in to comment.