server implements http.Handler (ServeHTTP(responseWrite, request)) | apirequest.BuildFromHTTPRequest() becomes apirequest.FromHTTP() | update readme
This commit is contained in:
parent
eeec067e15
commit
5aef3f5572
30
README.md
30
README.md
|
@ -37,7 +37,7 @@ You need a recent machine with `go` installed.
|
||||||
##### 1. Download and install the package
|
##### 1. Download and install the package
|
||||||
|
|
||||||
```bash
|
```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.
|
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.
|
You should then compile the project builder to help you manage your projects.
|
||||||
|
|
||||||
```bash
|
```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
|
For a project that does not need a different structure, you just have to run this command under your project root
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ aicra .
|
aicra .
|
||||||
```
|
```
|
||||||
|
|
||||||
The output should look like
|
The output should look like
|
||||||
|
@ -154,13 +154,27 @@ The output should look like
|
||||||
The main default program is pretty small as below :
|
The main default program is pretty small as below :
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "git.xdrm.io/go/aicra"
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.xdrm.io/go/aicra"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -12,8 +12,8 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// BuildFromHTTPRequest builds an interface request from a http.Request
|
// FromHTTP builds an interface request from a http.Request
|
||||||
func BuildFromHTTPRequest(req *http.Request) (*Request, error) {
|
func FromHTTP(req *http.Request) (*Request, error) {
|
||||||
|
|
||||||
/* (1) Get useful data */
|
/* (1) Get useful data */
|
||||||
uri := normaliseURI(req.URL.Path)
|
uri := normaliseURI(req.URL.Path)
|
||||||
|
|
18
server.go
18
server.go
|
@ -1,7 +1,6 @@
|
||||||
package aicra
|
package aicra
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
e "git.xdrm.io/go/aicra/err"
|
e "git.xdrm.io/go/aicra/err"
|
||||||
"git.xdrm.io/go/aicra/internal/apirequest"
|
"git.xdrm.io/go/aicra/internal/apirequest"
|
||||||
"git.xdrm.io/go/aicra/internal/checker"
|
"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
|
// ServeHTTP implements http.Handler and has to be called on each request
|
||||||
func (s *Server) Listen(port uint16) error {
|
func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) {
|
||||||
|
|
||||||
/* (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) {
|
|
||||||
|
|
||||||
/* (1) Build request */
|
/* (1) Build request */
|
||||||
apiRequest, err := apirequest.BuildFromHTTPRequest(req)
|
apiRequest, err := apirequest.FromHTTP(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue