|
|
|
@ -3,6 +3,7 @@ package web |
|
|
|
|
import ( |
|
|
|
|
"embed" |
|
|
|
|
"f00860/kartograph-map-editor/pkg/generator" |
|
|
|
|
"fmt" |
|
|
|
|
"html/template" |
|
|
|
|
"io" |
|
|
|
|
"log" |
|
|
|
@ -33,7 +34,7 @@ func Start(address string, port int) { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func worldFromRequest(req *http.Request) generator.World { |
|
|
|
|
func worldFromRequest(req *http.Request) (generator.World, error) { |
|
|
|
|
size, err := strconv.Atoi(req.URL.Query().Get("s")) |
|
|
|
|
if err != nil { |
|
|
|
|
size = 11 |
|
|
|
@ -59,16 +60,30 @@ func worldFromRequest(req *http.Request) generator.World { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func mapHandler(w http.ResponseWriter, req *http.Request) { |
|
|
|
|
world := worldFromRequest(req) |
|
|
|
|
log.Printf("GET /map.svg?%v (%v)", req.URL.Query().Encode(), req.RemoteAddr) |
|
|
|
|
|
|
|
|
|
var SVGContent string |
|
|
|
|
|
|
|
|
|
world, err := worldFromRequest(req) |
|
|
|
|
if err != nil { |
|
|
|
|
SVGContent = generator.ErrorSVG(err, 100, 100) |
|
|
|
|
} else { |
|
|
|
|
SVGContent = world.SVG() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "image/svg+xml") |
|
|
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(world.SVG()))) |
|
|
|
|
io.WriteString(w, world.SVG()) |
|
|
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(SVGContent))) |
|
|
|
|
io.WriteString(w, SVGContent) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func printHandler(w http.ResponseWriter, req *http.Request) { |
|
|
|
|
world := worldFromRequest(req) |
|
|
|
|
log.Printf("GET /print?%v (%v)", req.URL.Query().Encode(), req.RemoteAddr) |
|
|
|
|
|
|
|
|
|
world, err := worldFromRequest(req) |
|
|
|
|
if err != nil { |
|
|
|
|
fmt.Print(err) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
tpl, err := template.ParseFS(templateFiles, "templates/print.tpl.html") |
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
@ -80,7 +95,7 @@ func printHandler(w http.ResponseWriter, req *http.Request) { |
|
|
|
|
|
|
|
|
|
func indexHandler(w http.ResponseWriter, req *http.Request) { |
|
|
|
|
seed := generator.RandomSeed() |
|
|
|
|
world := generator.New(11, 7, 5, 6, seed) |
|
|
|
|
world, _ := generator.New(11, 7, 5, 6, seed) |
|
|
|
|
log.Printf("GET / (%v) Seed: %v", req.RemoteAddr, seed) |
|
|
|
|
tpl, err := template.ParseFS(templateFiles, "templates/index.tpl.html") |
|
|
|
|
|
|
|
|
|