วันพฤหัสบดีที่ 30 มิถุนายน พ.ศ. 2565

จง​อดทน​จนกว่า​องค์​พระ​ผู้​เป็น​เจ้า​จะ​เสด็จ​มา

 ยก. 5:7 เพราะ​ฉะนั้น พี่​น้อง​เอ๋ย จง​อด​ทน​จน​กว่า​องค์​พระ​ผู้​เป็น​เจ้า​จะ​เสด็จ​มา ดูซิ ชาว​นา​ยัง​รอ​คอย​พืช​ผล​อัน​ล้ำ​ค่า​ที่​จะ​ได้​จาก​แผ่น​ดิน อด​ทน​รอ​คอย​จน​กว่า​ฝน​ต้น​ฤดู​และ​ฝน​ชุก​ปลาย​ฤดู​จะ​มา

ยก. 5:8 ท่าน​ทั้ง​หลาย​ก็​ต้อง​อด​ทน​อย่าง​นั้น จง​ทำ​ใจ​ให้​ดี​ไว้ เพราะ​การ​เสด็จ​มา​ของ​องค์​พระ​ผู้​เป็น​เจ้า​ใกล้​เข้า​มา​แล้ว

ยก. 5:9 พี่​น้อง​ทั้ง​หลาย อย่า​บ่น​ว่า​กัน​และ​กัน เพื่อ​จะ​ได้​ไม่​ต้อง​ถูก​พิพาก​ษา นี่แน่ะ องค์​พระ​ผู้​พิพาก​ษา​ทรง​ยืน​อยู่​ที่​หน้าประตู​แล้ว

ยก. 5:10 พี่​น้อง​ทั้ง​หลาย จง​เอา​อย่าง​การ​ทน​ทุกข์​และ​การ​อด​ทน​ของ​บรร​ดา​ผู้​เผย​พระ​วจนะ​ซึ่ง​กล่าว​ใน​พระ​นาม​ของ​องค์​พระ​ผู้​เป็น​เจ้า

ยก. 5:11 นี่​แน่ะ เรา​ถือ​ว่า​คน​เหล่า​นั้น​ที่​สู้​ทน​ก็​เป็น​สุข ท่าน​ได้​ยิน​เรื่อง​ความ​ทรหด​อด​ทน​ของ​โยบ และ​ได้​เห็น​สิ่ง​ที่​องค์​พระ​ผู้​เป็น​เจ้า​ทรง​ทำ​ให้​เขา​ใน​บั้น​ปลาย คือ​ว่า​องค์​พระ​ผู้​เป็น​เจ้า​ทรง​มี​ความ​สง​สาร​และ​ความ​เมต​ตา​กรุณา​เพียง​ไร

ยก. 5:12 พี่​น้อง​ของ​ข้าพ​เจ้า ที่​สำ​คัญ​กว่า​สิ่ง​อื่น​ใด​ก็​คือ อย่า​สา​บาน ไม่​ว่า​จะ​ทำ​โดย​อ้าง​สวรรค์​หรือ​โลก​หรือ​อ้าง​คำ​สา​บาน​อื่น​ใด​ก็​ตาม ถ้า​ใช่​ก็​จง​บอก​ว่า​ใช่ ถ้า​ไม่​ใช่​ก็​จง​บอก​ว่า​ไม่​ใช่ เพื่อ​ท่าน​ทั้ง​หลาย​จะ​ไม่​ถูก​พิพาก​ษา

ยก. 5:13 มี​ใคร​ใน​พวก​ท่าน​ทน​ทุกข์​หรือ? จง​ให้​คน​นั้น​อธิษ​ฐาน มี​ใคร​ร่า​เริง​ยินดี​หรือ? จง​ให้​คน​นั้น​ร้อง​เพลง​สรร​เสริญ

ยก. 5:14 มี​ใคร​ใน​พวก​ท่าน​เจ็บ​ป่วย​หรือ? จง​ให้​คน​นั้น​เชิญ​บรร​ดา​ผู้​ปก​ครอง​ของ​คริสต​จักร​มา และ​ให้​ท่าน​เหล่า​นั้น​อธิษ​ฐาน​เผื่อ​เขา​และ​ชโลม​เขา​ด้วย​น้ำ​มัน​ใน​พระ​นาม​ของ​องค์​พระ​ผู้​เป็น​เจ้า

ยก. 5:15 การ​อธิษ​ฐาน​ด้วย​ความ​เชื่อ​จะ​รักษา​ผู้​ป่วย​ให้​หาย​โรค และ​องค์​พระ​ผู้​เป็น​เจ้า​จะ​ทรง​ให้​เขา​ลุก​ขึ้น​ได้ และ​ถ้า​เขา​เคยทำ​บาป พระ​องค์​ก็​จะ​ทรง​ให้​อภัย

