Compare commits

...

3 Commits
main ... goo

Author SHA1 Message Date
goo987
49fd50bf5a changes something 2025-06-20 18:22:22 +07:00
goo987
e8e5ee6af3 modify some visuals 2025-05-29 19:26:25 +07:00
pclatihan
7402fcacf6 hijrah 2025-05-23 18:35:45 +07:00
12 changed files with 357 additions and 34 deletions

View File

@ -1,51 +1,50 @@
package main package main
import ( import (
"fmt" "log/slog"
"net/http" "net/http"
"git.proximination.com/pcpelatihan/wstempl/component" view "git.proximination.com/pclatihan/wstempl/component"
"git.proximination.com/pclatihan/wstempl/mock"
"git.proximination.com/pclatihan/wstempl/model"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
) )
func main() { type SumberData interface {
port := ":8123" GetBarang(nama string) (model.Barang, error)
router := chi.NewRouter() GetDaftarBarang() (model.DaftarBarang, error)
router.Get("/", HandleHome)
router.Get("/satu", HandleFormSatu)
router.Get("/dua", HandleFormDua)
router.Get("/tiga", HandleFormTiga)
router.Get("/coba", HandleCoba)
server := http.Server{
Addr: port,
Handler: router,
} }
fmt.Printf("server jalan di http://localhost%s", port) func main() {
if err := server.ListenAndServe(); err != nil { router := chi.NewRouter()
db := mock.NewMockV2()
router.Get("/", HandleDaftarBarang(db))
router.Get("/barang/{nama}", HandleNamaBarang(db))
// fmt.Println("server running at http://localhost:8080")
slog.Info("server running at http://localhost:8080")
if err := http.ListenAndServe(":8080", router); err != nil {
panic(err) panic(err)
} }
} }
func HandleHome(w http.ResponseWriter, r *http.Request) { func HandleDaftarBarang(db SumberData) http.HandlerFunc {
component.HomePage().Render(r.Context(), w) return func(w http.ResponseWriter, r *http.Request) {
daftar, _ := db.GetDaftarBarang()
view.DaftarBarangView(daftar).Render(r.Context(), w)
}
} }
func HandleFormSatu(w http.ResponseWriter, r *http.Request) { func HandleNamaBarang(db SumberData) http.HandlerFunc {
component.FormSatu().Render(r.Context(), w) return func(w http.ResponseWriter, r *http.Request) {
path := chi.URLParam(r, "nama")
slog.Info(path)
model, _ := db.GetBarang(path)
view.BarangView(model).Render(r.Context(), w)
} }
func HandleFormDua(w http.ResponseWriter, r *http.Request) {
component.FormDua().Render(r.Context(), w)
}
func HandleFormTiga(w http.ResponseWriter, r *http.Request) {
component.FormTiga().Render(r.Context(), w)
}
func HandleCoba(w http.ResponseWriter, r *http.Request) {
component.Try().Render(r.Context(), w)
} }

58
cmd2/main.go Normal file
View File

@ -0,0 +1,58 @@
package main
import (
"fmt"
"git.proximination.com/pclatihan/wstempl/model"
"github.com/brianvoe/gofakeit/v7"
)
type Makhlukhidup interface {
Getnama() string
Getumur() int
Getberat() int
Gettinggi() int
Gethobi() string
Getwarna() string
}
func FakeMurid() model.Murid {
gofakeit.Seed(0)
return model.NewMurid(
gofakeit.Name(),
gofakeit.Number(10, 18),
gofakeit.Number(40, 100),
gofakeit.Number(145, 200),
gofakeit.Hobby(),
gofakeit.Color(),
)
}
func FakeGuru() model.Guru {
gofakeit.Seed(0)
return model.NewGuru(
gofakeit.Name(),
gofakeit.Number(10, 18),
gofakeit.Number(40, 100),
gofakeit.Number(145, 200),
gofakeit.Hobby(),
gofakeit.Color(),
)
}
func cetak(m Makhlukhidup) {
fmt.Println("Nama :", m.Getnama())
fmt.Println("Umur :", m.Getumur())
fmt.Println("Berat :", m.Getberat())
fmt.Println("Tinggi :", m.Gettinggi())
fmt.Println("Hobi :", m.Gethobi())
fmt.Println("Warna :", m.Getwarna())
}
func main() {
m := FakeMurid()
g := FakeGuru()
cetak(m)
fmt.Println()
cetak(g)
}

49
component/barang.templ Normal file
View File

