From 5fdb9a883daec50cf1ce96841ea32515029f9ad5 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Fri, 22 Mar 2024 15:08:24 +0100 Subject: [PATCH] Renamed module --- Benchmarks_test.go | 12 ++++----- Configuration.go | 2 +- Context.go | 2 +- Handler.go | 2 +- README.md | 14 +++++----- Request.go | 2 +- Response.go | 2 +- Server.go | 6 ++--- Server_test.go | 60 ++++++++++++++++++++--------------------- common_test.go | 2 +- content/content.go | 16 +++++------ content/content_test.go | 20 +++++++------- examples/hello/main.go | 6 ++--- examples/logger/main.go | 6 ++--- examples/stream/main.go | 6 ++--- go.mod | 2 +- 16 files changed, 80 insertions(+), 80 deletions(-) diff --git a/Benchmarks_test.go b/Benchmarks_test.go index 46dc52b..3c5e2c4 100644 --- a/Benchmarks_test.go +++ b/Benchmarks_test.go @@ -1,4 +1,4 @@ -package server_test +package web_test import ( "net/http/httptest" @@ -6,7 +6,7 @@ import ( "testing" "git.akyoto.dev/go/router/testdata" - "git.akyoto.dev/go/server" + "git.akyoto.dev/go/web" ) func BenchmarkStatic(b *testing.B) { @@ -16,10 +16,10 @@ func BenchmarkStatic(b *testing.B) { "/hello/world", } - s := server.New() + s := web.NewServer() for _, path := range paths { - s.Get(path, func(ctx server.Context) error { + s.Get(path, func(ctx web.Context) error { return ctx.String("Hello") }) } @@ -42,10 +42,10 @@ func BenchmarkGitHub(b *testing.B) { "/repos/:a/:b", } - s := server.New() + s := web.NewServer() for _, route := range testdata.Routes("testdata/github.txt") { - s.Router().Add(route.Method, route.Path, func(ctx server.Context) error { + s.Router().Add(route.Method, route.Path, func(ctx web.Context) error { return ctx.String("Hello") }) } diff --git a/Configuration.go b/Configuration.go index b9596c7..79e9374 100644 --- a/Configuration.go +++ b/Configuration.go @@ -1,4 +1,4 @@ -package server +package web import "time" diff --git a/Context.go b/Context.go index c87e5cc..d58bf62 100644 --- a/Context.go +++ b/Context.go @@ -1,4 +1,4 @@ -package server +package web import ( "errors" diff --git a/Handler.go b/Handler.go index 4030ee7..96b6cb6 100644 --- a/Handler.go +++ b/Handler.go @@ -1,4 +1,4 @@ -package server +package web // Handler is a function that deals with the given request/response context. type Handler func(Context) error diff --git a/README.md b/README.md index 5c57576..eaec92a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# server +# web -HTTP server. +Web server. ## Features @@ -11,26 +11,26 @@ HTTP server. ## Installation ```shell -go get git.akyoto.dev/go/server +go get git.akyoto.dev/go/web ``` ## Usage ```go -s := server.New() +s := web.NewServer() // Static route -s.Get("/", func(ctx server.Context) error { +s.Get("/", func(ctx web.Context) error { return ctx.String("Hello") }) // Parameter route -s.Get("/blog/:post", func(ctx server.Context) error { +s.Get("/blog/:post", func(ctx web.Context) error { return ctx.String(ctx.Get("post")) }) // Wildcard route -s.Get("/images/*file", func(ctx server.Context) error { +s.Get("/images/*file", func(ctx web.Context) error { return ctx.String(ctx.Get("file")) }) diff --git a/Request.go b/Request.go index ecce4f8..9715449 100644 --- a/Request.go +++ b/Request.go @@ -1,4 +1,4 @@ -package server +package web import ( "context" diff --git a/Response.go b/Response.go index fbdd14f..519e56d 100644 --- a/Response.go +++ b/Response.go @@ -1,4 +1,4 @@ -package server +package web import ( "io" diff --git a/Server.go b/Server.go index 8f8949a..d0e76da 100644 --- a/Server.go +++ b/Server.go @@ -1,4 +1,4 @@ -package server +package web import ( "context" @@ -34,8 +34,8 @@ type server struct { config Configuration } -// New creates a new HTTP server. -func New() Server { +// NewServer creates a new HTTP server. +func NewServer() Server { s := &server{ router: router.Router[Handler]{}, config: defaultConfig(), diff --git a/Server_test.go b/Server_test.go index 9950993..a6bbe3b 100644 --- a/Server_test.go +++ b/Server_test.go @@ -1,4 +1,4 @@ -package server_test +package web_test import ( "errors" @@ -12,66 +12,66 @@ import ( "testing" "git.akyoto.dev/go/assert" - "git.akyoto.dev/go/server" + "git.akyoto.dev/go/web" ) func TestRouter(t *testing.T) { - s := server.New() + s := web.NewServer() - s.Get("/", func(ctx server.Context) error { + s.Get("/", func(ctx web.Context) error { return ctx.Bytes([]byte("Hello")) }) - s.Get("/string", func(ctx server.Context) error { + s.Get("/string", func(ctx web.Context) error { return ctx.String("Hello") }) - s.Get("/write", func(ctx server.Context) error { + s.Get("/write", func(ctx web.Context) error { _, err := ctx.Response().Write([]byte("Hello")) return err }) - s.Get("/writestring", func(ctx server.Context) error { + s.Get("/writestring", func(ctx web.Context) error { _, err := io.WriteString(ctx.Response(), "Hello") return err }) - s.Get("/error", func(ctx server.Context) error { + s.Get("/error", func(ctx web.Context) error { return ctx.Status(http.StatusUnauthorized).Error("Not logged in") }) - s.Get("/error2", func(ctx server.Context) error { + s.Get("/error2", func(ctx web.Context) error { return ctx.Status(http.StatusUnauthorized).Error("Not logged in", errors.New("Missing auth token")) }) - s.Get("/reader", func(ctx server.Context) error { + s.Get("/reader", func(ctx web.Context) error { return ctx.Copy(strings.NewReader("Hello")) }) - s.Get("/file", func(ctx server.Context) error { + s.Get("/file", func(ctx web.Context) error { return ctx.File("testdata/file.txt") }) - s.Get("/flush", func(ctx server.Context) error { + s.Get("/flush", func(ctx web.Context) error { ctx.Response().WriteString("Hello 1\n") ctx.Response().WriteString("Hello 2\n") ctx.Response().Flush() return nil }) - s.Get("/echo", func(ctx server.Context) error { + s.Get("/echo", func(ctx web.Context) error { return ctx.Copy(ctx.Request()) }) - s.Get("/context", func(ctx server.Context) error { + s.Get("/context", func(ctx web.Context) error { return ctx.Request().Context().Err() }) - s.Get("/redirect", func(ctx server.Context) error { + s.Get("/redirect", func(ctx web.Context) error { return ctx.Redirect(http.StatusTemporaryRedirect, "/") }) - s.Get("/request/data", func(ctx server.Context) error { + s.Get("/request/data", func(ctx web.Context) error { request := ctx.Request() method := request.Method() protocol := request.Protocol() @@ -80,40 +80,40 @@ func TestRouter(t *testing.T) { return ctx.String(fmt.Sprintf("%s %s %s %s", method, protocol, host, path)) }) - s.Get("/request/header", func(ctx server.Context) error { + s.Get("/request/header", func(ctx web.Context) error { acceptEncoding := ctx.Request().Header("Accept-Encoding") return ctx.String(acceptEncoding) }) - s.Get("/response/header", func(ctx server.Context) error { + s.Get("/response/header", func(ctx web.Context) error { ctx.Response().SetHeader("Content-Type", "text/plain") contentType := ctx.Response().Header("Content-Type") return ctx.String(contentType) }) - s.Get("/blog/:article", func(ctx server.Context) error { + s.Get("/blog/:article", func(ctx web.Context) error { article := ctx.Get("article") return ctx.String(article) }) - s.Get("/missing-parameter", func(ctx server.Context) error { + s.Get("/missing-parameter", func(ctx web.Context) error { missing := ctx.Get("missing") return ctx.String(missing) }) - s.Get("/scheme", func(ctx server.Context) error { + s.Get("/scheme", func(ctx web.Context) error { return ctx.String(ctx.Request().Scheme()) }) - s.Post("/", func(ctx server.Context) error { + s.Post("/", func(ctx web.Context) error { return ctx.String("Post") }) - s.Delete("/", func(ctx server.Context) error { + s.Delete("/", func(ctx web.Context) error { return ctx.String("Delete") }) - s.Put("/", func(ctx server.Context) error { + s.Put("/", func(ctx web.Context) error { return ctx.String("Put") }) @@ -165,9 +165,9 @@ func TestRouter(t *testing.T) { } func TestMiddleware(t *testing.T) { - s := server.New() + s := web.NewServer() - s.Use(func(ctx server.Context) error { + s.Use(func(ctx web.Context) error { ctx.Response().SetHeader("Middleware", "true") return ctx.Next() }) @@ -180,9 +180,9 @@ func TestMiddleware(t *testing.T) { } func TestPanic(t *testing.T) { - s := server.New() + s := web.NewServer() - s.Router().Add(http.MethodGet, "/panic", func(ctx server.Context) error { + s.Router().Add(http.MethodGet, "/panic", func(ctx web.Context) error { panic("Something unbelievable happened") }) @@ -202,7 +202,7 @@ func TestPanic(t *testing.T) { } func TestRun(t *testing.T) { - s := server.New() + s := web.NewServer() go func() { _, err := http.Get("http://127.0.0.1:8080/") @@ -219,6 +219,6 @@ func TestUnavailablePort(t *testing.T) { assert.Nil(t, err) defer listener.Close() - s := server.New() + s := web.NewServer() s.Run(":8080") } diff --git a/common_test.go b/common_test.go index 39514ca..25f06f3 100644 --- a/common_test.go +++ b/common_test.go @@ -1,4 +1,4 @@ -package server_test +package web_test import "net/http" diff --git a/content/content.go b/content/content.go index 2a43b2e..2eca461 100644 --- a/content/content.go +++ b/content/content.go @@ -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) } diff --git a/content/content_test.go b/content/content_test.go index 5638f7c..901eaeb 100644 --- a/content/content_test.go +++ b/content/content_test.go @@ -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, "") }) - 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, "") }) diff --git a/examples/hello/main.go b/examples/hello/main.go index 215c77d..c9853da 100644 --- a/examples/hello/main.go +++ b/examples/hello/main.go @@ -1,13 +1,13 @@ package main import ( - "git.akyoto.dev/go/server" + "git.akyoto.dev/go/web" ) func main() { - s := server.New() + s := web.NewServer() - s.Get("/", func(ctx server.Context) error { + s.Get("/", func(ctx web.Context) error { return ctx.String("Hello") }) diff --git a/examples/logger/main.go b/examples/logger/main.go index a35a4a1..7ccea7d 100644 --- a/examples/logger/main.go +++ b/examples/logger/main.go @@ -4,13 +4,13 @@ import ( "fmt" "time" - "git.akyoto.dev/go/server" + "git.akyoto.dev/go/web" ) func main() { - s := server.New() + s := web.NewServer() - s.Use(func(ctx server.Context) error { + s.Use(func(ctx web.Context) error { start := time.Now() defer func() { diff --git a/examples/stream/main.go b/examples/stream/main.go index 4d43d74..3c2f490 100644 --- a/examples/stream/main.go +++ b/examples/stream/main.go @@ -3,13 +3,13 @@ package main import ( "time" - "git.akyoto.dev/go/server" + "git.akyoto.dev/go/web" ) func main() { - s := server.New() + s := web.NewServer() - s.Get("/", func(ctx server.Context) error { + s.Get("/", func(ctx web.Context) error { ticker := time.NewTicker(time.Second) for { diff --git a/go.mod b/go.mod index 1473267..a4d4a7c 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module git.akyoto.dev/go/server +module git.akyoto.dev/go/web go 1.22