ยก. 5:16 เพราะ​ฉะ​นั้น​ท่าน​จง​สาร​ภาพ​บาป​ต่อ​กัน​และ​กัน และ​จง​อธิษ​ฐาน​เผื่อ​กัน​และ​กัน เพื่อ​ท่าน​ทั้ง​หลาย​จะ​ได้​รับ​การ​รัก​ษา​โรค คำ​วิง​วอน​ของ​ผู้​ชอบ​ธรรม​นั้น​มี​พลัง​มาก​และ​เกิด​ผล

ยก. 5:17 เอลี​ยาห์​ก็​เป็น​มนุษย์​ที่​มี​สภาพ​เหมือน​อย่าง​เรา ท่าน​อธิษ​ฐาน​อย่าง​จริง​จัง​ขอ​ไม่​ให้​ฝน​ตก และ​ฝน​ก็​ไม่​ตก​ต้อง​แผ่น​ดิน​ถึง​สาม​ปี​กับ​หก​เดือน

ยก. 5:18 และ​เมื่อ​ท่าน​อธิษ​ฐาน​ขอ​อีก​ครั้ง​หนึ่ง สวรรค์​ก็​ให้​ฝน และ​แผ่น​ดิน​ก็​ผลิต​พืช​ผล​ของ​มัน

ยก. 5:19 พี่​น้อง​ของ​ข้าพ​เจ้า ถ้า​ใคร​ใน​พวก​ท่าน​ถูก​หลอก​ให้​หลง​ผิด​ไป​จาก​ความ​จริง​และ​มี​คน​นำ​เขา​กลับ​มา

ยก. 5:20 ให้​คน​นั้น​รู้​เถิด​ว่า​ผู้​ที่​นำ​คน​บาป​กลับ​จาก​ทาง​ผิด​ของ​เขา จะ​ช่วย​วิญ​ญาณ​จิต​ของ​คน​บาป​นั้น​ให้​รอด​จาก​ความ​ตาย และ​จะ​ทำ​ให้​บาป​มาก​มาย​ได้​รับ​การ​อภัย




สดด. 100:1 แผ่น​ดิน​โลก​ทั้ง​สิ้น​เอ๋ย จง​โห่​ร้อง​ด้วย​ความ​ชื่น​บาน​ถวาย​แด่​พระ​ยาห์​เวห์
สดด. 100:2 ​​จง​ปรน​นิบัติ​พระ​ยาห์​เวห์​ด้วย​ความ​ยินดี จง​เข้า​มา​อยู่​เฉพาะ​พระ​พักตร์​พระ​องค์​ด้วย​การ​ร้อง​เพลง
สดด. 100:3 จง​รู้​เถิด​ว่า พระ​ยาห์​เวห์​ทรง​เป็น​พระ​เจ้า คือ​พระ​องค์​เอง​ที่​ทรง​สร้าง​เรา​ทั้ง​หลาย และ​เรา​ก็​เป็น​ของ​พระ​องค์ เรา​เป็น​ประ​ชา​กร​ของ​พระ​องค์ เป็น​แกะ​แห่ง​ทุ่ง​หญ้า​ของ​พระ​องค์
สดด. 100:4 จง​เข้า​ประตู​ของ​พระ​องค์​ด้วย​การ​ขอบ​พระ​คุณ และ​เข้า​บริ​เวณ​พระ​นิ​เวศ​ของ​พระ​องค์​ด้วย​การ​สรร​เสริญ จง​ขอบ​พระ​คุณ​พระ​องค์ จง​ถวาย​สาธุ​การ​แด่​พระ​นาม​ของ​พระ​องค์
สดด. 100:5 เพราะ​พระ​ยาห์​เวห์​ประ​เสริฐ ความ​รัก​มั่น​คง​ของ​พระ​องค์​ดำ​รง​เป็น​นิตย์ และ​ความ​ซื่อ​สัตย์​ของ​พระ​องค์​ดำ​รง​อยู่​ทุก​ชั่ว​ชาติ​พันธุ์


โยบ 1:1 มี​ชาย​คน​หนึ่ง​ใน​แผ่น​ดิน​อูส ชื่อ​โยบ ชาย​คน​นั้น​เป็น​คน​ดี​พร้อม​และ​เที่ยง​ธรรม เป็น​ผู้​ยำ​เกรง​พระ​เจ้า​และ​หัน​จาก​ความ​ชั่ว​ร้าย