@ -0,0 +1,49 @@
package component
import "git.proximination.com/pclatihan/wstempl/model"
templ BarangView(mdl model.Barang) {
<div class="max-w-xl mx-auto bg-white p-6 rounded-xl shadow-md">
<h1 class="text-2xl font-bold mb-4 text-blue-700">{ mdl.Nama }</h1>
<p class="text-gray-600 mb-2">Merk: <span class="font-semibold">{ mdl.Merk }</span></p>
<table class="w-full border border-gray-300 rounded text-sm">
<tr class="bg-gray-50">
<td class="p-2 font-semibold">Panjang</td>
<td class="p-2">{ mdl.Panjang }</td>
</tr>
<tr>
<td class="p-2 font-semibold">Lebar</td>
<td class="p-2">{ mdl.Lebar }</td>
</tr>
<tr class="bg-gray-50">
<td class="p-2 font-semibold">Tinggi</td>
<td class="p-2">{ mdl.Tinggi }</td>
</tr>
<tr>
<td class="p-2 font-semibold">Satuan</td>
<td class="p-2">{ mdl.Unit }</td>
</tr>
<tr class="bg-gray-50">
<td class="p-2 font-semibold">Status</td>
<td class="p-2">{ mdl.Status }</td>
</tr>
<tr>
<td class="p-2 font-semibold">Warna</td>
<td class="p-2">{ mdl.Warna }</td>
</tr>
</table>
<a href="/" class="inline-block mt-4 text-blue-600 hover:underline">← Kembali ke daftar</a>
</div>
}
templ DaftarBarangView(mdl model.DaftarBarang) {
@Common("DAFTAR") {
<ul class="list-disc list-inside text-blue-700 space-y-2">
for _, item := range mdl.Items {
<li>
<a class="hover:underline" href={ templ.SafeURL(item.Url) }>{ item.Nama }</a>
</li>
}
</ul>
}
}

16
component/common.templ Normal file
View File

@ -0,0 +1,16 @@
package component
templ Common(title string) {
<html>
<head>
<title>{ title }</title>
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet" />
</head>
<body class="bg-gray-100 text-gray-800">
<main class="min-h-screen py-10">
{ children... }
</main>
</body>
</html>
}

10
go.mod
View File

