Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7402fcacf6 |
@ -1,51 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"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"
|
||||
)
|
||||
|
||||
func main() {
|
||||
port := ":8123"
|
||||
router := chi.NewRouter()
|
||||
|
||||
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,
|
||||
type SumberData interface {
|
||||
GetBarang(nama string) (model.Barang, error)
|
||||
GetDaftarBarang() (model.DaftarBarang, error)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func HandleHome(w http.ResponseWriter, r *http.Request) {
|
||||
component.HomePage().Render(r.Context(), w)
|
||||
func HandleDaftarBarang(db SumberData) http.HandlerFunc {
|
||||
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) {
|
||||
component.FormSatu().Render(r.Context(), w)
|
||||
func HandleNamaBarang(db SumberData) http.HandlerFunc {
|
||||
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)
|
||||
}
|
||||
|
||||
45
component/barang.templ
Normal file
45
component/barang.templ
Normal file
@ -0,0 +1,45 @@
|
||||
package component
|
||||
|
||||
import "git.proximination.com/pclatihan/wstempl/model"
|
||||
|
||||
templ BarangView(mdl model.Barang) {
|
||||
<div class="">
|
||||
<h1>{ mdl.Nama }</h1>
|
||||
<div class="">{ mdl.Merk }</div>
|
||||
<table>
|
||||
<tr>
|
||||
<td>PANJANG</td>
|
||||
<td>{ mdl.Panjang }</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>LEBAR</td>
|
||||
<td>{ mdl.Lebar }</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>TINGGI</td>
|
||||
<td>{ mdl.Tinggi }</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>SATUAN</td>
|
||||
<td>{ mdl.Unit }</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>STATUS</td>
|
||||
<td>{ mdl.Status }</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a href="/">BACK</a>
|
||||
</div>
|
||||
}
|
||||
|
||||
templ DaftarBarangView(mdl model.DaftarBarang) {
|
||||
@Common("DAFTAR") {
|
||||
<ul>
|
||||
for _, item := range mdl.Items {
|
||||
<li>
|
||||
<a href={ templ.SafeURL(item.Url) }>{ item.Nama }</a>
|
||||
</li>
|
||||
}
|
||||
</ul>
|
||||
}
|
||||
}
|
||||
24
component/common.templ
Normal file
24
component/common.templ
Normal file
@ -0,0 +1,24 @@
|
||||
package component
|
||||
|
||||
templ Common(pagename string) {
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||
<title>{ pagename }</title>
|
||||
</head>
|
||||
<body>
|
||||
<nav>
|
||||
<a href="/">HOME</a>
|
||||
<a href="/about-us">about us</a>
|
||||
<a href="/profile">profile</a>
|
||||
<a href="/contact">Contact</a>
|
||||
<a href="/contact1">Contact1</a>
|
||||
<a href="/contact2">Contact2</a>
|
||||
<a href="/contact3">Contact3</a>
|
||||
</nav>
|
||||
{ children... }
|
||||
</body>
|
||||
</html>
|
||||
}
|
||||
10
go.mod
10
go.mod
@ -1,16 +1,20 @@
|
||||
module git.proximination.com/pcpelatihan/wstempl
|
||||
module git.proximination.com/pclatihan/wstempl
|
||||
|
||||
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 (
|
||||
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/cenkalti/backoff/v4 v4.3.0 // indirect
|
||||
github.com/cli/browser v1.3.0 // indirect
|
||||
github.com/fatih/color v1.16.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-isatty v0.0.20 // indirect
|
||||
github.com/natefinch/atomic v1.0.1 // indirect
|
||||
|
||||
4
go.sum
4
go.sum
@ -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/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
|
||||
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/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
|
||||
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/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/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/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
|
||||
33
mock/mock.go
Normal file
33
mock/mock.go
Normal 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
|
||||
}
|
||||
49
mock/mockv2.go
Normal file
49
mock/mockv2.go
Normal file
@ -0,0 +1,49 @@
|
||||
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"}),
|
||||
}
|
||||
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
|
||||
}
|
||||
11
model/barang.go
Normal file
11
model/barang.go
Normal file
@ -0,0 +1,11 @@
|
||||
package model
|
||||
|
||||
type Barang struct {
|
||||
Nama string
|
||||
Merk string
|
||||
Panjang string
|
||||
Lebar string
|
||||
Tinggi string
|
||||
Unit string
|
||||
Status string
|
||||
}
|
||||
11
model/listbarang.go
Normal file
11
model/listbarang.go
Normal file
@ -0,0 +1,11 @@
|
||||
package model
|
||||
|
||||
type BarangDalamList struct {
|
||||
Nama string
|
||||
Merk string
|
||||
Url string
|
||||
}
|
||||
|
||||
type DaftarBarang struct {
|
||||
Items []BarangDalamList
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user