โยบ 1:2 ท่าน​มี​บุตร​ชาย 7 คน​และ​บุตร​หญิง 3 คน
โยบ 1:3 ส่วน​ทรัพย์​สมบัติ​ของ​ท่าน มี​แกะ 7,000 ตัว อูฐ 3,000 ตัว วัว 500 คู่ ลา​ตัว​เมีย 500 ตัว และ​คน​ใช้​มาก​มาย ดัง​นั้น​ชาย​ผู้​นี้​จึง​มั่ง​คั่ง​ที่​สุด​ใน​บรร​ดา​ชาว​ตะวัน​ออก
โยบ 1:4 บุตร​ของ​ท่าน​เคย​จัด​งาน​เลี้ยง​ใน​บ้าน​ของ​แต่​ละ​คน​ตาม​วัน​กำ​หนด​ของ​ตน พวก​เขา​จะ​เชิญ​น้อง​สาว​ทั้ง​สาม​คน​มา​รับ​ประ​ทาน​และ​ดื่ม​กับ​พวก​เขา​ด้วย
โยบ 1:5 และ​เมื่อ​งาน​เลี้ยง​เวียน​ครบ​รอบ​แล้ว โยบ​จะ​ทำ​พิธี​ชำระ​ตัว​เขา​ทั้ง​หลาย​ให้​บริ​สุทธิ์ และ​ท่าน​จะ​ตื่น​แต่​เช้า​มืด ถวาย​เครื่อง​บูชา​เผา​ทั้ง​ตัว​ตาม​จำ​นวน​ของ​เขา​ทั้ง​หมด เพราะ​โยบ​กล่าว​ว่า “บาง​ที​ลูกๆ ของ​ข้า​ได้​ทำ​บาป​และ​แช่ง​พระ​เจ้า​ใน​ใจ” โยบ​ทำ​อย่าง​นี้​เรื่อย​มา
โยบ 1:6 อยู่​มา​วัน​หนึ่ง เมื่อ​เหล่า​ทูต​สวรรค์​มา​ราย​งาน​ตัว​ต่อ​พระ​ยาห์​เวห์ ซา​ตาน​ได้​มา​ใน​หมู่​เขา​ด้วย
โยบ 1:7 พระ​ยาห์​เวห์​ตรัส​ถาม​ซา​ตาน​ว่า “เจ้า​มา​จาก​ไหน?” ซา​ตาน​ทูล​ตอบ​พระ​ยาห์​เวห์​ว่า “จาก​ไปๆ มาๆ บน​แผ่น​ดิน​โลก และ​จาก​เดิน​ไป​เรื่อยๆ บน​นั้น

โยบ 1:8 และ​พระ​ยาห์​เวห์​ตรัส​กับ​ซา​ตาน​ว่า “เจ้า​ได้​พิ​จาร​ณา​ดู​โยบ​ผู้​รับ​ใช้​ของ​เรา​หรือ​ไม่​ว่า ใน​แผ่น​ดิน​โลก​ไม่​มี​ใคร​เหมือน​เขา เป็น​คน​ดี​พร้อม​และ​เที่ยง​ธรรม เป็น​ผู้​ยำ​เกรง​พระ​เจ้า​และ​หัน​จาก​ความ​ชั่ว​ร้าย?”

โยบ 1:9 แล้ว​ซา​ตาน​ทูล​ตอบ​พระ​ยาห์​เวห์​ว่า “โยบ​ยำ​เกรง​พระ​เจ้า​เปล่าๆ หรือ?

โยบ 1:10 พระ​องค์​ไม่​ได้​ทรง​กั้น​รั้ว​รอบ​ตัว​เขา ครอบ​ครัว​ของ​เขา และ​ทุก​สิ่ง​ที่​เขา​มี​อยู่​เสีย​ทุก​ด้าน​หรือ? พระ​องค์​ได้​ทรง​อวย​พร​งาน​ที่​มือ​เขา​ทำ และ​ฝูง​ปศุ​สัตว์​ของ​เขา​ได้​ทวี​ขึ้น​ใน​แผ่น​ดิน

