Zig Programming Language

last updated: {{ โ€œ1712202776โ€ | date: โ€œ%Y-%m-%d %H:%Mโ€ }} Intro a general-purpose programming language and toolchain for maintaining robust, optimal and reusable software. Install Debian/Ubuntu $ sudo apt install zig Alpine Linux $ doas apk add zig ์ตœ์‹  ๋ฆฌ๋ˆ…์Šค ๋ฒ„์ „ ์„ค์น˜ https://ziglang.org/download/ os์™€ ์•„ํ‚คํ…์ณ์— ๋งž๋Š” ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•œ๋‹ค. ์••์ถ•์„ ํ‘ผ ํ›„ PATH๋ฅผ ์„ค์ •ํ•ด์ฃผ๋ฉด ๋ฐ”๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. ํ˜„์žฌ ์‚ฌ์šฉ ๋ฒ„์ „: 0.12 $ tar xJf zig-linux-x86_64-0.12.0-dev.3212+40e64245f.tar.xz $ ln -s zig-linux-x86_64-0.12.0-dev.3212+40e64245f/ zig $ cd zig; ls $ ./zig version $ ./zig help Hello World hello-world.zig const std = @import("std"); pub fn main() !void { std.debug.print("Hello, World!\n", .{}); } run / test $ zig run hello-world.zig $ zig test hello-world.zig build (default=debug, small, fast, safe) $ zig build-exe hello.zig $ ./hello $ zig build-exe --name hello-small -O ReleaseSmall hello.zig $ zig build-exe --name hello-fast -O ReleaseFast hello.zig $ zig build-exe --name hello-safe -O ReleaseSafe hello.zig $ zig build-exe --name hello-debug -O Debug hello.zig $ ls -l $ ./hello-small More Docs zig-nvim - neovim์— zig ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ์„ค์ •ํ•˜๊ธฐ zig-cc - Zig๋กœ gcc/clang ์ปดํŒŒ์ผ๋Ÿฌ ๋Œ€์ฒดํ•˜๊ธฐ Links home - https://ziglang.org/ awesome zig - https://www.trackawesomelist.com/catdevnull/awesome-zig/readme/ Study and Docs zig-guide - https://zig.guide/ reference - https://ziglang.org/documentation/0.11.0/ zig-by-example - https://zig-by-example.com/ zig std - https://ziglang.org/documentation/master/std/ exercises - https://exercism.org/tracks/zig/exercises learn x in y minutes - https://learnxinyminutes.com/docs/zig/ Compile and Build zig-cc - https://andrewkelley.me/post/zig-cc-powerful-drop-in-replacement-gcc-clang.html Zig Build System Internals - https://mitchellh.com/zig/build-internals

2024-03-12 ยท 214 words

Openresty - Scalable Web Platform NGINX with Lua

