Renamed module
This commit is contained in:
parent
17158235ea
commit
ef6dd6885e
@ -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")
|
||||
})
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package server
|
||||
package web
|
||||
|
||||
import "time"
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
package server
|
||||
package web
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
@ -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
|
||||
|
14
README.md
14
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"))
|
||||
})
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
package server
|
||||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
@ -1,4 +1,4 @@
|
||||
package server
|
||||
package web
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
@ -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(),
|
||||
|
@ -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")
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package server_test
|
||||
package web_test
|
||||
|
||||
import "net/http"
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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>")
|
||||
})
|
||||
|
||||
|
@ -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")
|
||||
})
|
||||
|
||||
|
@ -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() {
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user