โยบ 1:11 แต่​ขอ​ยื่น​พระ​หัตถ์​แตะ​ต้อง​สิ่ง​ของ​ทั้ง​สิ้น​ที่​เขา​มี​อยู่ แล้ว​เขา​จะ​แช่ง​พระ​องค์​ต่อ​พระ​พักตร์​พระ​องค์
โยบ 1:12 และ​พระ​ยาห์​เวห์​ตรัส​กับ​ซา​ตาน​ว่า “ดู​เถิด ทุก​สิ่ง​ที่​เขา​มี​ก็​อยู่​ใน​มือ​ของ​เจ้า เพียง​แต่​อย่า​ยื่น​มือ​แตะ​ต้อง​ตัว​เขา” ซา​ตาน​จึง​ออก​ไป​จาก​เบื้อง​พระ​พักตร์​พระ​ยาห์​เวห์
โยบ 1:13 อยู่​มา​วัน​หนึ่ง เมื่อ​บุตร​ชาย​หญิง​ของ​ท่าน​กำ​ลัง​รับ​ประ​ทาน​และ​ดื่ม​เหล้า​องุ่น​อยู่​ใน​บ้าน​พี่​ชาย​คน​โต​ของ​เขา
โยบ 1:14 มี​ผู้​สื่อ​สาร​มา​หา​โยบ​เรียน​ว่า “ฝูง​วัว​กำ​ลัง​ไถ​นา​อยู่ และ​ฝูง​ลา​ตัว​เมีย​กำ​ลัง​กิน​หญ้า​อยู่​ข้างๆ
โยบ 1:15 คน​เส​บา​ก็​มา​โจม​ตี​เอา​มัน​ไป และ​ฆ่า​คน​ใช้​ด้วย​คม​ดาบ และ​ข้าพ​เจ้า​คน​เดียว​ได้​หนี​รอด​มา​เรียน​ท่าน”
โยบ 1:16 ขณะ​ที่​เขา​กำ​ลัง​พูด​อยู่ ก็​มี​อีก​คน​หนึ่ง​มา​เรียน​ว่า “ไฟ​ของ​พระ​เจ้า​ตก​จาก​ฟ้า​ไหม้​แกะ​กับ​คน​ใช้ และ​เผา​ผลาญ​หมด และ​ข้าพ​เจ้า​คน​เดียว​ได้​หนี​รอด​มา​เรียน​ท่าน”
โยบ 1:17 ขณะ​ที่​เขา​กำ​ลัง​พูด​อยู่ ก็​มี​อีก​คน​หนึ่ง​มา​เรียน​ว่า “ชาว​เคล​เดีย​จัด​เป็น​สาม​กอง​เข้า​ปล้น​เอา​อูฐ​ไป และ​ฆ่า​คน​ใช้​ด้วย​คม​ดาบ และ​ข้าพ​เจ้า​คน​เดียว​ได้​หนี​รอด​มา​เรียน​ท่าน”
โยบ 1:18 ขณะ​ที่​เขา​กำ​ลัง​พูด​อยู่ ก็​มี​อีก​คน​หนึ่ง​มา​เรียน​ว่า “ลูก​ชาย​กับ​ลูก​สาว​ของ​ท่าน​กำ​ลัง​รับ​ประ​ทาน​และ​ดื่ม​เหล้า​องุ่น​อยู่​ใน​บ้าน​พี่ชาย​คน​โต​ของ​เขา
โยบ 1:19 และ​ดู​เถิด มี​พายุ​ข้าม​ถิ่น​ทุร​กัน​ดาร​มา​ปะทะ​บ้าน​ทั้ง​สี่​มุม​จน​พัง​ลง​ทับ​คน​หนุ่ม​สาว และ​พวก​เขา​ก็​ตาย ข้าพ​เจ้า​คน​เดียว​ได้​หนี​รอด​มา​เรียน​ท่าน”
โยบ 1:20 แล้ว​โยบ​ก็​ลุก​ขึ้น ฉีก​เสื้อ​คลุม​ของ​ตน โกน​ศีรษะ กราบ​ลง​ถึง​ดิน​นมัส​การ
โยบ 1:21 ท่าน​ว่า “ข้า​มา​จาก​ครรภ์​มาร​ดา​ตัว​เปล่า และ​ข้า​จะ​กลับ​ไป​ตัว​เปล่า พระ​ยาห์​เวห์​ประ​ทาน และ​พระ​ยาห์​เวห์​ทรง​เอา​ไป​เสีย สาธุ​การ​แด่​พระ​นาม​พระ​ยาห์​เวห์
โยบ 1:22 ใน​เหตุ​การณ์​นี้​ทั้ง​สิ้น โยบ​ไม่​ได้​ทำ​บาป​หรือ​กล่าว​โทษ​พระ​เจ้า