INTRO openresty๋Š” nginx์™€ lua ๊ธฐ๋ฐ˜์˜ ์›น๊ฐœ๋ฐœ ํ”Œ๋žซํผ์ด๋‹ค. lua๋Š” luajit ์„ ์‚ฌ์šฉํ•˜๊ณ  ์—”์ง„์ด ํฌํ•จ๋˜์–ด ์žˆ๋‹ค. ์„œ๋ฒ„์—์„œ ๋™์ž‘ํ•œ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๋ฆฌ๋ˆ…์Šค ๋ฐฐํฌํŒ์€ ํŒจํ‚ค์ง€๋ฅผ ์ง€์›ํ•œ๋‹ค. ์—ฌ๊ธฐ์—์„œ๋Š” docker ์ด๋ฏธ์ง€๋กœ ์„ค์น˜ํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ •๋ฆฌํ•œ๋‹ค. System Info: RaspberryPi 3 Model B Rev 1.2 OS: Raspbian (Debian 11, Bullseye) Docker: version 24.0.5 Getting Started Hello World # Install Docker and Relogin after exit curl -sSL https://get.docker.com | sh sudo usermod -aG docker $usermod exit ... docker run hello-world # Docker Image get docker pull openresty/openresty:alpine # Setup mkdir ~/www cd ~/www mkdir conf html vi conf/nginx.conf ... worker_processes 1; error_log error.log; events { worker_connections 1024; } http { server { listen 8000; location / { default_type text/html; content_by_lua ' ngx.say("<p>hello world!</p>") '; } } } ... # Run openresty docker image docker run --name mresty --rm --volume `pwd`/conf/:/usr/local/openresty/nginx/conf/ \ -p 127.0.0.1:8000:8000 openresty/openresty:alpine # docker process check docker ps # openresty web server test curl localhost:8000 <p>hello world!</p> Openresty Comandline Utility # version check resty -v # Hello World resty -e 'print("Hello World")' echo 'print("Hello World")' > hello.lua resty hello.lua # resty ngx module test time resty -e 'gnx.sleep(1) ngx.say("done")' resty -e 'local sock = ngx.socket.tcp() print(socke:connect("openresty.com",443))' resty -e 'ngx.thread.wait(ngx.thread.spawn(function () print("in thread!") end))' mkdir lua/ vim lua/test.lua ... local _M = {} function _M.hello() print("Hello") end return _M ... resty -e 'require "test".hello()' # Error resty -I lua/ -e 'require "test".hello()' # Hello resty -e 'local ok, stdout = require "resty.shell".run([[echo ok]]) print(stdout)' # ok resty --shdict 'dogs 10m' -e 'print(ngx.shared.dogs:set("age",11))' # truenilfalse resty --shdict 'dogs 7m' --shdict 'cats 5m' -e 'print(ngx.shared.dogs, " ", ngx.shared.cats)' resty --http-conf 'lua_regex_match_limit 102400;' -e 'print "ok"' # Bench Test echo 'local a = 0 for 1, 1e8 do a = a+1 end print(a)' > bench.lua time resty -joff bench.lua # without Jit Compiler time resty bench.lua # with Jit Compiler (Fast) resty -jv bench.lua resty -jdump bench.lua | less # Help resty -h | less # See Help restydoc resty-cli # See Document Structure and Lua Module Create Project Folder and Modules mkdir test-module cd test-module mkdir logs conf lua vim lua/hello.lua ... local _M = {} function _M.greet(name) ngx.say("Greetings from ", name) end return _M ... Setup nginx.conf vim conf/nginx.conf ... worker_processes 1; events { worker_connections 1024; } http { # Lua module Preload for performance and Cache init_by_lua_block { require "hello" } # add module path to lua package path: $prefix means nginx -p option value lua_package_path "$prefix/lua/?.lua;;" # Server Setup: port, lua block... server { listen 8080 reuseport; location / { content_by_lua_block { local hello = require "hello" hello.greet("a Lua module") } } } } ... Server start and Test # server configuration test nginx -p $PWD/ -t # server start nginx -p $PWD/ # Chech Error Log tail logs/error.log # client request test : Browser also available curl 'http://127.0.0.1:8080' Greetins from a Lua module Server Admin # check server process ps aux | grep nginx | grep -v /tmp/ pgrep -l nginx # Edit nginx.conf vim conf/nginx.conf # Server Restart (After editting of nginx.conf) kill -HUP `cat logs/nginx.pid` # client request again curl 'http://127.0.0.1:8080' Build Develop Environment Makefile์„ ๋งŒ๋“ค๊ณ  ๋„์ปค์ด๋ฏธ์ง€๋ฅผ ์ œ์–ดํ•˜๊ณ  ๋กœ๊ทธ ๋“ฑ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ํ™˜๊ฒฝ์„ ๋งŒ๋“ ๋‹ค. Makefile conf/nginx.conf Test REFERENCE awesome-resty - https://github.com/bungle/awesome-resty ...

2023-09-16 ยท (updated 2023-09-27) ยท 566 words

