From 5aef3f5572c5d7cf2d1ca2390541c768b64e9785 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 11 Jul 2018 19:02:33 +0200 Subject: [PATCH] server implements http.Handler (ServeHTTP(responseWrite, request)) | apirequest.BuildFromHTTPRequest() becomes apirequest.FromHTTP() | update readme --- README.md | 28 +++++++++++++++++++++------- internal/apirequest/request.go | 4 ++-- server.go | 18 +++--------------- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index b73708e..cee7f59 100644 --- a/README.md +++ b/README.md @@ -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() { + + // 1. create server server, err := aicra.New("manifest.json") - if err != nil { return } + 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) + } - server.Listen(4242) } ``` diff --git a/internal/apirequest/request.go b/internal/apirequest/request.go index 69beb27..3bf4426 100644 --- a/internal/apirequest/request.go +++ b/internal/apirequest/request.go @@ -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) diff --git a/server.go b/server.go index bcd6ebc..eaf009a 100644 --- a/server.go +++ b/server.go @@ -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) }