โยบ 2:1 และ​อยู่​มา​วัน​หนึ่ง เมื่อ​เหล่า​ทูต​สวรรค์​มา​ราย​งาน​ตัว​ต่อ​พระ​ยาห์​เวห์ ซา​ตาน​ได้​มา​ใน​หมู่​เขา เพื่อ​ราย​งาน​ตัว​ต่อ​พระ​ยาห์​เวห์​ด้วย
โยบ 2:2 และ​พระ​ยาห์​เวห์​ตรัส​ถาม​ซา​ตาน​ว่า “เจ้า​มา​จาก​ไหน?” ซา​ตาน​ทูล​ตอบ​พระ​ยาห์​เวห์​ว่า “จาก​ไปๆ มาๆ บน​แผ่น​ดิน​โลก และ​จาก​เดิน​ไป​เรื่อยๆ บน​นั้น

โยบ 2:3 และ​พระ​ยาห์​เวห์​ตรัส​กับ​ซา​ตาน​ว่า “เจ้า​ได้​พิ​จาร​ณา​ดู​โยบ​ผู้​รับ​ใช้​ของ​เรา​หรือ​ไม่​ว่า ใน​แผ่น​ดิน​โลก​ไม่​มี​ใคร​เหมือน​เขา เป็น​คน​ดี​พร้อม​และ​เที่ยง​ธรรม เป็น​ผู้​ยำ​เกรง​พระ​เจ้า​และ​หัน​จาก​ความ​ชั่ว​ร้าย? เขา​ยัง​ยึด​มั่น​ใน​ความ​ซื่อ​สัตย์​ของ​เขา​อยู่ ถึง​แม้​เจ้า​ชวน​เรา​ให้​ต่อ​สู้​กับ​เขา เพื่อ​ทำ​ลาย​เขา​โดย​ไม่​มี​เหตุ

โยบ 2:4 แล้ว​ซา​ตาน​ทูล​ตอบ​พระ​ยาห์​เวห์​ว่า “หนัง​แทน​หนัง คน​ย่อม​ให้​ทุก​อย่าง​ที่​เขา​มี​อยู่​แทน​ชีวิต​ของ​เขา
โยบ 2:5 แต่​บัด​นี้​ขอ​เหยียด​พระ​หัตถ์​แตะ​ต้อง​กระ​ดูก​และ​เนื้อ​ของ​เขา แล้ว​เขา​จะ​แช่ง​พระ​องค์​ต่อ​พระ​พักตร์​พระ​องค์”

โยบ 2:6 และ​พระ​ยาห์​เวห์​ตรัส​กับ​ซา​ตาน​ว่า “ดู​เถิด เขา​อยู่​ใน​มือ​เจ้า จง​ไว้​ชีวิต​เขา​เท่า​นั้น
โยบ 2:7 ซา​ตาน​จึง​ออก​ไป​จาก​เบื้อง​พระ​พักตร์​พระ​ยาห์​เวห์ และ​ทำ​ให้​โยบ​เป็น​ฝี​ร้าย ตั้ง​แต่​ฝ่า​เท้า​จน​ถึง​กระ​หม่อม
โยบ 2:8 และ​ท่าน​ก็​เอา​ชิ้น​หม้อ​แตก​มา​ขูด​ตัว และ​นั่ง​อยู่​ใน​กอง​ขี้​เถ้า
โยบ 2:9 แล้ว​ภรร​ยา​ท่าน​กล่าว​กับ​ท่าน​ว่า “เธอ​ยัง​จะ​ยึด​มั่น​ใน​ความ​ซื่อ​สัตย์​อยู่​อีก​หรือ? จง​แช่ง​พระ​เจ้า​และ​ตาย​เสีย​เถอะ
โยบ 2:10 แต่​ท่าน​ตอบ​นาง​ว่า “เธอ​พูด​อย่าง​หญิง​โง่​เขลา​จะ​พึง​พูด เรา​จะ​รับ​สิ่ง​ดี​จาก​พระ​เจ้า และ​จะ​ไม่​รับ​สิ่ง​ไม่​ดี​บ้าง​หรือ?” ใน​เหตุ​การณ์​นี้​ทั้ง​สิ้น โยบ​ไม่​ได้​ทำ​บาป​ด้วย​ริม​ฝี​ปาก​ของ​ตน


โยบ 42:1 แล้ว​โยบ​ทูล​พระ​ยาห์​เวห์​ว่า
โยบ 42:2 “ข้า​พระ​องค์​ทราบ​ว่า พระ​องค์​ทรง​ทำ​ทุก​สิ่ง​ได้ และ​พระ​ประ​สงค์​ของ​พระ​องค์​จะ​สำ​เร็จ

