Skip to content

Commit

Permalink
server: migrate iris => fiber
Browse files Browse the repository at this point in the history
  • Loading branch information
avakarev committed Nov 26, 2023
1 parent 97166a7 commit 10c0959
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 436 deletions.
44 changes: 8 additions & 36 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,67 +5,39 @@ go 1.21.3
require (
github.com/avakarev/go-util v1.1.13
github.com/go-gormigrate/gormigrate/v2 v2.1.1
github.com/kataras/golog v0.1.11
github.com/kataras/iris/v12 v12.2.8
github.com/gofiber/contrib/fiberzerolog v0.2.2
github.com/gofiber/fiber/v2 v2.51.0
github.com/rs/zerolog v1.31.0
github.com/stretchr/objx v0.5.1
github.com/valyala/fasthttp v1.51.0
gorm.io/datatypes v1.2.0
gorm.io/driver/sqlite v1.5.4
gorm.io/gorm v1.25.5
)

require (
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53 // indirect
github.com/CloudyKit/jet/v6 v6.2.0 // indirect
github.com/Joker/jade v1.1.3 // indirect
github.com/Shopify/goreferrer v0.0.0-20220729165902-8cddb4f5de06 // indirect
github.com/andybalholm/brotli v1.0.6 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/flosch/pongo2/v4 v4.0.2 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.16.0 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/gomarkdown/markdown v0.0.0-20230922112808-5421fefb8386 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/iris-contrib/schema v0.0.6 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/kataras/blocks v0.0.8 // indirect
github.com/kataras/pio v0.0.13 // indirect
github.com/kataras/sitemap v0.0.6 // indirect
github.com/kataras/tunnel v0.0.4 // indirect
github.com/klauspost/compress v1.17.2 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mailgun/raymond/v2 v2.0.48 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/microcosm-cc/bluemonday v1.0.26 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/schollz/closestmatch v2.1.0+incompatible // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/tdewolff/minify/v2 v2.20.6 // indirect
github.com/tdewolff/parse/v2 v2.7.4 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/yosssi/ace v0.0.5 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.4.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/mysql v1.4.7 // indirect
)
164 changes: 14 additions & 150 deletions go.sum

Large diffs are not rendered by default.

101 changes: 0 additions & 101 deletions internal/irisutil/irisutil.go

This file was deleted.

20 changes: 6 additions & 14 deletions internal/web/api/awtrixlight/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import (
"fmt"
"net/http"

"github.com/avakarev/go-util/httputil"
"github.com/kataras/iris/v12"
"github.com/rs/zerolog/log"

"github.com/homeland-live/awtrix-light-hub/internal/irisutil"
"github.com/gofiber/fiber/v2"
)

const awtrixLightReleaseURL = "https://api.github.com/repos/Blueforcer/awtrix-light/releases/latest"
Expand All @@ -37,17 +33,13 @@ func reqRelease() (*release, error) {

}

