51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
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"
|
|
)
|
|
|
|
type SumberData interface {
|
|
GetBarang(nama string) (model.Barang, error)
|
|
GetDaftarBarang() (model.DaftarBarang, error)
|
|
}
|
|
|
|
func main() {
|
|
|
|
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 HandleDaftarBarang(db SumberData) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
daftar, _ := db.GetDaftarBarang()
|
|
|
|
view.DaftarBarangView(daftar).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)
|
|
}
|
|
}
|