โยบ 42:10 และ​พระ​ยาห์​เวห์​ทรง​ให้​โยบ​กลับ​สู่​สภาพ​ดี เมื่อ​ท่าน​อธิษ​ฐาน​เผื่อ​สหาย​ของ​ท่าน และ​พระ​ยาห์​เวห์​ประ​ทาน​ให้​โยบ​มี​มาก​เป็น​สอง​เท่า​ของ​ที่​มี​อยู่​ก่อน

โยบ 42:11 และ​พี่​น้อง​ชาย​หญิง​ของ​ท่าน​และ​บรร​ดา​ผู้​ที่​รู้​จัก​ท่าน​มา​ก่อน​ได้​มา​หา​ท่าน และ​รับ​ประ​ทาน​อา​หาร​กับ​ท่าน​ใน​บ้าน​ของ​ท่าน และ​เขา​ทั้ง​หลาย​แสดง​ความ​เห็น​ใจ​และ​ปลอบ​โยน​ท่าน ด้วย​เรื่อง​เหตุ​ร้าย​ทั้ง​สิ้น​ซึ่ง​พระ​ยาห์​เวห์​ทรง​นำ​มา​เหนือ​ท่าน และ​ต่าง​ก็​ให้​เงิน​แผ่น​หนึ่ง​กับ​แหวน​ทอง​คำ​วง​หนึ่ง​แก่​ท่าน

โยบ 42:12 และ​พระ​ยาห์​เวห์​ทรง​อวย​พร​ชีวิต​ตอน​ปลาย​ของ​โยบ​มากยิ่ง​กว่า​ตอน​ต้น​ของ​ท่าน และ​ท่าน​มี​แกะ 14,000 ตัว อูฐ 6,000 ตัว วัว​ผู้ 1,000 คู่ และ​ลา​ตัว​เมีย 1,000 ตัว
โยบ 42:13 ท่าน​มี​บุตร​ชาย 7 คน​และ​บุตร​หญิง 3 คน​ด้วย

โยบ 42:14 และ​ท่าน​เรียก​ชื่อ​บุตร​หญิงคน​แรก​ว่า เย​มี​มาห์ และ​ชื่อ​คน​ที่​สอง​ว่า เคสิ​ยาห์ และ​ชื่อ​คน​ที่​สาม​ว่า เค​เรน​หัป​ปุค
โยบ 42:15 ทั่ว​ทั้ง​แผ่น​ดิน ไม่​มี​หญิง​ใด​งด​งาม​เท่า​บุตร​สาว​ของ​โยบ และ​บิดา​ได้​ให้​มร​ดก​แก่​พวก​เธอ​พร้อม​กับ​พวก​พี่​ชาย​และ​น้อง​ชาย

โยบ 42:16 ต่อ​จาก​นี้​ไป โยบ​มี​ชีวิต​อยู่​อีก 140 ปี และ​ได้​เห็น​บุตร​ชาย​ของ​ท่าน​กับ​หลาน​เหลน​ของ​ท่าน​สี่​ชั่ว​อายุ
โยบ 42:17 และ​โยบ​ก็​สิ้น​ชีวิต​เป็น​คน​แก่​หง่อม​ที​เดียว

วันจันทร์ที่ 27 มิถุนายน พ.ศ. 2565

User Creation And Authentication in Golang Part 2— CRUD Operations

 CRUD OPERATIONS THROUGH A HTTP INTERFACE

In the previous article, we demonstrated establishing a successful connection to a local Postgres database using Go. In this article, we’ll continue working with the database and introduce HTML templates as an interface between HTTP requests made in a browser and CRUD (Create, Read, Update and delete) operations made against the database.

Prerequisites:

  • An installation of Go. For installation instructions, see Installing Go.
  • A tool to edit your code. Any text editor you have will work fine, I use Goland.
  • A command terminal. Go works well using any terminal on Linux and Mac, and on PowerShell or cmd in Windows.
  • A local installation of PostgreSQL
  • Installation of the PSQL CLI

1. Reading from the database:

Let’s begin with what I believe is the simplest operation to achieve, reading from the database. We’ll run a SQL query manually using PSQL and then retrieve the information over a browser in an HTML template.

First, create a ‘users’ table with the rows: user_id , username, password, city and email. We’ll then seed some arbitrary data for testing. Open a PSQL shell and execute the following queries against the myapp db:

