Renamed module

This commit is contained in:
2024-03-22 15:08:24 +01:00
parent f65c34ea78
commit 5fdb9a883d
16 changed files with 80 additions and 80 deletions

View File

@ -3,47 +3,47 @@ package content
import (
"encoding/json"
"git.akyoto.dev/go/server"
"git.akyoto.dev/go/web"
)
// CSS sends the body with the content type set to `text/css`.
func CSS(ctx server.Context, body string) error {
func CSS(ctx web.Context, body string) error {
ctx.Response().SetHeader("Content-Type", "text/css")
return ctx.String(body)
}
// CSV sends the body with the content type set to `text/csv`.
func CSV(ctx server.Context, body string) error {
func CSV(ctx web.Context, body string) error {
ctx.Response().SetHeader("Content-Type", "text/csv")
return ctx.String(body)
}
// HTML sends the body with the content type set to `text/html`.
func HTML(ctx server.Context, body string) error {
func HTML(ctx web.Context, body string) error {
ctx.Response().SetHeader("Content-Type", "text/html")
return ctx.String(body)
}
// JS sends the body with the content type set to `text/javascript`.
func JS(ctx server.Context, body string) error {
func JS(ctx web.Context, body string) error {
ctx.Response().SetHeader("Content-Type", "text/javascript")
return ctx.String(body)
}
// JSON encodes the object in JSON format and sends it with the content type set to `application/json`.
func JSON(ctx server.Context, object any) error {
func JSON(ctx web.Context, object any) error {
ctx.Response().SetHeader("Content-Type", "application/json")
return json.NewEncoder(ctx.Response()).Encode(object)
}
// Text sends the body with the content type set to `text/plain`.
func Text(ctx server.Context, body string) error {
func Text(ctx web.Context, body string) error {
ctx.Response().SetHeader("Content-Type", "text/plain")
return ctx.String(body)
}
// XML sends the body with the content type set to `text/xml`.
func XML(ctx server.Context, body string) error {
func XML(ctx web.Context, body string) error {
ctx.Response().SetHeader("Content-Type", "text/xml")
return ctx.String(body)
}

View File

@ -8,38 +8,38 @@ import (
"testing"
"git.akyoto.dev/go/assert"
"git.akyoto.dev/go/server"
"git.akyoto.dev/go/server/content"
"git.akyoto.dev/go/web"
"git.akyoto.dev/go/web/content"
)
func TestContentTypes(t *testing.T) {
s := server.New()
s := web.NewServer()
s.Get("/css", func(ctx server.Context) error {
s.Get("/css", func(ctx web.Context) error {
return content.CSS(ctx, "body{}")
})
s.Get("/csv", func(ctx server.Context) error {
s.Get("/csv", func(ctx web.Context) error {
return content.CSV(ctx, "ID;Name\n")
})
s.Get("/html", func(ctx server.Context) error {
s.Get("/html", func(ctx web.Context) error {
return content.HTML(ctx, "<html></html>")
})
s.Get("/js", func(ctx server.Context) error {
s.Get("/js", func(ctx web.Context) error {
return content.JS(ctx, "console.log(42)")
})
s.Get("/json", func(ctx server.Context) error {
s.Get("/json", func(ctx web.Context) error {
return content.JSON(ctx, struct{ Name string }{Name: "User 1"})
})
s.Get("/text", func(ctx server.Context) error {
s.Get("/text", func(ctx web.Context) error {
return content.Text(ctx, "Hello")
})
s.Get("/xml", func(ctx server.Context) error {
s.Get("/xml", func(ctx web.Context) error {
return content.XML(ctx, "<xml></xml>")
})