New web server without Echo

This commit is contained in:
nefrace 2022-12-06 02:04:05 +03:00
parent b38e1b97bb
commit 4e566187f0
9 changed files with 138 additions and 83 deletions

View File

@ -7,10 +7,12 @@ import (
"github.com/NicoNex/echotron/v3"
"github.com/google/uuid"
database "nefrace.ru/doomer/db"
)
func (b *bot) inlineHandler(update *echotron.Update) {
cache := b.doomer.db.Prefix(FileIDItem)
cache := b.doomer.db.Prefix(database.FileIDItem)
if update.InlineQuery.Query == "" {
return

View File

@ -1,4 +1,4 @@
package main
package db
import (
"time"
@ -10,14 +10,21 @@ type Db struct {
db *badger.DB
}
var database Db
func NewDatabase() (*Db, error) {
db, err := badger.Open(badger.DefaultOptions("./database"))
if err != nil {
return nil, err
}
return &Db{
database = Db{
db: db,
}, nil
}
return &database, nil
}
func DB() *Db {
return &database
}
type PrefixStore struct {

View File

@ -7,15 +7,16 @@ import (
"github.com/fogleman/gg"
"golang.org/x/image/font"
database "nefrace.ru/doomer/db"
)
type Doomer struct {
Face font.Face
db *Db
db *database.Db
Border int
}
func NewDoomer(db *Db) *Doomer {
func NewDoomer(db *database.Db) *Doomer {
face, err := gg.LoadFontFace("./fonts/AmazDooMLeft.ttf", 108)
if err != nil {
log.Fatalf("Error loading font: %v", err)
@ -30,7 +31,7 @@ func NewDoomer(db *Db) *Doomer {
func (d *Doomer) GetImage(text string, bgType BackgroundType) (*[]byte, error) {
hashed := hash(text)
key := hashed + ":" + fmt.Sprint(bgType)
store := d.db.Prefix(ImageItem)
store := d.db.Prefix(database.ImageItem)
img, err := store.GetItem(key)
if err != nil {
image, err := d.GenImage(text, bgType)

8
go.mod
View File

@ -7,7 +7,6 @@ require (
github.com/dgraph-io/badger/v3 v3.2103.4
github.com/fogleman/gg v1.3.0
github.com/google/uuid v1.3.0
github.com/labstack/echo/v4 v4.9.1
golang.org/x/image v0.1.0
)
@ -24,16 +23,9 @@ require (
github.com/golang/snappy v0.0.4 // indirect
github.com/google/flatbuffers v22.11.23+incompatible // indirect
github.com/klauspost/compress v1.15.12 // indirect
github.com/labstack/gommon v0.4.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.3.0 // indirect
golang.org/x/net v0.2.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)

25
go.sum
View File

@ -88,17 +88,7 @@ github.com/klauspost/compress v1.15.12/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrD
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/labstack/echo/v4 v4.9.1 h1:GliPYSpzGKlyOhqIbG8nmHBo3i1saKWFOgh41AN3b+Y=
github.com/labstack/echo/v4 v4.9.1/go.mod h1:Pop5HLc+xoc4qhTZ1ip6C0RtP7Z+4VzRLWZZFKqbbjo=
github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
@ -122,17 +112,11 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
@ -145,8 +129,6 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.3.0 h1:a06MkbcxBrEFc0w0QIZWXrH/9cCX6KJyWbBOIwAn+7A=
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/image v0.1.0 h1:r8Oj8ZA2Xy12/b5KZYj3tuv7NG/fBz3TwQVvpJ9l8Rk=
golang.org/x/image v0.1.0/go.mod h1:iyPr49SD/G/TBxYVB/9RRtGUT5eNbo2u4NamWeQcD5c=
@ -185,12 +167,8 @@ golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@ -200,8 +178,6 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
@ -245,7 +221,6 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

10
main.go
View File

@ -4,11 +4,12 @@ import (
"log"
"nefrace.ru/doomer/config"
"nefrace.ru/doomer/db"
)
func main() {
cfg := config.InitConfig()
db, err := NewDatabase()
config.InitConfig()
db, err := db.NewDatabase()
if err != nil {
log.Fatalf("Cant open database: %v", err)
}
@ -17,10 +18,9 @@ func main() {
doomer := NewDoomer(db)
dp := NewDispatcher(doomer)
server := CreateServer(db)
app := InitServer(":3000")
go func() {
log.Fatal(server.Start(cfg.Host))
log.Fatal(app.Run())
}()
log.Println(dp.Poll())
}

View File

@ -1,38 +0,0 @@
package main
import (
"bytes"
"image/jpeg"
"image/png"
"github.com/labstack/echo/v4"
)
func CreateServer(db *Db) *echo.Echo {
e := echo.New()
e.GET("/:key", ImageHandler(db))
return e
}
func ImageHandler(db *Db) echo.HandlerFunc {
store := db.Prefix(ImageItem)
return func(c echo.Context) error {
key := c.Param("key")
isJpeg := c.QueryParam("jpeg")
img, err := store.GetItem(key)
if err != nil {
return c.String(404, err.Error())
}
if isJpeg != "1" {
return c.Blob(200, "image/png", img)
}
png, _ := png.Decode(bytes.NewReader(img))
jpg := new(bytes.Buffer)
err = jpeg.Encode(jpg, png, &jpeg.Options{Quality: 95})
if err != nil {
return c.String(500, err.Error())
}
return c.Blob(200, "image/jpeg", jpg.Bytes())
}
}

88
server/server.go Normal file
View File

@ -0,0 +1,88 @@
package server
import (
"encoding/json"
"log"
"net/http"
"net/url"
)
type ApiError struct {
Err string
Status int
}
func (e ApiError) Error() string {
return e.Err
}
type Api struct {
mux *http.ServeMux
host string
}
func NewApi(host string) Api {
return Api{
mux: http.NewServeMux(),
host: host,
}
}
func (a Api) Run() error {
log.Println("Server is running at ", a.host)
return http.ListenAndServe(a.host, a.mux)
}
func (a Api) Group(path string) group {
return group{path: path, api: &a}
}
type group struct {
api *Api
path string
}
func (g group) Group(path string) group {
p, _ := url.JoinPath(g.path, path)
return group{path: p, api: g.api}
}
type apiFunc func(http.ResponseWriter, *http.Request) error
func (g group) Endpoint(path string, f apiFunc) *group {
p, _ := url.JoinPath(g.path, path)
log.Println("New path registered: ", p, f)
g.api.mux.HandleFunc(p, func(w http.ResponseWriter, r *http.Request) {
if err := f(w, r); err != nil {
if e, ok := err.(ApiError); ok {
WriteJSON(w, e.Status, e)
return
}
WriteJSON(w, 500, ApiError{Err: err.Error(), Status: http.StatusInternalServerError})
return
}
})
return &g
}
func StatusAndContent(w http.ResponseWriter, status int, contentType string) {
w.WriteHeader(status)
w.Header().Add("Content-Type", contentType)
}
func WriteJSON(w http.ResponseWriter, status int, v any) error {
StatusAndContent(w, status, "application/json")
return json.NewEncoder(w).Encode(v)
}
func WritePlain(w http.ResponseWriter, status int, text string) error {
StatusAndContent(w, status, "text/plain")
_, err := w.Write([]byte(text))
return err
}
func WriteBlob(w http.ResponseWriter, status int, contentType string, blob []byte) error {
StatusAndContent(w, status, contentType)
_, err := w.Write(blob)
return err
}

28
webapi.go Normal file
View File

@ -0,0 +1,28 @@
package main
import (
"net/http"
database "nefrace.ru/doomer/db"
"nefrace.ru/doomer/server"
)
func InitServer(host string) server.Api {
app := server.NewApi(host)
app.Group("/api").Endpoint("gen", ImageHandler)
return app
}
func ImageHandler(w http.ResponseWriter, r *http.Request) error {
db := database.DB()
doomer := NewDoomer(db)
text := r.URL.Query().Get("text")
img, err := doomer.GetImage(text, Black)
if err != nil {
return server.ApiError{
Err: "cannot generate image: " + err.Error(),
Status: http.StatusInternalServerError,
}
}
return server.WriteBlob(w, 200, "image/png", *img)
}