From 5f0293992b85193b7cc53eb2d3048c6628419849 Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Sun, 19 Jun 2022 12:08:26 -0400 Subject: [PATCH] add working webserver example --- bin/main.go | 41 +++++++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ graph/graph.go | 27 ++++++++++++++++++++++++++- main.go | 28 ---------------------------- 5 files changed, 70 insertions(+), 29 deletions(-) create mode 100644 bin/main.go delete mode 100644 main.go diff --git a/bin/main.go b/bin/main.go new file mode 100644 index 0000000..d16f447 --- /dev/null +++ b/bin/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "math/rand" + "net/http" + "time" + + "github.com/gorilla/mux" + "github.com/taigrr/gitgraph/graph" +) + +type DayCount [366]int + +func init() { + rand.Seed(time.Now().UnixMilli()) +} +func main() { + r := mux.NewRouter() + r.HandleFunc("/weekly.svg", func(w http.ResponseWriter, r *http.Request) { + freq := []int{} + for i := 0; i < 7; i++ { + freq = append(freq, rand.Int()) + } + w.Header().Add("Content-Type", "text/html") + svg := graph.GetWeekImage(freq) + svg.WriteTo(w) + + }) + r.HandleFunc("/yearly.svg", func(w http.ResponseWriter, r *http.Request) { + freq := []int{} + for i := 0; i < 365; i++ { + freq = append(freq, rand.Int()) + } + svg := graph.GetYearImage(freq) + w.Header().Add("Content-Type", "text/html") + svg.WriteTo(w) + + }) + + http.ListenAndServe("0.0.0.0:5578", r) +} diff --git a/go.mod b/go.mod index 3859143..e75dc61 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ replace github.com/taigrr/go-colorpallettes => ../go-colorpallettes require ( github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b + github.com/gorilla/mux v1.8.0 github.com/srwiley/oksvg v0.0.0-20220128195007-1f435e4c2b44 github.com/srwiley/rasterx v0.0.0-20220615024203-67b7089efd25 github.com/taigrr/go-colorpallettes v0.0.0-00010101000000-000000000000 diff --git a/go.sum b/go.sum index 0bcf8d6..e02bc20 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,8 @@ github.com/ajstarks/deck v0.0.0-20200831202436-30c9fc6549a9/go.mod h1:JynElWSGnm github.com/ajstarks/deck/generate v0.0.0-20210309230005-c3f852c02e19/go.mod h1:T13YZdzov6OU0A1+RfKZiZN9ca6VeKdBdyDV+BY97Tk= github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b h1:slYM766cy2nI3BwyRiyQj/Ud48djTMtMebDqepE95rw= github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b/go.mod h1:1KcenG0jGWcpt8ov532z81sp/kMMUG485J2InIOyADM= +github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= +github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/srwiley/oksvg v0.0.0-20220128195007-1f435e4c2b44 h1:XPYXKIuH/n5zpUoEWk2jWV/SjEMNYmqDYmTgbjmhtaI= github.com/srwiley/oksvg v0.0.0-20220128195007-1f435e4c2b44/go.mod h1:cNQ3dwVJtS5Hmnjxy6AgTPd0Inb3pW05ftPSX7NZO7Q= diff --git a/graph/graph.go b/graph/graph.go index 991ad5e..7c3fa85 100644 --- a/graph/graph.go +++ b/graph/graph.go @@ -26,7 +26,7 @@ func CreateGraph() bytes.Buffer { func init() { colors := []string{"#767960", "#a7297f", "#e8ca89", "#f5efd6", "#858966"} - colors = []string{"#161b22", "#0e4429", "#006d32", "#26a641", "#39d353"} + // colors = []string{"#000000", "#0e4429", "#006d32", "#26a641", "#39d353"} for _, c := range colors { color := sc.FromHexString(c) colorScheme = append(colorScheme, color) @@ -51,6 +51,31 @@ func ColorForFrequency(freq, min, max int) sc.SimpleColor { } return colorScheme[colorIndex] } +func GetWeekImage(frequencies []int) bytes.Buffer { + squareColors := []sc.SimpleColor{} + min, max := minmax(frequencies) + for _, f := range frequencies { + squareColors = append(squareColors, ColorForFrequency(f, min, max)) + } + return drawWeekImage(squareColors) +} + +func drawWeekImage(c []sc.SimpleColor) bytes.Buffer { + var sb bytes.Buffer + sbw := bufio.NewWriter(&sb) + squareLength := 10 + width := (len(c) + 1) * squareLength * 2 + height := squareLength * 2 + canvas := svg.New(sbw) + canvas.Start(width, height) + canvas.Rect(0, 0, width, height, "fill:black") + for i, s := range c { + canvas.Square(squareLength*2*(i+1), squareLength/2, squareLength, fmt.Sprintf("fill:%s", s.HexString())) + } + canvas.End() + sbw.Flush() + return sb +} func GetYearImage(frequencies []int) bytes.Buffer { squareColors := []sc.SimpleColor{} min, max := minmax(frequencies) diff --git a/main.go b/main.go deleted file mode 100644 index af89bab..0000000 --- a/main.go +++ /dev/null @@ -1,28 +0,0 @@ -package main - -import ( - "fmt" - "math/rand" - "os" - "time" - - "github.com/taigrr/gitgraph/graph" -) - -type DayCount [366]int - -func main() { - freq := []int{} - rand.Seed(time.Now().UnixMilli()) - for i := 0; i < 366; i++ { - freq = append(freq, rand.Int()) - } - svg := graph.GetYearImage(freq) - f, err := os.Create("out.svg") - if err != nil { - fmt.Printf("Error: %v\n", err) - return - } - defer f.Close() - svg.WriteTo(f) -}