From ed3466d56fec909c3e9a8d774cc6c93b73b4fdc8 Mon Sep 17 00:00:00 2001 From: Gerald Bauer Date: Sat, 18 Mar 2023 15:32:13 +0100 Subject: [PATCH] up --- artbase.go | 210 ++------------------------------ serve/image_png.go | 120 ++++++++++++++++++ serve/image_svg.go | 49 ++++++++ serve/serve.go | 82 +++++++++++++ {artbase => serve}/templates.go | 12 +- 5 files changed, 266 insertions(+), 207 deletions(-) create mode 100644 serve/image_png.go create mode 100644 serve/image_svg.go create mode 100644 serve/serve.go rename {artbase => serve}/templates.go (93%) diff --git a/artbase.go b/artbase.go index f8efcb9..027d7af 100644 --- a/artbase.go +++ b/artbase.go @@ -2,224 +2,28 @@ package main import ( "fmt" - "log" + // "log" "os" "net/http" - "github.com/learnpixelart/pixelart.go/pixelart" - "github.com/pixelartexchange/artbase.server/artbase" + // "github.com/pixelartexchange/artbase.server/artbase" "github.com/pixelartexchange/artbase.server/artbase/collections" - "github.com/pixelartexchange/artbase.server/router" // simple http router & helpers from scratch (no 3rd party deps) - replace with your own http libs/frameworks + "github.com/pixelartexchange/artbase.server/serve" ) -func handleHome( collections []artbase.Collection ) http.HandlerFunc { - return func( w http.ResponseWriter, req *http.Request ) { - b := artbase.RenderHome( collections ) - - w.Header().Set( "Content-Type", "text/html; charset=utf-8" ) - w.Write( b ) - } -} - -func handleCollection( col artbase.Collection ) http.HandlerFunc { - return func( w http.ResponseWriter, req *http.Request ) { - b := artbase.RenderCollection( &col ) - - w.Header().Set( "Content-Type", "text/html; charset=utf-8" ) - w.Write( b ) - } -} - -func handleCollectionStripPNG( col artbase.Collection ) http.HandlerFunc { - return func( w http.ResponseWriter, req *http.Request ) { - b := col.HandleStripPNG() - - w.Header().Set( "Content-Type", "image/png" ) - w.Write( b ) - } -} - - - -func handleCollectionImagePNG( col artbase.Collection ) http.HandlerFunc { - return func( w http.ResponseWriter, req *http.Request ) { - - id, _ := router.ParamInt( req, "id" ) - - opts := artbase.PNGOpts{} - - - backgroundQuery, ok := router.Query( req, "background" ) - if !ok { - backgroundQuery, ok = router.Query( req, "bg" ) // allow shortcut z too - } - - if ok { - log.Printf( "=> parsing background color (in hex) >%s<...\n", backgroundQuery ) - - background, err := pixelart.ParseColor( backgroundQuery ) - if err != nil { - log.Panic( err ) - } - - opts.Background = background - opts.BackgroundName = backgroundQuery - } - - - silhouetteQuery, ok := router.Query( req, "silhouette" ) - if ok { - log.Printf( "=> parsing silhouette (forground) color (in hex) >%s<...\n", silhouetteQuery ) - - silhouette, err := pixelart.ParseColor( silhouetteQuery ) - if err != nil { - log.Panic( err ) - } - - opts.Silhouette = silhouette - opts.SilhouetteName = silhouetteQuery - } - - flag, ok := router.Query( req, "flag" ) - if ok { - opts.Flag = flag - } - - - mirror, ok := router.QueryBool( req, "mirror" ) - if !ok { - mirror, ok = router.QueryBool( req, "m" ) // allow shortcut m too - } - - if mirror { - opts.Mirror = true - } - - transparent, ok := router.QueryBool( req, "transparent" ) - if transparent { - opts.Transparent = true - } - - - circle, ok := router.QueryBool( req, "circle" ) - if circle { - opts.Circle = true - } - - - zoom, ok := router.QueryInt( req, "zoom" ) - if !ok { - zoom, ok = router.QueryInt( req, "z" ) // allow shortcut z too - } - - if zoom > 1 { - opts.Zoom = zoom - } - - size, ok := router.QueryInt( req, "size" ) - if !ok { - size, ok = router.QueryInt( req, "s" ) // allow shortcut s too - } - - if size > 0 { - opts.Resize = size - } - - - - save, ok := router.QueryBool( req, "autosave" ) - if !ok { - save, ok = router.QueryBool( req, "save" ) // allow shortcut save too - } - - if save { - opts.Save = true - } - - - b := col.HandleTilePNG( id, opts ) - - w.Header().Set( "Content-Type", "image/png" ) - w.Write( b ) - } -} - - -func handleCollectionImageSVG( col artbase.Collection ) http.HandlerFunc { - return func( w http.ResponseWriter, req *http.Request ) { - - id, _ := router.ParamInt( req, "id" ) - - opts := artbase.SVGOpts{} - - mirror, ok := router.QueryBool( req, "mirror" ) - if !ok { - mirror, ok = router.QueryBool( req, "m" ) // allow shortcut m too - } - - if mirror { - opts.Mirror = true - } - - save, ok := router.QueryBool( req, "autosave" ) - if !ok { - save, ok = router.QueryBool( req, "save" ) // allow shortcut save too - } - - if save { - opts.Save = true - } - - b := col.HandleTileSVG( id, opts ) - - w.Header().Set( "Content-Type", "image/svg+xml" ) - w.Write( b ) - } -} - - - func main() { - ////// - // for debugging and double check on module print version strings - fmt.Println( "go package versions:" ) - fmt.Println( " artbase:", artbase.Version ) - fmt.Println( " pixelart:", pixelart.Version ) - fmt.Println( " router:", router.Version ) - fmt.Println() - - //// note: // use built-in "standard" collections for now, // yes, you can - use / set-up your own collections - // collections := collections.Standard - collections := collections.Ordinals - - - fmt.Printf( "%d collection(s):\n", len( collections )) - fmt.Println( collections ) - + collections := collections.Standard + // collections := collections.Ordinals - serve := router.Router{} - - serve.GET( "/", handleHome( collections ) ) - - for i, c := range collections { - fmt.Printf( " [%d] %s %dx%d - %s\n", i, c.Name, c.Width, c.Height, c.Path ) - - serve.GET( "/" + c.Name, handleCollection( c ) ) - serve.GET( "/" + c.Name + "-strip.png", handleCollectionStripPNG( c ) ) - - // note - &c will NOT work - as c as reference gets - // all handlers pointing to last collection!!!! - serve.GET( "/" + c.Name + `/(?P[0-9]+)(\.png)?`, handleCollectionImagePNG( c ) ) - serve.GET( "/" + c.Name + `/(?P[0-9]+)\.svg`, handleCollectionImageSVG( c ) ) - } + serve := serve.NewRouter( collections ) @@ -236,7 +40,7 @@ func main() { } - http.ListenAndServe( addr, &serve ) + http.ListenAndServe( addr, serve ) fmt.Println( "Bye!") } diff --git a/serve/image_png.go b/serve/image_png.go new file mode 100644 index 0000000..83f9299 --- /dev/null +++ b/serve/image_png.go @@ -0,0 +1,120 @@ +package serve + +import ( + // "fmt" + "log" + "net/http" + + "github.com/learnpixelart/pixelart.go/pixelart" + + "github.com/pixelartexchange/artbase.server/artbase" + "github.com/pixelartexchange/artbase.server/router" // simple http router & helpers from scratch (no 3rd party deps) - replace with your own http libs/frameworks +) + + + +func handleCollectionImagePNG( col artbase.Collection ) http.HandlerFunc { + return func( w http.ResponseWriter, req *http.Request ) { + + id, _ := router.ParamInt( req, "id" ) + + opts := artbase.PNGOpts{} + + + backgroundQuery, ok := router.Query( req, "background" ) + if !ok { + backgroundQuery, ok = router.Query( req, "bg" ) // allow shortcut z too + } + + if ok { + log.Printf( "=> parsing background color (in hex) >%s<...\n", backgroundQuery ) + + background, err := pixelart.ParseColor( backgroundQuery ) + if err != nil { + log.Panic( err ) + } + + opts.Background = background + opts.BackgroundName = backgroundQuery + } + + + silhouetteQuery, ok := router.Query( req, "silhouette" ) + if ok { + log.Printf( "=> parsing silhouette (forground) color (in hex) >%s<...\n", silhouetteQuery ) + + silhouette, err := pixelart.ParseColor( silhouetteQuery ) + if err != nil { + log.Panic( err ) + } + + opts.Silhouette = silhouette + opts.SilhouetteName = silhouetteQuery + } + + flag, ok := router.Query( req, "flag" ) + if ok { + opts.Flag = flag + } + + + mirror, ok := router.QueryBool( req, "mirror" ) + if !ok { + mirror, ok = router.QueryBool( req, "m" ) // allow shortcut m too + } + + if mirror { + opts.Mirror = true + } + + transparent, ok := router.QueryBool( req, "transparent" ) + if transparent { + opts.Transparent = true + } + + + circle, ok := router.QueryBool( req, "circle" ) + if circle { + opts.Circle = true + } + + + zoom, ok := router.QueryInt( req, "zoom" ) + if !ok { + zoom, ok = router.QueryInt( req, "z" ) // allow shortcut z too + } + + if zoom > 1 { + opts.Zoom = zoom + } + + size, ok := router.QueryInt( req, "size" ) + if !ok { + size, ok = router.QueryInt( req, "s" ) // allow shortcut s too + } + + if size > 0 { + opts.Resize = size + } + + + + save, ok := router.QueryBool( req, "autosave" ) + if !ok { + save, ok = router.QueryBool( req, "save" ) // allow shortcut save too + } + + if save { + opts.Save = true + } + + + b := col.HandleTilePNG( id, opts ) + + w.Header().Set( "Content-Type", "image/png" ) + w.Write( b ) + } +} + + + diff --git a/serve/image_svg.go b/serve/image_svg.go new file mode 100644 index 0000000..4031977 --- /dev/null +++ b/serve/image_svg.go @@ -0,0 +1,49 @@ +package serve + +import ( + // "fmt" + // "log" + "net/http" + + // "github.com/learnpixelart/pixelart.go/pixelart" + + "github.com/pixelartexchange/artbase.server/artbase" + "github.com/pixelartexchange/artbase.server/router" // simple http router & helpers from scratch (no 3rd party deps) - replace with your own http libs/frameworks +) + + + + +func handleCollectionImageSVG( col artbase.Collection ) http.HandlerFunc { + return func( w http.ResponseWriter, req *http.Request ) { + + id, _ := router.ParamInt( req, "id" ) + + opts := artbase.SVGOpts{} + + mirror, ok := router.QueryBool( req, "mirror" ) + if !ok { + mirror, ok = router.QueryBool( req, "m" ) // allow shortcut m too + } + + if mirror { + opts.Mirror = true + } + + save, ok := router.QueryBool( req, "autosave" ) + if !ok { + save, ok = router.QueryBool( req, "save" ) // allow shortcut save too + } + + if save { + opts.Save = true + } + + b := col.HandleTileSVG( id, opts ) + + w.Header().Set( "Content-Type", "image/svg+xml" ) + w.Write( b ) + } +} + + diff --git a/serve/serve.go b/serve/serve.go new file mode 100644 index 0000000..95e0cc2 --- /dev/null +++ b/serve/serve.go @@ -0,0 +1,82 @@ +package serve + +import ( + "fmt" + // "log" + // "os" + "net/http" + + "github.com/learnpixelart/pixelart.go/pixelart" + + "github.com/pixelartexchange/artbase.server/artbase" + "github.com/pixelartexchange/artbase.server/router" // simple http router & helpers from scratch (no 3rd party deps) - replace with your own http libs/frameworks +) + + +func handleHome( collections []artbase.Collection ) http.HandlerFunc { + return func( w http.ResponseWriter, req *http.Request ) { + b := renderHome( collections ) + + w.Header().Set( "Content-Type", "text/html; charset=utf-8" ) + w.Write( b ) + } +} + +func handleCollection( col artbase.Collection ) http.HandlerFunc { + return func( w http.ResponseWriter, req *http.Request ) { + b := renderCollection( &col ) + + w.Header().Set( "Content-Type", "text/html; charset=utf-8" ) + w.Write( b ) + } +} + +func handleCollectionStripPNG( col artbase.Collection ) http.HandlerFunc { + return func( w http.ResponseWriter, req *http.Request ) { + b := col.HandleStripPNG() + + w.Header().Set( "Content-Type", "image/png" ) + w.Write( b ) + } +} + + + + +func NewRouter( collections []artbase.Collection ) *router.Router { + + ////// + // for debugging and double check on module print version strings + fmt.Println( "go package versions:" ) + fmt.Println( " artbase:", artbase.Version ) + fmt.Println( " pixelart:", pixelart.Version ) + fmt.Println( " router:", router.Version ) + fmt.Println() + + fmt.Printf( "%d collection(s):\n", len( collections )) + fmt.Println( collections ) + + + + serve := router.Router{} + + serve.GET( "/", handleHome( collections ) ) + + for i, c := range collections { + fmt.Printf( " [%d] %s %dx%d - %s\n", i, c.Name, c.Width, c.Height, c.Path ) + + serve.GET( "/" + c.Name, handleCollection( c ) ) + serve.GET( "/" + c.Name + "-strip.png", handleCollectionStripPNG( c ) ) + + // note - &c will NOT work - as c as reference gets + // all handlers pointing to last collection!!!! + serve.GET( "/" + c.Name + `/(?P[0-9]+)(\.png)?`, handleCollectionImagePNG( c ) ) + serve.GET( "/" + c.Name + `/(?P[0-9]+)\.svg`, handleCollectionImageSVG( c ) ) + } + + fmt.Println( "Bye!" ) + + return &serve +} + + diff --git a/artbase/templates.go b/serve/templates.go similarity index 93% rename from artbase/templates.go rename to serve/templates.go index e51c5bd..bc1bfc6 100644 --- a/artbase/templates.go +++ b/serve/templates.go @@ -1,9 +1,11 @@ -package artbase +package serve import ( "html/template" "bytes" "fmt" + + "github.com/pixelartexchange/artbase.server/artbase" ) @@ -145,7 +147,7 @@ var Templates = make( map[string]*template.Template ) func init() { - fmt.Println( " [artbase.init] compileTemplates" ) + fmt.Println( " [serve.init] compileTemplates" ) compileTemplates() } @@ -156,14 +158,16 @@ func compileTemplates() { -func RenderHome( data []Collection ) []byte { + + +func renderHome( data []artbase.Collection ) []byte { buf := new( bytes.Buffer ) Templates["home"].Execute( buf, data ) return buf.Bytes() } -func RenderCollection( data *Collection ) []byte { +func renderCollection( data *artbase.Collection ) []byte { buf := new( bytes.Buffer ) Templates["index"].Execute( buf, data ) return buf.Bytes()