GoGym
GoGym
is a framework for building RESTful APIs, which is written in Golang
. It is inspired by an artisan framework Laravel.
Import Package
-
Install the package from the command line:
$ go get github.com/ZhenhangTung/GoGym
GoGym
How to use 1. Define your Own Controllers And Actions
type HelloController struct {
}
func (h *HelloController) SayHello(api *GoGym.Gym) {
// Your logic goes there
}
2. Do preparations for your Gym's opening
var gym = new(GoGym.Gym)
gym.Prepare()
3. Register your controller, and make it as a part of your Gym family
gym.Router.RegisterController(&HelloController{})
4. Be ready for getting requests. Let's smile and say hello. :)
gym.Router.Get("/", "HelloController@SayHello")
5. Now your Gym are opening. Let's cheer!
gym.OpenAt(3000)
Core Services
Gym
Gym
is a service container :
RegisterService(name string, service GymService)
: RegisterService registers user's own service into service containerRegisterServices(services map[string]GymService)
: RegisterServices is a function registers mutiple servicesGetService(name string) GymService
: GetService is a fucntion gets a service fromGym
CallServiceMethod(service GymService, method string, param []interface{}) []reflect.Value
: CallServiceMethod is a function call a method of servicePrepare() *Gym
: Prepare is a function prepares the service containerOpenAt(port int)
: OpenAt is a function which is used to serve the service
Router
Get(path, controllerWithActionString string)
: Get is a fucntion handles GET requestsPost(path, controllerWithActionString string)
: Post is a fucntion handles POST requestsPut(path, controllerWithActionString string)
: Put is a method handles PUT requestsPatch(path, controllerWithActionString string)
: Patch is a method handles PATCH requestsOptions(path, controllerWithActionString string)
: Options is a method handles OPTIONS requestsDelete(path, controllerWithActionString string)
: Delete is a method handles DELETE requestsRegisterController
: RegisterControllers is a method registers a controller into controllerRegistryRegisterControllers
: RegisterControllers is a method registers a struct of controllers into controllerRegistry
Request
Request.Method
: It gets the method of the http requestRequest.Header
: It gets the header of the http requestRequest.Query
: It parses query of the http requestRequest.Form
: It parses request form of the http request
Response
JsonResponse(resp interface{}, statusCode int, header http.Header)
:JsonResponse
accept response, status code and http header to generate http JSON response.
Helpers
GetType(value interface{}) string
: GetType is a function gets the type of valueCallServiceMethodWithReflect(g GymService, method string, param []interface{}) []reflect.Value
: CallServiceMethodWithReflect is a functon calls user's own service method
Want to implement your own service?
- Implement
GymService
interface - Pass your service into
Gym
using methodRegisterService
orRegisterServices
- Get your service using method
GetService
- Call the service's method using
CallMethod
- You could write your own
CallMethod
or use the helper functionCallServiceMethodWithReflect()
Code Example
package main
import (
"fmt"
"github.com/ZhenhangTung/GoGym"
"net/http"
"reflect"
)
type HelloController struct {
}
func (h *HelloController) SayHello(api *GoGym.Gym) {
method := api.Request.Method
fmt.Println(method)
api.Response.JsonResponse(map[string]string{"hello": "world"}, 200, http.Header{})
}
type FooService struct {
boss *GoGym.Gym
}
func (f *FooService) Prepare(g *GoGym.Gym) {
f.WhoIsYourBoss(g)
}
func (f *FooService) WhoIsYourBoss(g *GoGym.Gym) {
f.boss = g
}
func (f *FooService) CallYourBoss() *GoGym.Gym {
return f.boss
}
func (f *FooService) Test() {
fmt.Println("oh yes")
}
func (f *FooService) CallMethod(method string, param []interface{}) []reflect.Value {
r := GoGym.CallServiceMethodWithReflect(f, method, param)
return r
}
func main() {
var gym = new(GoGym.Gym)
gym.Prepare()
gym.Router.RegisterController(&HelloController{})
gym.Router.Get("/", "HelloController@SayHello")
gym.RegisterService("Foo", new(FooService))
gym.GetService("Foo").CallMethod("Test", nil)
gym.OpenAt(3000)
}
// Then open the http://localhost:3000 to see the result
Contribution
Your contribution to GoGym
development is very welcome!
You may contribute in the following ways:
- Post issues and feedbacks
- Submit fixes, features via Pull Request
- Write/polish documentation
Contributors
https://github.com/ZhenhangTung/GoGym/graphs/contributors
License
GoGym
is released under the MIT License.