// Routes adds health api routes to the iris app
func Routes(api iris.Party) {
api.Get("/v1/awtrix-light/release", func(ctx iris.Context) {
// Routes adds health api routes to the fiber
func Routes(api fiber.Router) {
api.Get("/v1/awtrix-light/release", func(c *fiber.Ctx) error {
rel, err := reqRelease()
if err != nil {
log.Error().Err(err).Send()
irisutil.WriteErrJSON(httputil.NewErrFrom(err), ctx)
return
}
if err := ctx.JSON(iris.Map{"release": rel}); err != nil {
log.Error().Err(err).Send()
return err
}
return c.JSON(fiber.Map{"release": rel})
})
}
13 changes: 5 additions & 8 deletions internal/web/api/health/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
package health

import (
"github.com/kataras/iris/v12"
"github.com/rs/zerolog/log"
"github.com/gofiber/fiber/v2"
)

// Routes adds health api routes to the iris app
func Routes(api iris.Party) {
api.Get("/v1/health", func(ctx iris.Context) {
if err := ctx.JSON(iris.Map{"healthy": true}); err != nil {
log.Error().Err(err).Send()
}
// Routes adds health api routes to the fiber
func Routes(api fiber.Router) {
api.Get("/v1/health", func(c *fiber.Ctx) error {
return c.JSON(fiber.Map{"healthy": true})
})
}
76 changes: 26 additions & 50 deletions internal/web/api/node/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,92 +2,68 @@ package node

import (
"fmt"
"net/http"

"github.com/avakarev/go-util/httputil"
"github.com/kataras/iris/v12"
"github.com/rs/zerolog/log"
"github.com/gofiber/fiber/v2"
"github.com/stretchr/objx"

"github.com/homeland-live/awtrix-light-hub/internal/db"
"github.com/homeland-live/awtrix-light-hub/internal/irisutil"
)

// ListNodesHandler handles `/nodes` http get requests
func ListNodesHandler(ctx iris.Context) {
func ListNodesHandler(c *fiber.Ctx) error {
nodes := db.ListNodes()
irisutil.WriteJSON(iris.Map{"nodes": nodes}, ctx)
return c.JSON(fiber.Map{"nodes": nodes})
}

// CreateNodeHandler handles `/nodes` http post requests
func CreateNodeHandler(ctx iris.Context) {
func CreateNodeHandler(c *fiber.Ctx) error {
var n db.Node
if err := ctx.ReadJSON(&n); err != nil {
log.Error().Err(err).Send()
irisutil.WriteErrJSON(httputil.NewErr(http.StatusBadRequest, err.Error()), ctx)
return
if err := c.BodyParser(&n); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}

if err := db.CreateNode(&n); err != nil {
log.Error().Err(err).Send()
irisutil.WriteErrJSON(httputil.NewErrFrom(err), ctx)
return
return err
}

ctx.StatusCode(http.StatusCreated)
irisutil.WriteJSON(iris.Map{"node": n}, ctx)
return c.Status(fiber.StatusCreated).JSON(fiber.Map{"node": n})
}

// GetNodeHandler handles `/nodes/:id` http get requests
func GetNodeHandler(ctx iris.Context) {
id := ctx.Params().Get("id")
func GetNodeHandler(c *fiber.Ctx) error {
id := c.Params("id")
n := db.FindNodeByID(id)
if n == nil {
irisutil.WriteErrJSON(
httputil.NewErr(
http.StatusNotFound,
fmt.Sprintf("no node found with given id %s", id),
), ctx)
return
return fiber.NewError(
fiber.StatusNotFound,
fmt.Sprintf("no node found with given id %s", id),
)
}
irisutil.WriteJSON(iris.Map{"node": n}, ctx)
return c.JSON(fiber.Map{"node": n})
}

// UpdateNodeHandler handles `/nodes/:id` http patch requests
func UpdateNodeHandler(ctx iris.Context) {
id := ctx.Params().Get("id")
func UpdateNodeHandler(c *fiber.Ctx) error {
id := c.Params("id")
n := db.FindNodeByID(id)
if n == nil {
irisutil.WriteErrJSON(
httputil.NewErr(
http.StatusNotFound,
fmt.Sprintf("no node found with given id %s", id),
), ctx)
return
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("no node found with given id %s", id))
}
data := objx.Map{}
if err := ctx.ReadJSON(&data); err != nil {
log.Error().Err(err).Send()
irisutil.WriteErrJSON(httputil.NewErr(http.StatusBadRequest, err.Error()), ctx)
return
if err := c.BodyParser(&n); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
if err := db.UpdateNode(n, data); err != nil {
log.Error().Err(err).Send()
irisutil.WriteErrJSON(httputil.NewErrFrom(err), ctx)
return
return err
}
irisutil.WriteJSON(iris.Map{"node": n}, ctx)
return c.JSON(fiber.Map{"node": n})
}

// DeleteNodeHandler handles `/nodes/:id` http delete requests
func DeleteNodeHandler(ctx iris.Context) {
id := ctx.Params().Get("id")
func DeleteNodeHandler(c *fiber.Ctx) error {
id := c.Params("id")
if err := db.DeleteNode(id); err != nil {
log.Error().Err(err).Send()
irisutil.WriteErrJSON(httputil.NewErrFrom(err), ctx)
return
return err
}

ctx.StatusCode(http.StatusOK)
irisutil.WriteJSON(iris.Map{}, ctx)
return c.Status(fiber.StatusOK).JSON(fiber.Map{})
}
23 changes: 11 additions & 12 deletions internal/web/api/node/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@
package node

import (
"github.com/kataras/iris/v12"
"github.com/gofiber/fiber/v2"
)

// Routes adds node api routes to the iris app
func Routes(api iris.Party) {
api.PartyFunc("/v1/nodes", func(nodes iris.Party) {
nodes.Get("/", ListNodesHandler)
nodes.Post("/", CreateNodeHandler)
nodes.PartyFunc("/{id:string format(uuid)}", func(node iris.Party) {
node.Get("/", GetNodeHandler)
node.Patch("/", UpdateNodeHandler)
node.Delete("/", DeleteNodeHandler)
})
})
// Routes adds node api routes to the fiber
func Routes(api fiber.Router) {
nodes := api.Group("/v1/nodes")
nodes.Get("/", ListNodesHandler)
nodes.Post("/", CreateNodeHandler)

node := nodes.Group("/:id")
node.Get("/", GetNodeHandler)
node.Patch("/", UpdateNodeHandler)
node.Delete("/", DeleteNodeHandler)
}
Loading

0 comments on commit 10c0959

Please sign in to comment.