Skip to content

Commit 34ca8b1

Browse files
committed
Add indigo
1 parent f1e5b50 commit 34ca8b1

File tree

15 files changed

+2060
-4
lines changed

15 files changed

+2060
-4
lines changed

frameworks/Go/fiber/src/server_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package main
22

33
import (
4-
"encoding/json"
5-
"io/ioutil"
4+
"io"
65
"net/http"
76
"net/http/httptest"
87
"testing"
@@ -37,7 +36,7 @@ func Benchmark_Plaintext(b *testing.B) {
3736
utils.AssertEqual(b, nil, err, "app.Test(req)")
3837
utils.AssertEqual(b, 200, resp.StatusCode, "Status code")
3938
utils.AssertEqual(b, fiber.MIMETextPlainCharsetUTF8, resp.Header.Get("Content-Type"))
40-
body, _ := ioutil.ReadAll(resp.Body)
39+
body, _ := io.ReadAll(resp.Body)
4140
utils.AssertEqual(b, helloworldRaw, body)
4241
}
4342

@@ -66,6 +65,6 @@ func Benchmark_JSON(b *testing.B) {
6665
utils.AssertEqual(b, nil, err, "app.Test(req)")
6766
utils.AssertEqual(b, 200, resp.StatusCode, "Status code")
6867
utils.AssertEqual(b, fiber.MIMEApplicationJSON, resp.Header.Get("Content-Type"))
69-
body, _ := ioutil.ReadAll(resp.Body)
68+
body, _ := io.ReadAll(resp.Body)
7069
utils.AssertEqual(b, `{"message":"Hello, World!"}`, string(body))
7170
}

frameworks/Go/indigo/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# [Indigo](https://github.com/indigo-web/indigo) (Go) Benchmarking Test
2+
3+
This is the go portion of a [benchmarking test suite](https://www.techempower.com/benchmarks/) comparing a variety of web development platforms.
4+
5+
> Indigo is a web-framework focusing at performance, elegancy and robustness.
6+
7+
## Test URLs
8+
* http://localhost:8080/json
9+
* http://localhost:8080/db
10+
* http://localhost:8080/queries?queries=[1-500]
11+
* http://localhost:8080/cached-worlds?count=[1-500]
12+
* http://localhost:8080/fortune
13+
* http://localhost:8080/update?queries=[1-500]
14+
* http://localhost:8080/plaintext
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"framework": "indigo",
3+
"tests": [
4+
{
5+
"default": {
6+
"json_url": "/json",
7+
"db_url": "/db",
8+
"query_url": "/queries?n=",
9+
"update_url": "/update?n=",
10+
"cached_query_url": "/cached-worlds?n=",
11+
"fortune_url": "/fortunes",
12+
"plaintext_url": "/plaintext",
13+
"port": 8080,
14+
"approach": "Realistic",
15+
"classification": "Platform",
16+
"database": "Postgres",
17+
"framework": "indigo",
18+
"language": "Go",
19+
"flavor": "None",
20+
"orm": "Raw",
21+
"platform": "None",
22+
"webserver": "None",
23+
"os": "Linux",
24+
"database_os": "Linux",
25+
"display_name": "Indigo",
26+
"notes": "",
27+
"versus": "go"
28+
}
29+
}
30+
]
31+
}

frameworks/Go/indigo/config.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[framework]
2+
name = "indigo"
3+
4+
[main]
5+
urls.json = "/json"
6+
urls.db = "/db"
7+
urls.query = "/queries?n="
8+
urls.update = "/updates?n="
9+
urls.cached_query = "/cached-worlds?q="
10+
urls.fortune = "/fortunes"
11+
urls.plaintext = "/plaintext"
12+
approach = "Realistic"
13+
classification = "Platform"
14+
database = "Postgres"
15+
database_os = "Linux"
16+
os = "Linux"
17+
orm = "Raw"
18+
platform = "None"
19+
webserver = "None"
20+
versus = "go"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM golang:1.24.4-alpine3.22 as builder
2+
3+
WORKDIR /indigo
4+
5+
COPY ./src /indigo
6+
7+
RUN go mod download && \
8+
go generate -x ./templates && \
9+
GOAMD64=v3 go build -ldflags="-s -w" -o app .
10+
11+
FROM alpine:3.22
12+
13+
WORKDIR /indigo
14+
15+
COPY --from=builder /indigo/app .
16+
17+
EXPOSE 8080
18+
19+
CMD ./app

frameworks/Go/indigo/src/app.go

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"math/rand/v2"
7+
"slices"
8+
"strconv"
9+
10+
"github.com/indigo-web/indigo/http"
11+
"github.com/indigo-web/indigo/http/mime"
12+
13+
"indigo/app/models"
14+
"indigo/app/templates"
15+
)
16+
17+
type (
18+
App struct {
19+
DB *DB
20+
Cache models.Worlds
21+
}
22+
)
23+
24+
func NewApp(DB *DB) *App {
25+
return &App{DB: DB}
26+
}
27+
28+
func (app *App) PopulateCache(ctx context.Context) error {
29+
cache := make(models.Worlds, 10000)
30+
31+
err := app.DB.FillWorlds(context.Background(), cache)
32+
if err != nil {
33+
return err
34+
}
35+
36+
app.Cache = cache
37+
38+
return nil
39+
}
40+
41+
func (app *App) HandleJSON(request *http.Request) *http.Response {
42+
return request.Respond().JSON(&models.Message{
43+
Message: "Hello, World!",
44+
})
45+
}
46+
47+
func (app *App) HandleDB(request *http.Request) *http.Response {
48+
world := &models.World{
49+
ID: rand.IntN(10000) + 1,
50+
}
51+
52+
err := app.DB.FillWorldByID(context.Background(), world)
53+
if err != nil {
54+
return http.Error(request, err)
55+
}
56+
57+
return request.Respond().JSON(world)
58+
}
59+
60+
func (app *App) HandleQueries(request *http.Request) *http.Response {
61+
n, err := normalizeNumber(request.Params.Get("n"))
62+
if err != nil {
63+
return http.Error(request, err)
64+
}
65+
66+
i, worlds := 0, make(models.Worlds, n)
67+
68+
for i = range worlds {
69+
worlds[i].ID = rand.IntN(10000) + 1
70+
}
71+
72+
err = app.DB.FillWorldsByID(context.Background(), worlds)
73+
if err != nil {
74+
return http.Error(request, err)
75+
}
76+
77+
return request.Respond().JSON(&worlds)
78+
}
79+
80+
func (app *App) HandleUpdate(request *http.Request) *http.Response {
81+
n, err := normalizeNumber(request.Params.Get("n"))
82+
if err != nil {
83+
return http.Error(request, err)
84+
}
85+
86+
i, worlds := 0, make(models.Worlds, n)
87+
88+
for i = range worlds {
89+
worlds[i].ID = rand.IntN(10000) + 1
90+
}
91+
92+
err = app.DB.FillWorldsByID(context.Background(), worlds)
93+
if err != nil {
94+
return http.Error(request, err)
95+
}
96+
97+
slices.SortFunc(worlds, func(a, b models.World) int {
98+
return a.ID - b.ID
99+
})
100+
101+
err = app.DB.UpdateWorlds(context.Background(), worlds)
102+
if err != nil {
103+
return http.Error(request, err)
104+
}
105+
106+
return request.Respond().JSON(&worlds)
107+
}
108+
109+
func (app *App) HandleCachedWorlds(request *http.Request) *http.Response {
110+
n, err := normalizeNumber(request.Params.Get("n"))
111+
if err != nil {
112+
return http.Error(request, err)
113+
}
114+
115+
i, worlds := 0, make(models.Worlds, n)
116+
117+
for i = range worlds {
118+
worlds[i] = app.Cache[rand.Int32N(10000)]
119+
}
120+
121+
return request.Respond().JSON(&worlds)
122+
}
123+
124+
func (app *App) HandleFortunes(request *http.Request) *http.Response {
125+
fortunes, err := app.DB.GetFortunes(context.Background())
126+
if err != nil {
127+
return http.Error(request, err)
128+
}
129+
130+
fortunes = append(fortunes, models.Fortune{
131+
Message: "Additional fortune added at request time.",
132+
})
133+
134+
slices.SortFunc(fortunes, func(a, b models.Fortune) int {
135+
if a.Message < b.Message {
136+
return -1
137+
} else if a.Message > b.Message {
138+
return 1
139+
}
140+
141+
return 0
142+
})
143+
144+
return request.Respond().ContentType(mime.HTML).String(templates.HTMLFortunes(fortunes))
145+
}
146+
147+
func (app *App) HandlePlaintext(request *http.Request) *http.Response {
148+
return request.Respond().String("Hello, World!")
149+
}
150+
151+
func normalizeNumber(nString string, found bool) (int, error) {
152+
if !found {
153+
return 0, fmt.Errorf("not found")
154+
}
155+
156+
n, err := strconv.Atoi(nString)
157+
if err != nil {
158+
return 0, err
159+
}
160+
161+
if n < 1 {
162+
n = 1
163+
} else if n > 500 {
164+
n = 500
165+
}
166+
167+
return n, nil
168+
}

0 commit comments

Comments
 (0)