DockerHub - ๋„์ปคํ—ˆ๋ธŒ ์†Œ๊ฐœ ๋ฐ ํ™œ์šฉ

Intro ๋„์ปค ์ด๋ฏธ์ง€ ์›๊ฒฉ ์ €์žฅ์†Œ ๋งŒ๋“ค์–ด ๋†“์€ ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œ ๋ฐ ๋‹ค์šด๋กœ๋“œ ํ™œ์šฉ ํšŒ์›๊ฐ€์ž… https://hub.docker.com - ๊ณ„์ • ๋ฐ ์ด๋ฉ”์ผ ๋“ฑ๋ก : ๊ณต๊ฐœ์šฉ(๊ฐœ์ธ)์€ ๋ฌด๋ฃŒ hub.docker.com ์ ‘์† ํ›„ ํšŒ์›๊ฐ€์ž… Email Confirm (์ž์‹ ์˜ ์ด๋ฉ”์ผ์—์„œ ์ธ์ฆ) ๋„์ปคํ—ˆ๋ธŒ ๋กœ๊ทธ์ธ ์ €์žฅ์†Œ ์ƒ์„ฑํ•˜๊ณ  ๋กœ์ปฌ์—์„œ ๋นŒ๋“œํ•œ ์ด๋ฏธ์ง€ ์˜ฌ๋ฆฌ๊ธฐ ๋กœ๊ทธ์ธ ํ›„ Repository ์ƒ์„ฑ ex) ์‚ฌ์šฉ์ž๋ช…/์ €์žฅ์†Œ๋ช…:ํƒœ๊ทธ๋ช… neosolaris/mdev:debian-luajit doocker client(my local pc)์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์˜ฌ๋ฆฐ๋‹ค. $ docker login # hub์— ํšŒ์›๊ฐ€์ž…ํ•œ id, password ๋“ฑ๋ก $ docker images # ๋‚ด ์ด๋ฏธ์ง€ ํ™•์ธ (mdev:debian-luajit์ด๋ผ ๊ฐ€์ •) $ docker tag mdev:debian-luajit neosolaris/mdev:debian-luajit $ docker push neosolaris/mdev:debian-luajit #์‚ฌ์šฉ์ž๋ช…/์ €์žฅ์†Œ๋ช…:ํƒœ๊ทธ๋ช… hub.docker.com ์ ‘์† ํ›„ repository ํ™•์ธ ์œ„ ๊ณผ์ • ์ค‘ ๋„์ปคํ—ˆ๋ธŒ์—์„œ ์ €์žฅ์†Œ๋ฅผ ์ง์ ‘ ๋งŒ๋“ค์ง€ ์•Š์•„๋„ local์—์„œ ๋กœ๊ทธ์ธ๋˜์–ด ์žˆ๋‹ค๋ฉด docker push๋กœ ๊ฐ€๋Šฅํ•˜๋‹ค. ์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ ๋„์ปคํ—ˆ๋ธŒ์—์„œ ์ž์‹ ์ด ์›ํ•˜๋Š” ์ด๋ฏธ์ง€ ๋˜๋Š” ์ž์‹ ์ด ๋งŒ๋“  ์ด๋ฏธ์ง€๋ฅผ ๋‹ค์šด๋ฐ›๋Š”๋‹ค. $ docker pull gcc:bullseye # ๊ณต์‹ debian-bullseye-gcc ํ™˜๊ฒฝ ์ด๋ฏธ์ง€ $ docker neosolaris/mdev:debian-luajit # ๋‚ด ํ”„๋กœ์ ํŠธ ์ด๋ฏธ์ง€ Links https://hub.docker.com

2022-11-13 ยท 129 words

Bash readlines how to

Method 1 for read -r line do echo $line done < input_file.txt > out_file.txt Method 1 file=$(cat input_file.txt) for line in $file do echo $line done < input_file.txt > out_file.txt Links https://www.geeksforgeeks.org/bash-scripting-how-to-read-a-file-line-by-line/

2022-08-26 ยท 32 words