Renamed module
This commit is contained in:
parent
f65c34ea78
commit
5fdb9a883d
@ -1,4 +1,4 @@
|
|||||||
package server_test
|
package web_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
@ -6,7 +6,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.akyoto.dev/go/router/testdata"
|
"git.akyoto.dev/go/router/testdata"
|
||||||
"git.akyoto.dev/go/server"
|
"git.akyoto.dev/go/web"
|
||||||
)
|
)
|
||||||
|
|
||||||
func BenchmarkStatic(b *testing.B) {
|
func BenchmarkStatic(b *testing.B) {
|
||||||
@ -16,10 +16,10 @@ func BenchmarkStatic(b *testing.B) {
|
|||||||
"/hello/world",
|
"/hello/world",
|
||||||
}
|
}
|
||||||
|
|
||||||
s := server.New()
|
s := web.NewServer()
|
||||||
|
|
||||||
for _, path := range paths {
|
for _, path := range paths {
|
||||||
s.Get(path, func(ctx server.Context) error {
|
s.Get(path, func(ctx web.Context) error {
|
||||||
return ctx.String("Hello")
|
return ctx.String("Hello")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -42,10 +42,10 @@ func BenchmarkGitHub(b *testing.B) {
|
|||||||
"/repos/:a/:b",
|
"/repos/:a/:b",
|
||||||
}
|
}
|
||||||
|
|
||||||
s := server.New()
|
s := web.NewServer()
|
||||||
|
|
||||||
for _, route := range testdata.Routes("testdata/github.txt") {
|
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")
|
return ctx.String("Hello")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package server
|
package web
|
||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package server
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package server
|
package web
|
||||||
|
|
||||||
// Handler is a function that deals with the given request/response context.
|
// Handler is a function that deals with the given request/response context.
|
||||||
type Handler func(Context) error
|
type Handler func(Context) error
|
||||||
|
14
README.md
14
README.md
@ -1,6 +1,6 @@
|
|||||||
# server
|
# web
|
||||||
|
|
||||||
HTTP server.
|
Web server.
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
@ -11,26 +11,26 @@ HTTP server.
|
|||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
go get git.akyoto.dev/go/server
|
go get git.akyoto.dev/go/web
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```go
|
```go
|
||||||
s := server.New()
|
s := web.NewServer()
|
||||||
|
|
||||||
// Static route
|
// Static route
|
||||||
s.Get("/", func(ctx server.Context) error {
|
s.Get("/", func(ctx web.Context) error {
|
||||||
return ctx.String("Hello")
|
return ctx.String("Hello")
|
||||||
})
|
})
|
||||||
|
|
||||||
// Parameter route
|
// 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"))
|
return ctx.String(ctx.Get("post"))
|
||||||
})
|
})
|
||||||
|
|
||||||
// Wildcard route
|
// 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"))
|
return ctx.String(ctx.Get("file"))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package server
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package server
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package server
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@ -34,8 +34,8 @@ type server struct {
|
|||||||
config Configuration
|
config Configuration
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new HTTP server.
|
// NewServer creates a new HTTP server.
|
||||||
func New() Server {
|
func NewServer() Server {
|
||||||
s := &server{
|
s := &server{
|
||||||
router: router.Router[Handler]{},
|
router: router.Router[Handler]{},
|
||||||
config: defaultConfig(),
|
config: defaultConfig(),
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package server_test
|
package web_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@ -12,66 +12,66 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.akyoto.dev/go/assert"
|
"git.akyoto.dev/go/assert"
|
||||||
"git.akyoto.dev/go/server"
|
"git.akyoto.dev/go/web"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRouter(t *testing.T) {
|
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"))
|
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")
|
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"))
|
_, err := ctx.Response().Write([]byte("Hello"))
|
||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
|
|
||||||
s.Get("/writestring", func(ctx server.Context) error {
|
s.Get("/writestring", func(ctx web.Context) error {
|
||||||
_, err := io.WriteString(ctx.Response(), "Hello")
|
_, err := io.WriteString(ctx.Response(), "Hello")
|
||||||
return err
|
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")
|
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"))
|
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"))
|
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")
|
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 1\n")
|
||||||
ctx.Response().WriteString("Hello 2\n")
|
ctx.Response().WriteString("Hello 2\n")
|
||||||
ctx.Response().Flush()
|
ctx.Response().Flush()
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
s.Get("/echo", func(ctx server.Context) error {
|
s.Get("/echo", func(ctx web.Context) error {
|
||||||
return ctx.Copy(ctx.Request())
|
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()
|
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, "/")
|
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()
|
request := ctx.Request()
|
||||||
method := request.Method()
|
method := request.Method()
|
||||||
protocol := request.Protocol()
|
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))
|
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")
|
acceptEncoding := ctx.Request().Header("Accept-Encoding")
|
||||||
return ctx.String(acceptEncoding)
|
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")
|
ctx.Response().SetHeader("Content-Type", "text/plain")
|
||||||
contentType := ctx.Response().Header("Content-Type")
|
contentType := ctx.Response().Header("Content-Type")
|
||||||
return ctx.String(contentType)
|
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")
|
article := ctx.Get("article")
|
||||||
return ctx.String(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")
|
missing := ctx.Get("missing")
|
||||||
return ctx.String(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())
|
return ctx.String(ctx.Request().Scheme())
|
||||||
})
|
})
|
||||||
|
|
||||||
s.Post("/", func(ctx server.Context) error {
|
s.Post("/", func(ctx web.Context) error {
|
||||||
return ctx.String("Post")
|
return ctx.String("Post")
|
||||||
})
|
})
|
||||||
|
|
||||||
s.Delete("/", func(ctx server.Context) error {
|
s.Delete("/", func(ctx web.Context) error {
|
||||||
return ctx.String("Delete")
|
return ctx.String("Delete")
|
||||||
})
|
})
|
||||||
|
|
||||||
s.Put("/", func(ctx server.Context) error {
|
s.Put("/", func(ctx web.Context) error {
|
||||||
return ctx.String("Put")
|
return ctx.String("Put")
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -165,9 +165,9 @@ func TestRouter(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMiddleware(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")
|
ctx.Response().SetHeader("Middleware", "true")
|
||||||
return ctx.Next()
|
return ctx.Next()
|
||||||
})
|
})
|
||||||
@ -180,9 +180,9 @@ func TestMiddleware(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestPanic(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")
|
panic("Something unbelievable happened")
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -202,7 +202,7 @@ func TestPanic(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRun(t *testing.T) {
|
func TestRun(t *testing.T) {
|
||||||
s := server.New()
|
s := web.NewServer()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
_, err := http.Get("http://127.0.0.1:8080/")
|
_, err := http.Get("http://127.0.0.1:8080/")
|
||||||
@ -219,6 +219,6 @@ func TestUnavailablePort(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
defer listener.Close()
|
defer listener.Close()
|
||||||
|
|
||||||
s := server.New()
|
s := web.NewServer()
|
||||||
s.Run(":8080")
|
s.Run(":8080")
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package server_test
|
package web_test
|
||||||
|
|
||||||
import "net/http"
|
import "net/http"
|
||||||
|
|
||||||
|
@ -3,47 +3,47 @@ package content
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
"git.akyoto.dev/go/server"
|
"git.akyoto.dev/go/web"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CSS sends the body with the content type set to `text/css`.
|
// 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")
|
ctx.Response().SetHeader("Content-Type", "text/css")
|
||||||
return ctx.String(body)
|
return ctx.String(body)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CSV sends the body with the content type set to `text/csv`.
|
// 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")
|
ctx.Response().SetHeader("Content-Type", "text/csv")
|
||||||
return ctx.String(body)
|
return ctx.String(body)
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTML sends the body with the content type set to `text/html`.
|
// 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")
|
ctx.Response().SetHeader("Content-Type", "text/html")
|
||||||
return ctx.String(body)
|
return ctx.String(body)
|
||||||
}
|
}
|
||||||
|
|
||||||
// JS sends the body with the content type set to `text/javascript`.
|
// 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")
|
ctx.Response().SetHeader("Content-Type", "text/javascript")
|
||||||
return ctx.String(body)
|
return ctx.String(body)
|
||||||
}
|
}
|
||||||
|
|
||||||
// JSON encodes the object in JSON format and sends it with the content type set to `application/json`.
|
// 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")
|
ctx.Response().SetHeader("Content-Type", "application/json")
|
||||||
return json.NewEncoder(ctx.Response()).Encode(object)
|
return json.NewEncoder(ctx.Response()).Encode(object)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Text sends the body with the content type set to `text/plain`.
|
// 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")
|
ctx.Response().SetHeader("Content-Type", "text/plain")
|
||||||
return ctx.String(body)
|
return ctx.String(body)
|
||||||
}
|
}
|
||||||
|
|
||||||
// XML sends the body with the content type set to `text/xml`.
|
// 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")
|
ctx.Response().SetHeader("Content-Type", "text/xml")
|
||||||
return ctx.String(body)
|
return ctx.String(body)
|
||||||
}
|
}
|
||||||
|
@ -8,38 +8,38 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.akyoto.dev/go/assert"
|
"git.akyoto.dev/go/assert"
|
||||||
"git.akyoto.dev/go/server"
|
"git.akyoto.dev/go/web"
|
||||||
"git.akyoto.dev/go/server/content"
|
"git.akyoto.dev/go/web/content"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestContentTypes(t *testing.T) {
|
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{}")
|
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")
|
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>")
|
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)")
|
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"})
|
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")
|
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>")
|
return content.XML(ctx, "<xml></xml>")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.akyoto.dev/go/server"
|
"git.akyoto.dev/go/web"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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")
|
return ctx.String("Hello")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -4,13 +4,13 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.akyoto.dev/go/server"
|
"git.akyoto.dev/go/web"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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()
|
start := time.Now()
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
|
@ -3,13 +3,13 @@ package main
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.akyoto.dev/go/server"
|
"git.akyoto.dev/go/web"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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)
|
ticker := time.NewTicker(time.Second)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
Loading…
Reference in New Issue
Block a user