eomolo$ psql
psql (14.2)
Type "help" for help.
eomolo=# \c myappYou are now connected to database "myapp" as user "eomolo".myapp=# CREATE TABLE users (
user_id serial PRIMARY KEY,
username VARCHAR ( 50 ) UNIQUE NOT NULL,
password VARCHAR ( 50 ) NOT NULL,
city VARCHAR ( 50 ) NOT NULL,
email VARCHAR ( 255 ) UNIQUE NOT NULL);
CREATE TABLEmyapp=# \dtList of relations
Schema | Name | Type | Owner
--------+-------+-------+--------
public | users | table | eomolo
(1 row)
myapp=# INSERT INTO users(username,password, city,email) VALUES ('user1',123456, 'Lanham', 'user1@user.com');INSERT 0 1
myapp=#

Next, in your myapp project directory, create a directory ‘form’ where the html templates will exist. Inside it create an Index.tmpl file:

Construct and add the header and footer as well:

The form directory structure should currently be as follows:

ls myapp/form/
Footer.tmpl Header.tmpl Index.tmpl

In the main.go file, we’re going to import 2 additional packages:

  1. text/template — Package template implements data-driven templates for generating textual output.
  2. net/http — Package http provides HTTP client and server implementations.

We’ll need to create a struct defining a user model:

// define a user model
type User struct {
Id int
Username string
City string
Email string
Password string
}

Refactor the main.go file as below adding an “Index” function that will Query the User table and return the content of it’s rows:

All users can now be listed by making a request to the address http://localhost:8080/.

2. Creating a user

Creating a user would require the capability to input information, then make a post request that can be passed to a function that will then insert the data into the database. Once it does so, it should then redirect to the index page that now displays the newly added information.

To achieve this, we’ll create a “New” and “Show” templates along with corresponding Insert and Show functions. The “New” template will include a form action to send the data to the “insert” path that will be used to pass input to the Insert function.

First, get the input required to create a new user using the New.tmpl form:

The form action will post the inserted data to a route “insert” which will create a new row in the database through it’s corresponding Insert function:

When the database statement is executed successfully, the browser will redirect to the index page now displaying the added user.

An individual user can be displayed using the View button in the table by constructing the following:

A show template that displays a single user:

And a show function that returns information from the database filtered by the Id value passed when the View button is clicked:

3. Updating a user

Now that we’ve achieved the capabilities of creating and showing users, we can now enable an edit function that updates an entry in the database then redirects to the index page showing the new data.

Let’s create an edit form that redirects to an “update” action.

Then, an edit function that will query the database for the user to be updated:

The user will then be passed to the Update action function which will execute a statement using the passed values:

4. Deleting a user

The final action to configure would be to delete a user. When a user ID is passed through the /delete route, a corresponding statement should be made removing the entry from the database. The following function should achieve this:

Your main.go file should now be similar to the following:

This should simplify accomplish the CRUD operations required to manipulate the database. In the next article, we’ll be adding an authentication layer to restrict or enable access and functions based on a user identity.

Fiber (2022) CRUD with Fiber, Postgres, and GORM

 

Build a simple API in Go with Fiber, Postgres, and GORM

According to the StackOverflow Developer Survey and the TIOBE index, Go (or Golang) has gained more traction in recent years, especially among backend developers and DevOps teams working on infrastructure automation. That’s reason enough to write a simple tutorial to introduce you to Go.

It’s worth learning it, so in this article, I wanna show you a brief guide on how to develop a scalable but simple CRUD API in Go using Fiber and GORM. For sake of simplicity, we won’t use Docker.

What is Fiber?

Fiber is an Express-inspired web framework built on top of Fasthttp, the fastest HTTP engine for Go. Designed to ease things up for fast development with zero memory allocation and performance in mind. So if you come from a NodeJS background, Fibe might be a good fit.

What are we going to build?

The project will be very classic. We are going to build a simple Book CRUD-based API. So we going to have different endpoints for create, read, update, and delete.

Prerequisites

You need to have a basic understanding of Go. I will choose Visual Studio Code as my code editor. You can use whatever you prefer. But keep in mind, that you’ll see the command code . once in this article. This is a VSCode (Visual Studio Code) specific command which opens the current directory in VSCode.

You also need to install these:

  • Install Go on your local machine
  • Install Postgres on your local machine

Any recent version of both is okay. However, my Go version is 1.18 and PostgreSQL is version 14.

Create Database

First, we need to create a Postgres database. I know, everybody handles that differently, some people use a GUI, but we going to use our terminal. Again, you need to have Postgres installed on your machine. If you have Postgres installed, the following four commands will run on Linux, Mac, and Windows machines.

  • psql postgres open psql CLI with user postgres
  • CREATE DATABASE go_fiber_api; creating the database we need
  • \l list all databases
  • \q CLI exit

My terminal would look like this after we executed all four commands successfully. As we can see, the database go_fiber_api was created.

Project Setup

First of all, we going to initiate our project and install the modules we need.

