created initialiser + request builder (which supports form-data, urlencoded, json)

This commit is contained in:
Adrien Marquès 2018-05-21 12:02:24 +02:00
parent 60c0c92f3a
commit 7301f2065c
6 changed files with 216 additions and 19 deletions

View File

@ -13,35 +13,35 @@ type Err struct {
var ( var (
/* Base */ /* Base */
ErrSuccess = &Err{0, "all right", nil} ErrSuccess = Err{0, "all right", nil}
ErrFailure = &Err{1, "it failed", nil} ErrFailure = Err{1, "it failed", nil}
ErrUnknown = &Err{-1, "", nil} ErrUnknown = Err{-1, "", nil}
ErrNoMatchFound = &Err{2, "no resource found", nil} ErrNoMatchFound = Err{2, "no resource found", nil}
ErrAlreadyExists = &Err{3, "resource already exists", nil} ErrAlreadyExists = Err{3, "resource already exists", nil}
ErrConfig = &Err{4, "configuration error", nil} ErrConfig = Err{4, "configuration error", nil}
/* I/O */ /* I/O */
ErrUpload = &Err{100, "upload failed", nil} ErrUpload = Err{100, "upload failed", nil}
ErrDownload = &Err{101, "download failed", nil} ErrDownload = Err{101, "download failed", nil}
ErrMissingDownloadHeaders = &Err{102, "download headers are missing", nil} ErrMissingDownloadHeaders = Err{102, "download headers are missing", nil}
ErrMissingDownloadBody = &Err{103, "download body is missing", nil} ErrMissingDownloadBody = Err{103, "download body is missing", nil}
/* Controllers */ /* Controllers */
ErrUnknownController = &Err{200, "unknown controller", nil} ErrUnknownController = Err{200, "unknown controller", nil}
ErrUnknownMethod = &Err{201, "unknown method", nil} ErrUnknownMethod = Err{201, "unknown method", nil}
ErrUncallableController = &Err{202, "uncallable controller", nil} ErrUncallableController = Err{202, "uncallable controller", nil}
ErrUncallableMethod = &Err{203, "uncallable method", nil} ErrUncallableMethod = Err{203, "uncallable method", nil}
/* Permissions */ /* Permissions */
ErrPermission = &Err{300, "permission error", nil} ErrPermission = Err{300, "permission error", nil}
ErrToken = &Err{301, "token error", nil} ErrToken = Err{301, "token error", nil}
/* Check */ /* Check */
ErrMissingParam = &Err{400, "missing parameter", nil} ErrMissingParam = Err{400, "missing parameter", nil}
ErrInvalidParam = &Err{401, "invalid parameter", nil} ErrInvalidParam = Err{401, "invalid parameter", nil}
ErrInvalidDefaultParam = &Err{402, "invalid default param", nil} ErrInvalidDefaultParam = Err{402, "invalid default param", nil}
) )
// BindArgument adds an argument to the error // BindArgument adds an argument to the error

23
loader.go Normal file
View File

@ -0,0 +1,23 @@
package gfw
import "git.xdrm.io/gfw/internal/config"
// Init initilises a new framework instance
func Init(path string) (*Server, error) {
/* (1) Init instance */
inst := &Server{
config: nil,
Params: make(map[string]interface{}),
err: ErrSuccess,
}
/* (2) Load configuration */
config, err := config.Load(path)
if err != nil {
return nil, err
}
inst.config = config
return inst, nil
}

108
request_builder.go Normal file
View File

@ -0,0 +1,108 @@
package gfw
import (
"encoding/json"
"log"
"math"
"net/http"
"strings"
)
func buildRequest(req *http.Request) (*Request, error) {
/* (1) Init request */
uri := NormaliseUri(req.URL.Path)
inst := &Request{
Uri: strings.Split(uri, "/"),
GetData: FetchGetData(req),
FormData: FetchFormData(req),
}
return inst, nil
}
// NormaliseUri removes the trailing '/' to always
// have the same Uri format for later processing
func NormaliseUri(uri string) string {
if len(uri) < 1 {
return uri
}
if uri[0] == '/' {
uri = uri[1:]
}
if uri[len(uri)-1] == '/' {
uri = uri[0 : len(uri)-1]
}
return uri
}
// FetchGetData extracts the GET data
// from an HTTP request
func FetchGetData(req *http.Request) map[string]interface{} {
res := make(map[string]interface{})
for name, value := range req.URL.Query() {
res[name] = value
}
return res
}
// FetchFormData extracts FORM data
//
// - parse 'form-data' if not supported (not POST requests)
// - parse 'x-www-form-urlencoded'
// - parse 'application/json'
func FetchFormData(req *http.Request) map[string]interface{} {
res := make(map[string]interface{})
ct := req.Header.Get("Content-Type")
if strings.HasPrefix(ct, "application/json") {
receiver := make(map[string]interface{}, 0)
// 1. Init JSON reader
decoder := json.NewDecoder(req.Body)
if err := decoder.Decode(&receiver); err != nil {
log.Printf("[parse.json] %s\n", err)
return res
}
// 2. Return result
return receiver
} else if strings.HasPrefix(ct, "application/x-www-form-urlencoded") {
// 1. Parse url encoded data
req.ParseForm()
// 2. Extract values
for name, value := range req.PostForm {
res[name] = value
}
} else { // form-data or anything
// 1. Parse form-data
if err := req.ParseMultipartForm(math.MaxInt32); err != nil {
log.Printf("[read.multipart] %s\n", err)
return res
}
// 2. Extract values
for name, value := range req.PostForm {
res[name] = value
}
}
return res
}

32
router.go Normal file
View File

@ -0,0 +1,32 @@
package gfw
import (
"fmt"
"log"
"net/http"
)
func route(res http.ResponseWriter, req *http.Request) {
/* (1) Build request
---------------------------------------------------------*/
/* (1) Try to build request */
request, err := buildRequest(req)
if err != nil {
log.Fatal(req)
}
fmt.Printf("Uri: %v\n", request.Uri)
fmt.Printf("GET: %v\n", request.GetData)
fmt.Printf("POST: %v\n", request.FormData)
// 1. Query parameters
// fmt.Printf("query: %v\n", req.URL.Query())
// 2. URI path
// fmt.Printf("uri: %v\n", req.URL.Path)
// 3. Form values
// fmt.Printf("form: %v\n", req.FormValue("asa"))
}

17
server.go Normal file
View File

@ -0,0 +1,17 @@
package gfw
import (
"fmt"
"net/http"
)
// Launch listens and binds the server to the given port
func (s *Server) Launch(port uint16) error {
/* (1) Bind router */
http.HandleFunc("/", route)
/* (2) Bind listener */
return http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
}

17
types.go Normal file
View File

@ -0,0 +1,17 @@
package gfw
import (
"git.xdrm.io/gfw/internal/config"
)
type Server struct {
config *config.Controller
Params map[string]interface{}
err Err
}
type Request struct {
Uri []string
FormData map[string]interface{}
GetData map[string]interface{}
}