fgdfg
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
module main
|
||||
|
||||
go 1.25.5
|
||||
@@ -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))
|
||||
}
|
||||
@@ -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>
|
||||
Reference in New Issue
Block a user