Watch out: Replace YOUR_USERNAME with your Github username.

Now let’s install Fiber, GORM, and Viper. We use Viper to manage our Environment Variables.

Let’s continue with the final project structure.

Additionally, let’s add some files.

So, after creating our project, the file structure should look like this:

Now, it’s time to code.

Environment Variables

First, we need to add some environment variables where we store the application port we going to listen on and we URL to the database. Keep in mind, to replace DB_USERDB_PASSWORDDB_HOST, and DB_PORT with your database data.

Let’s add code to pkg/common/envs/.env

For instance, how it would look on my local machine:

Configuration

Let’s add code to pkg/common/config/config.go

Book Models

Here, we going to create the Book model/entity. gorm.Model will add properties such as IDCreatedAtUpdatedAt and DeletedAt for us.

In addition, we add 3 string properties. The json tag at the end gives GORM the information of each column's names in our Postgres database.

Let’s add code to pkg/common/models/book.go

Database Initialization

The book model is done. Now, we configure GORM and auto migrate the model we just created. This AutoMigrate function will create the books table for us as soon as we run this application.

Let’s add code to pkg/common/db/db.go

Main File

This is our bootstrap file. We going to do a lot here.

  • Initializing Viper to handle our environment variables
  • Initializing the database based on GORM
  • Adding a simple “/” route
  • Starting the application

We’re going to change this file later once again.

Let’s add code to cmd/main.go

Now, let’s test the current version of our project. Usually, the application will run in debug mode, so we will get some warnings, just ignore them.

Output inside the console. The very last line is important.

Let’s visit http://localhost:3000

Open Application in Browser

Great. We can see our application’s port. This means everything works as expected.

Book Handlers

Great, everything works. We’re going to replace this output, so don’t worry. Now, let’s add some handlers for our Book API.

Controller

The book handlers/routes will be based on so-called pointer receivers, for that, we define its struct. This struct will receive the database information later, so whenever we call a book handler/route, we will have access to GORM. We’re going to change this file once later again.

Let’s add code to pkg/books/controller.go

Add Book Handler

So this file is very interesting. After the imports, we define a struct for the request’s body. In line 16 you can see the pointer receiver we defined in the previous step. In line 31 you can see, that we using this pointer receiver whose variable name is simply h.

Everything else is pretty straightforward. We get the request’s body, declare a new book variable, merge the request’s body with this book variable, and create a new database entry. Then, we create a response with the book information.

Let’s add code to pkg/books/add_book.go

Get Books Handler

On this route, we going to return all books from our database. This works now pretty fast, but as soon as you have bigger data to handle, better go for a pagination approach.

Let’s add code to pkg/books/get_books.go

Get Book Handler

Here, we just respond with only 1 book based on the ID which we get from a parameter.

Let’s add code to pkg/books/get_book.go

Update Book Handler

If we add a book, we also should have the option to update created books. This route is similar to the AddBook route we have coded earlier.

Let’s add code to pkg/books/update_book.go

Delete Book Handler

This is our last route in this article. Here, we delete a book based on its ID, but only, if the desired entry exists inside the database. We only respond with an HTTP status code.

Let’s add code to pkg/books/delete_book.go

Controller (again)

The routes are done. Now we need to modify the controller file once again. This time, we create a function called RegisterRoutes, it’s pretty self-explaining what it does, right?

Do you remember the receiver pointer? Here we get the receiver pointer for our routes/handlers.

Let’s change the filepkg/books/controller.go from:

to:

Main File (again)

Also, we have to modify our main file once again. Before, we just initialized the database. But this time, we get its return and register our routes/handlers.

Let’s change the file cmd/main.go from:

to:

Makefile

This is optional. Here we can set some scripts to simplify commands. For instance, we define a server script to run the application. So instead of running the application by go run cmd/main we run it instead by make server. This example is not good, since the actual command is pretty short, but imagine you would have to deal with longer commands.

Let’s add code to Makefile inside the root directory.

Run Application

Everything is done! No coding anymore. Now, let’s run the application.

or

The output should look like this. Besides the warnings, we can see, that our routes got settled up and the application runs on port 3000.

Testing the Endpoints

Now, we can test the two routes we have just created. We can test it, by using software such as PostmanInsomnia, or we simply run CURL commands.

POST: Add a new Book

GET: Get All Books

Don’t forget, you can run GET commands also in your browser.

GET: Get Book by ID

PUT: Update Book by ID

DELETE: Delete Book by ID

We are done! Great. Don’t forget, that I have uploaded this project on Github.


User Creation And Authentication in Golang Part 2— CRUD Operations