webfmwk
is a go web API framework build on top of several packages :
valya/fasthttp
instead ofnet/http
fasthttp/router
to route API endpointsgorilla/schema
,go-playground/validator/v10
,go-playground/local
andgo-playground/local
for structure validation and error translationsegmito/encoding
instead ofencoding/json
Import github.com/burgesQ/webfmwk/v6
.
Go 1.19
is required.
go sample
package main
import (
"net/http"
"github.com/burgesQ/webfmwk/v6"
)
// curl -X GET 127.0.0.1:4242/hello
// { "message": "hello world" }
func main() {
var s = webfmwk.InitServer()
s.GET("/hello", func(c webfmwk.Context) error {
c.JSONBlob(http.StatusOK, []byte(`{ "message": "hello world" }`))
})
// ctrl+c is handled internaly
defer s.WaitAndStop()
s.Start(":4242")
}
Reach the endpoint:
curl sample
$ curl -i 'http://localhost:4242/hello'
HTTP/1.1 200 OK
Accept: application/json; charset=UTF-8
Content-Type: application/json; charset=UTF-8
Produce: application/json; charset=UTF-8
Date: Mon, 18 May 2020 07:45:31 GMT
Content-Length: 25
{"message":"hello world"}%
Some samples are in the ./doc
directory. The main (doc.go
) hande the samples
orchestration. Use go run . [sample file name]
to run the example file.
see more sample
$ cd doc
$ go run . panic_to_error
. panic_to_error
running panic_to_error (use panic to handle some error case)
- DBG : -- crtl-c support enabled
- DBG : -- handlers loaded
- DBG : exit handler: starting
- DBG : http server :4242: starting
- DBG : [+] server 1 (:4242)
- DBG : [+] new connection
+ INFO : [+] (f2124b89-414b-4361-96ec-5f227c0e3369) : [GET]/panic
+ INFO : [-] (f2124b89-414b-4361-96ec-5f227c0e3369) : [422](27)
- DBG : [-] (f2124b89-414b-4361-96ec-5f227c0e3369) : >{"error":"user not logged"}<
what | filename |
---|---|
return hello world | hello_world.go |
fetch value from url | url_param.go |
fetch query param value | query_param.go |
post content handling | post_content.go |
register mutliple endpoints | routes.go |
overload the framework context | custom_context.go |
register extra hanlders / middleware | handlers.go |
generate and expose a swagger doc | swagger.go |
start the server in https | tls.go |
attach worker to the server pool | custom_worker.go |
add an ID per requrest (ease logging for ex) | request_id.go |
panic to return an http error | panic_to_error.go |
Simply run make
, lint is also available via make lint
First of all, thank you for contributing hearts
If you find any typo/misconfiguration/... please send me a PR or open an issue.
Also, while creating your Pull Request on GitHub, please write a description which gives the context and/or explains why you are creating it.
Frafos GmbH 🎉 where I've writted most of that code