mirror of
https://github.com/taigrr/gico.git
synced 2026-04-02 03:09:07 -07:00
add working webserver example
This commit is contained in:
41
bin/main.go
Normal file
41
bin/main.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
1
go.mod
1
go.mod
@@ -6,6 +6,7 @@ replace github.com/taigrr/go-colorpallettes => ../go-colorpallettes
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b
|
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/oksvg v0.0.0-20220128195007-1f435e4c2b44
|
||||||
github.com/srwiley/rasterx v0.0.0-20220615024203-67b7089efd25
|
github.com/srwiley/rasterx v0.0.0-20220615024203-67b7089efd25
|
||||||
github.com/taigrr/go-colorpallettes v0.0.0-00010101000000-000000000000
|
github.com/taigrr/go-colorpallettes v0.0.0-00010101000000-000000000000
|
||||||
|
|||||||
2
go.sum
2
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/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 h1:slYM766cy2nI3BwyRiyQj/Ud48djTMtMebDqepE95rw=
|
||||||
github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b/go.mod h1:1KcenG0jGWcpt8ov532z81sp/kMMUG485J2InIOyADM=
|
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/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 h1:XPYXKIuH/n5zpUoEWk2jWV/SjEMNYmqDYmTgbjmhtaI=
|
||||||
github.com/srwiley/oksvg v0.0.0-20220128195007-1f435e4c2b44/go.mod h1:cNQ3dwVJtS5Hmnjxy6AgTPd0Inb3pW05ftPSX7NZO7Q=
|
github.com/srwiley/oksvg v0.0.0-20220128195007-1f435e4c2b44/go.mod h1:cNQ3dwVJtS5Hmnjxy6AgTPd0Inb3pW05ftPSX7NZO7Q=
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ func CreateGraph() bytes.Buffer {
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
colors := []string{"#767960", "#a7297f", "#e8ca89", "#f5efd6", "#858966"}
|
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 {
|
for _, c := range colors {
|
||||||
color := sc.FromHexString(c)
|
color := sc.FromHexString(c)
|
||||||
colorScheme = append(colorScheme, color)
|
colorScheme = append(colorScheme, color)
|
||||||
@@ -51,6 +51,31 @@ func ColorForFrequency(freq, min, max int) sc.SimpleColor {
|
|||||||
}
|
}
|
||||||
return colorScheme[colorIndex]
|
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 {
|
func GetYearImage(frequencies []int) bytes.Buffer {
|
||||||
squareColors := []sc.SimpleColor{}
|
squareColors := []sc.SimpleColor{}
|
||||||
min, max := minmax(frequencies)
|
min, max := minmax(frequencies)
|
||||||
|
|||||||
28
main.go
28
main.go
@@ -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)
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user