server implements http.Handler (ServeHTTP(responseWrite, request)) | apirequest.BuildFromHTTPRequest() becomes apirequest.FromHTTP() | update readme

This commit is contained in:
Adrien Marquès 2018-07-11 19:02:33 +02:00
parent eeec067e15
commit 5aef3f5572
3 changed files with 26 additions and 24 deletions

View File

@ -37,7 +37,7 @@ You need a recent machine with `go` installed.
##### 1. Download and install the package
```bash
$ go get -u git.xdrm.io/go/aicra
go get -u git.xdrm.io/go/aicra
```
It should now be available locally and available for your imports.
@ -49,7 +49,7 @@ It should now be available locally and available for your imports.
You should then compile the project builder to help you manage your projects.
```bash
$ go install git.xdrm.io/go/aicra/cmd/aicra
go install git.xdrm.io/go/aicra/cmd/aicra
```
@ -75,7 +75,7 @@ The default project structure for **aicra** is as follows :
```
In order for your project to be run, each controller, middleware and type checker has to be compiled as a *plugin* (*i.e. shared objects*). They can then be loaded by the server.
In order for your project to be run, each controller, middleware and type checker has to be compiled as a *plugin* (*i.e. shared objects*). They can then be loaded by the server.
@ -142,7 +142,7 @@ Options:
For a project that does not need a different structure, you just have to run this command under your project root
```bash
$ aicra .
aicra .
```
The output should look like
@ -154,13 +154,27 @@ The output should look like
The main default program is pretty small as below :
```go
import "git.xdrm.io/go/aicra"
package main
import (
"git.xdrm.io/go/aicra"
"net/http"
)
func main() {
server, err := aicra.New("manifest.json")
if err != nil { return }
server.Listen(4242)
// 1. create server
server, err := aicra.New("manifest.json")
if err != nil {
panic(err)
}
// 2. listen to incoming http requests
err = http.ListenAndServe("127.0.0.1:4242", server)
if err != nil {
panic(err)
}
}
```

View File

@ -12,8 +12,8 @@ import (
"time"
)
// BuildFromHTTPRequest builds an interface request from a http.Request
func BuildFromHTTPRequest(req *http.Request) (*Request, error) {
// FromHTTP builds an interface request from a http.Request
func FromHTTP(req *http.Request) (*Request, error) {
/* (1) Get useful data */
uri := normaliseURI(req.URL.Path)

View File

@ -1,7 +1,6 @@
package aicra
import (
"fmt"
e "git.xdrm.io/go/aicra/err"
"git.xdrm.io/go/aicra/internal/apirequest"
"git.xdrm.io/go/aicra/internal/checker"
@ -47,22 +46,11 @@ func New(path string) (*Server, error) {
}
// Listen binds the server to the given port
func (s *Server) Listen(port uint16) error {
/* (1) Bind router */
http.HandleFunc("/", s.manageRequest)
/* (2) Bind listener */
return http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
}
// Router called for each request
func (s *Server) manageRequest(res http.ResponseWriter, req *http.Request) {
// ServeHTTP implements http.Handler and has to be called on each request
func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) {
/* (1) Build request */
apiRequest, err := apirequest.BuildFromHTTPRequest(req)
apiRequest, err := apirequest.FromHTTP(req)
if err != nil {
log.Fatal(err)
}