Skip to content

Add indigo go networking framework #9965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions frameworks/Go/fiber/src/server_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package main

import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -37,7 +36,7 @@ func Benchmark_Plaintext(b *testing.B) {
utils.AssertEqual(b, nil, err, "app.Test(req)")
utils.AssertEqual(b, 200, resp.StatusCode, "Status code")
utils.AssertEqual(b, fiber.MIMETextPlainCharsetUTF8, resp.Header.Get("Content-Type"))
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
utils.AssertEqual(b, helloworldRaw, body)
}

Expand Down Expand Up @@ -66,6 +65,6 @@ func Benchmark_JSON(b *testing.B) {
utils.AssertEqual(b, nil, err, "app.Test(req)")
utils.AssertEqual(b, 200, resp.StatusCode, "Status code")
utils.AssertEqual(b, fiber.MIMEApplicationJSON, resp.Header.Get("Content-Type"))
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
utils.AssertEqual(b, `{"message":"Hello, World!"}`, string(body))
}
14 changes: 14 additions & 0 deletions frameworks/Go/indigo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# [Indigo](https://github.com/indigo-web/indigo) (Go) Benchmarking Test

This is the go portion of a [benchmarking test suite](https://www.techempower.com/benchmarks/) comparing a variety of web development platforms.

> Indigo is a web-framework focusing at performance, elegancy and robustness.

## Test URLs
* http://localhost:8080/json
* http://localhost:8080/db
* http://localhost:8080/query?n=[1-500]
* http://localhost:8080/update?n=[1-500]
* http://localhost:8080/cached-query?n=[1-500]
* http://localhost:8080/fortune
* http://localhost:8080/plaintext
31 changes: 31 additions & 0 deletions frameworks/Go/indigo/benchmark_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"framework": "indigo",
"tests": [
{
"default": {
"json_url": "/json",
"db_url": "/db",
"query_url": "/query?n=",
"update_url": "/update?n=",
"cached_query_url": "/cached-query?n=",
"fortune_url": "/fortunes",
"plaintext_url": "/plaintext",
"port": 8080,
"approach": "Realistic",
"classification": "Platform",
"database": "Postgres",
"framework": "indigo",
"language": "Go",
"flavor": "None",
"orm": "Raw",
"platform": "None",
"webserver": "None",
"os": "Linux",
"database_os": "Linux",
"display_name": "Indigo",
"notes": "",
"versus": "go"
}
}
]
}
20 changes: 20 additions & 0 deletions frameworks/Go/indigo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[framework]
name = "indigo"

[main]
urls.json = "/json"
urls.db = "/db"
urls.query = "/query?n="
urls.update = "/update?n="
urls.cached_query = "/cached-query?n="
urls.fortune = "/fortunes"
urls.plaintext = "/plaintext"
approach = "Realistic"
classification = "Platform"
database = "Postgres"
database_os = "Linux"
os = "Linux"
orm = "Raw"
platform = "None"
webserver = "None"
versus = "go"
19 changes: 19 additions & 0 deletions frameworks/Go/indigo/indigo.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM golang:1.25-alpine3.22 as builder

WORKDIR /indigo

COPY ./src /indigo

RUN go mod download && \
go generate -x ./templates && \
GOAMD64=v3 go build -ldflags="-s -w" -o app .

FROM alpine:3.22

WORKDIR /indigo

COPY --from=builder /indigo/app .

EXPOSE 8080

CMD ./app
163 changes: 163 additions & 0 deletions frameworks/Go/indigo/src/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package main

import (
"context"
"math/rand/v2"
"slices"
"strconv"
"time"

"github.com/indigo-web/indigo/http"
"github.com/indigo-web/indigo/http/mime"

"indigo/app/models"
"indigo/app/templates"
)

type (
App struct {
DB *DB
Cache models.Worlds
}
)

func NewApp(DB *DB) *App {
return &App{DB: DB}
}

func (app *App) PopulateCache(ctx context.Context) error {
cache := make(models.Worlds, 10000)

err := app.DB.FillWorlds(context.Background(), cache)
if err != nil {
return err
}

app.Cache = cache

return nil
}

func (app *App) HandleJSON(request *http.Request) *http.Response {
return request.Respond().Header("Date", time.Now().Format(time.RFC1123)).Header("Server", "indigo").JSON(&models.Message{
Message: "Hello, World!",
})
}

func (app *App) HandleDB(request *http.Request) *http.Response {
world := &models.World{
ID: rand.IntN(10000) + 1,
}

err := app.DB.FillWorldByID(context.Background(), world)
if err != nil {
return http.Error(request, err)
}

return request.Respond().Header("Date", time.Now().Format(time.RFC1123)).Header("Server", "indigo").JSON(world)
}

func (app *App) HandleQuery(request *http.Request) *http.Response {
n := normalizeNumber(request.Params.Get("n"))

i, worlds := 0, make(models.Worlds, n)

for i = range worlds {
worlds[i].ID = rand.IntN(10000) + 1
}

err := app.DB.FillWorldsByID(context.Background(), worlds)
if err != nil {
return http.Error(request, err)
}

return request.Respond().Header("Date", time.Now().Format(time.RFC1123)).Header("Server", "indigo").JSON(&worlds)
}

func (app *App) HandleUpdate(request *http.Request) *http.Response {
n := normalizeNumber(request.Params.Get("n"))

i, worlds := 0, make(models.Worlds, n)

for i = range worlds {
worlds[i].ID = rand.IntN(10000) + 1
}

err := app.DB.FillWorldsByID(context.Background(), worlds)
if err != nil {
return http.Error(request, err)
}

for i = range worlds {
worlds[i].RandomNumber = rand.IntN(10000) + 1
}

slices.SortFunc(worlds, func(a, b models.World) int {
return a.ID - b.ID
})

err = app.DB.UpdateWorlds(context.Background(), worlds)
if err != nil {
return http.Error(request, err)
}

return request.Respond().Header("Date", time.Now().Format(time.RFC1123)).Header("Server", "indigo").JSON(&worlds)
}

func (app *App) HandleCachedQuery(request *http.Request) *http.Response {
n := normalizeNumber(request.Params.Get("n"))

i, worlds := 0, make(models.Worlds, n)

for i = range worlds {
worlds[i] = app.Cache[rand.Int32N(10000)]
}

return request.Respond().Header("Date", time.Now().Format(time.RFC1123)).Header("Server", "indigo").JSON(&worlds)
}

func (app *App) HandleFortune(request *http.Request) *http.Response {
fortunes, err := app.DB.GetFortunes(context.Background())
if err != nil {
return http.Error(request, err)
}

fortunes = append(fortunes, models.Fortune{
Message: "Additional fortune added at request time.",
})

slices.SortFunc(fortunes, func(a, b models.Fortune) int {
if a.Message < b.Message {
return -1
} else if a.Message > b.Message {
return 1
}

return 0
})

return request.Respond().Header("Date", time.Now().Format(time.RFC1123)).Header("Server", "indigo").ContentType(mime.HTML + "; charset=UTF-8").String(templates.HTMLFortunes(fortunes))
}

func (app *App) HandlePlaintext(request *http.Request) *http.Response {
return request.Respond().Header("Date", time.Now().Format(time.RFC1123)).Header("Server", "indigo").ContentType(mime.Plain).String("Hello, World!")
}

func normalizeNumber(nString string, found bool) int {
if !found {
nString = "0"
}

n, err := strconv.Atoi(nString)
if err != nil {
n = 0
}

if n < 1 {
n = 1
} else if n > 500 {
n = 500
}

return n
}
Loading