This commit is contained in:
a.pivkin
2025-12-30 21:55:12 +03:00
parent 00b9ce122d
commit 23931220bb
8 changed files with 268 additions and 7 deletions
+3
View File
@@ -0,0 +1,3 @@
module main
go 1.25.5
+96
View File
@@ -0,0 +1,96 @@
package main
import (
"fmt"
"log"
"net/http"
"strconv"
"html/template"
)
type rub float64
func (r rub) String() string {
return fmt.Sprintf("%.2f",r)
}
var tmpl *template.Template
func Render(w http.ResponseWriter, req *http.Request) {
err := tmpl.Execute(w,req)
if err != nil {
http.Error(w,err.Error(),http.StatusInternalServerError)
}
}
type database map[string]rub
type List struct {
Items database
}
func (db *database) list(w http.ResponseWriter, req *http.Request) {
data := List{
Items: *db,
}
err := tmpl.ExecuteTemplate(w,"template.html",data)
if err != nil {
http.Error(w,err.Error(),http.StatusInternalServerError)
}
}
func (db *database) price(w http.ResponseWriter,req *http.Request) {
item := req.URL.Query().Get("item")
v,ok := (*db)[item]
if !ok {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w,"%s not found in goods list",item)
return
}
fmt.Fprintf(w,"Price of %s is %s",item,v)
}
func (db *database) update(w http.ResponseWriter, req *http.Request) {
item := req.URL.Query().Get("item")
price := req.URL.Query().Get("price")
f,err := strconv.ParseFloat(price,2)
if err != nil {
log.Fatal(err)
}
(*db)[item] = rub(f)
fmt.Fprintf(w,"item %s and price %s",item,(*db)[item])
}
// func (db database) ServeHTTP(w http.ResponseWriter, r *http.Request ) {
// switch r.URL.Path {
// case "/list":
// for k,v := range db {
// fmt.Fprintf(w,"%s costs %s rub\n",k,v)
// }
// case "/price":
// item := r.URL.Query().Get("item")
// v,ok := db[item]
// if !ok {
// w.WriteHeader(http.StatusNotFound)
// fmt.Fprintf(w,"%s not found in goods list",item)
// return
// }
// fmt.Fprintf(w,"Price of %s is %s",item,v)
// }
// }
func main() {
db := database{
"sneakers": 10.0,
"boots": 20,
}
// mux.Handle("/list",http.HandlerFunc(db.list))
// mux.Handle("/price",http.HandlerFunc(db.price))
tmpl = template.Must(template.ParseFiles("template.html"))
http.HandleFunc("/list",db.list)
http.HandleFunc("/price",db.price)
http.HandleFunc("/update",db.update)
log.Fatal(http.ListenAndServe(":9090",nil))
}
+34
View File
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<title>TABLE</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
.header { background: #3498db; color: white; padding: 20px; border-radius: 8px; }
.content { margin: 20px 0; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px; border: 1px solid #ddd; text-align: left; }
th { background: #f4f4f4; }
.price { text-align: right; font-weight: bold; color: #27ae60; }
.total { background: #ecf0f1; font-weight: bold; }
</style>
</head>
<body>
<div class="content">
<h2>Items</h2>
<table>
<tr>
<th>Item Name</th>
<th>Price</th>
</tr>
{{range $key, $value := .Items}}
<tr>
<td>{{$key}}</td>
<td class="price">{{$value.String}}</td>
</tr>
{{end}}
</table>
</div>
</body>
</html>