|
|
@ -1,16 +1,11 @@
|
|
|
|
package main
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"flag"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
// "log"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"strconv"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"code.ndumas.com/ndumas/badges"
|
|
|
|
"code.ndumas.com/ndumas/badges"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
@ -23,39 +18,15 @@ type BadgeRenderer struct {
|
|
|
|
Fonts map[string]string
|
|
|
|
Fonts map[string]string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewBadgeRenderer(fontDir string) (*BadgeRenderer, error) {
|
|
|
|
func NewBadgeRenderer() *BadgeRenderer {
|
|
|
|
var br BadgeRenderer
|
|
|
|
var br BadgeRenderer
|
|
|
|
br.Fonts = make(map[string]string)
|
|
|
|
br.Fonts = make(map[string]string)
|
|
|
|
|
|
|
|
|
|
|
|
root := os.DirFS(fontDir)
|
|
|
|
// walkfunc to find ttf files here
|
|
|
|
err := fs.WalkDir(root, ".", func(path string, d fs.DirEntry, err error) error {
|
|
|
|
return &br
|
|
|
|
if strings.HasSuffix(path, ".ttf") {
|
|
|
|
|
|
|
|
absFont := filepath.Join(fontDir, path)
|
|
|
|
|
|
|
|
br.Fonts[filepath.Base(path)] = absFont
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return &br, fmt.Errorf("error walking for font files: %w", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return &br, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (br *BadgeRenderer) listFonts(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func (br *BadgeRenderer) listFonts(w http.ResponseWriter, r *http.Request) {
|
|
|
|
style := chi.URLParam(r, "format")
|
|
|
|
|
|
|
|
switch style {
|
|
|
|
|
|
|
|
case "json":
|
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
|
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
|
|
|
|
err := enc.Encode(br.Fonts)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
w.WriteHeader(400)
|
|
|
|
|
|
|
|
fmt.Fprintf(w, "couldn't encode font list to json: %s\n", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (br *BadgeRenderer) renderBadge(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func (br *BadgeRenderer) renderBadge(w http.ResponseWriter, r *http.Request) {
|
|
|
@ -117,16 +88,6 @@ func (br *BadgeRenderer) renderBadge(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch q["style"][0] {
|
|
|
|
switch q["style"][0] {
|
|
|
|
case "flat":
|
|
|
|
case "flat":
|
|
|
|
fmt.Fprintf(w, "%s", bg.GenerateFlat(label, message, color))
|
|
|
|
fmt.Fprintf(w, "%s", bg.GenerateFlat(label, message, color))
|
|
|
|
case "flatsimple":
|
|
|
|
|
|
|
|
fmt.Fprintf(w, "%s", bg.GenerateFlatSimple(message, color))
|
|
|
|
|
|
|
|
case "flatsquare":
|
|
|
|
|
|
|
|
fmt.Fprintf(w, "%s", bg.GenerateFlatSquare(label, message, color))
|
|
|
|
|
|
|
|
case "flatsquaresimple":
|
|
|
|
|
|
|
|
fmt.Fprintf(w, "%s", bg.GenerateFlatSquareSimple(message, color))
|
|
|
|
|
|
|
|
case "plastic":
|
|
|
|
|
|
|
|
fmt.Fprintf(w, "%s", bg.GeneratePlastic(label, message, color))
|
|
|
|
|
|
|
|
case "plasticsimple":
|
|
|
|
|
|
|
|
fmt.Fprintf(w, "%s", bg.GeneratePlasticSimple(message, color))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
w.WriteHeader(400)
|
|
|
|
w.WriteHeader(400)
|
|
|
@ -134,28 +95,18 @@ func (br *BadgeRenderer) renderBadge(w http.ResponseWriter, r *http.Request) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var (
|
|
|
|
var (
|
|
|
|
port int
|
|
|
|
port int
|
|
|
|
fontsDir string
|
|
|
|
fontsDir string
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.IntVar(&port, "port", 8484, "http listening port")
|
|
|
|
flag.IntVar(&port, "port", 8484, "http listening port")
|
|
|
|
flag.StringVar(&fontsDir, "fonts", "fonts/", "directory containing ttf files")
|
|
|
|
flag.StringVar(&fontsDir, "fonts", "fonts/", "directory containing ttf files")
|
|
|
|
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
|
|
|
|
absFontDir, err := filepath.Abs(fontsDir)
|
|
|
|
br := NewBadgeRenderer()
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Fatalf("error building absolute font dir path: %s\n", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("absFontDir:", absFontDir)
|
|
|
|
|
|
|
|
br, err := NewBadgeRenderer(absFontDir)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
fmt.Println("couldn't create renderer", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
r := chi.NewRouter()
|
|
|
|
r := chi.NewRouter()
|
|
|
|
r.Use(middleware.Logger)
|
|
|
|
r.Use(middleware.Logger)
|
|
|
|