@ -1,16 +1,20 @@
module git.proximination.com/pcpelatihan/wstempl module git.proximination.com/pclatihan/wstempl
go 1.24.1 go 1.24.1
require (
github.com/a-h/templ v0.3.865
github.com/brianvoe/gofakeit/v7 v7.2.1
github.com/go-chi/chi/v5 v5.2.1
)
require ( require (
github.com/a-h/parse v0.0.0-20250122154542-74294addb73e // indirect github.com/a-h/parse v0.0.0-20250122154542-74294addb73e // indirect
github.com/a-h/templ v0.3.865 // indirect
github.com/andybalholm/brotli v1.1.0 // indirect github.com/andybalholm/brotli v1.1.0 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cli/browser v1.3.0 // indirect github.com/cli/browser v1.3.0 // indirect
github.com/fatih/color v1.16.0 // indirect github.com/fatih/color v1.16.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-chi/chi/v5 v5.2.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-isatty v0.0.20 // indirect
github.com/natefinch/atomic v1.0.1 // indirect github.com/natefinch/atomic v1.0.1 // indirect

4
go.sum
View File

@ -4,6 +4,8 @@ github.com/a-h/templ v0.3.865 h1:nYn5EWm9EiXaDgWcMQaKiKvrydqgxDUtT1+4zU2C43A=
github.com/a-h/templ v0.3.865/go.mod h1:oLBbZVQ6//Q6zpvSMPTuBK0F3qOtBdFBcGRspcT+VNQ= github.com/a-h/templ v0.3.865/go.mod h1:oLBbZVQ6//Q6zpvSMPTuBK0F3qOtBdFBcGRspcT+VNQ=
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
github.com/brianvoe/gofakeit/v7 v7.2.1 h1:AGojgaaCdgq4Adzrd2uWdbGNDyX6MWNhHdQBraNfOHI=
github.com/brianvoe/gofakeit/v7 v7.2.1/go.mod h1:QXuPeBw164PJCzCUZVmgpgHJ3Llj49jSLVkKPMtxtxA=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8= github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/cli/browser v1.3.0 h1:LejqCrpWr+1pRqmEPDGnTZOjsMe7sehifLynZJuqJpo= github.com/cli/browser v1.3.0 h1:LejqCrpWr+1pRqmEPDGnTZOjsMe7sehifLynZJuqJpo=
@ -14,6 +16,8 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/go-chi/chi/v5 v5.2.1 h1:KOIHODQj58PmL80G2Eak4WdvUzjSJSm0vG72crDCqb8= github.com/go-chi/chi/v5 v5.2.1 h1:KOIHODQj58PmL80G2Eak4WdvUzjSJSm0vG72crDCqb8=
github.com/go-chi/chi/v5 v5.2.1/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops= github.com/go-chi/chi/v5 v5.2.1/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=

33
mock/mock.go Normal file
View File

@ -0,0 +1,33 @@
package mock
import "git.proximination.com/pclatihan/wstempl/model"
type Mock struct{}
func NewMock() *Mock {
return &Mock{}
}
func (m Mock) GetBarang(nama string) (model.Barang, error) {
model := model.Barang{
Nama: nama,
Merk: "Ini dari DB pura-pura mau ngarep apalagi",
Panjang: "12",
Lebar: "10",
Tinggi: "12",
Unit: "Buah",
}
return model, nil
}
func (m Mock) GetDaftarBarang() (model.DaftarBarang, error) {
daftar := model.DaftarBarang{
Items: []model.BarangDalamList{
{Nama: "barang1", Merk: "abalabal", Url: "barang/barang1"},
{Nama: "barang2", Merk: "abalabal", Url: "barang/barang2"},
{Nama: "barang3", Merk: "abalabal", Url: "barang/barang3"},
{Nama: "barang4", Merk: "abalabal", Url: "barang/barang4"},
},
}
return daftar, nil
}

50
mock/mockv2.go Normal file
View File

@ -0,0 +1,50 @@
package mock
import (
"fmt"
"git.proximination.com/pclatihan/wstempl/model"
"github.com/brianvoe/gofakeit/v7"
)
type MockV2 struct{}
func NewMockV2() *MockV2 {
return &MockV2{}
}
func (m MockV2) GetBarang(nama string) (model.Barang, error) {
model := model.Barang{
Nama: nama,
Merk: gofakeit.Name(),
Panjang: gofakeit.Numerify("#"),
Lebar: gofakeit.Numerify("#"),
Tinggi: gofakeit.Numerify("#"),
Unit: gofakeit.AppName(),
Status: gofakeit.RandomString([]string{"Sehat", "Rusak", "Dicuri Orang"}),
Warna : gofakeit.Color(),
}
return model, nil
}
func (m MockV2) GetDaftarBarang() (model.DaftarBarang, error) {
daftar := model.DaftarBarang{
Items: DaftarBoongan(25),
}
return daftar, nil
}
func DaftarBoongan(num int) []model.BarangDalamList {
result := []model.BarangDalamList{}
for i := 0; i < num; i++ {
nama := gofakeit.Word()
data := model.BarangDalamList{
Nama: nama,
Merk: gofakeit.ProductCategory(),
Url: fmt.Sprintf("barang/%s", nama),
}
result = append(result, data)
}
return result
}

12
model/barang.go Normal file
View File

@ -0,0 +1,12 @@
package model
type Barang struct {
Nama string
Merk string
Panjang string
Lebar string
Tinggi string
Unit string
Status string
Warna string
}

43
model/guru.go Normal file
View File

@ -0,0 +1,43 @@
package model
type Guru struct {
Nama string
Umur int
Berat int
Tinggi int
Hobi string
Warnafavorit string
}
func NewGuru(nama string, umur int, tinggi int, berat int, hobi string, warnafavorit string) Guru {
return Guru{
Nama: nama,
Umur: umur,
Berat: berat,
Tinggi: tinggi,
Hobi: hobi,
Warnafavorit: warnafavorit,
}
}
func (g Guru) Getnama() string {
return g.Nama
}
func (g Guru) Getumur() int {
return g.Umur
}
func (g Guru) Getberat() int {
return g.Berat
}
func (g Guru) Gettinggi() int {
return g.Tinggi
}
func (g Guru) Gethobi() string {
return g.Hobi
}
func (g Guru) Getwarna() string {
return g.Warnafavorit
}

11
model/listbarang.go Normal file
View File

@ -0,0 +1,11 @@
package model
type BarangDalamList struct {
Nama string
Merk string
Url string
}
type DaftarBarang struct {
Items []BarangDalamList
}

44
model/murid.go Normal file
View File

@ -0,0 +1,44 @@
package model
type Murid struct {
Nama string
Umur int
Berat int
Tinggi int
Hobi string
Warnafavorit string
}
func NewMurid(nama string, umur int, berat int, tinggi int, hobi string, warnafavorit string) Murid {
return Murid{
Nama: nama,
Umur: umur,
Berat: berat,
Tinggi: tinggi,
Hobi: hobi,
Warnafavorit: warnafavorit,
}
}
func (m Murid) Getnama() string {
return m.Nama
}
func (m Murid) Getumur() int {
return m.Umur
}
func (m Murid) Getberat() int {
return m.Berat
}
func (m Murid) Gettinggi() int {
return m.Tinggi
}
func (m Murid) Gethobi() string {
return m.Hobi
}
func (m Murid) Getwarna() string {
return m.Warnafavorit
}