go-core

module
v1.2.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2023 License: BSD-3-Clause

README

Go-Core Web Framework

Go-Core is a fast, simple and young web framework for Go.

It provides some features. like

  1. Api Strategy
  2. Swagger Generator
  3. Return Hook
  4. Cors
  5. Api Error Catch
  6. ...

Quick start

package main

import (
	api_strategy "github.com/pefish/go-core-strategy/api-strategy"
	global_api_strategy3 "github.com/pefish/go-core-strategy/global-api-strategy"
	_type2 "github.com/pefish/go-core-type/api-session"
	"github.com/pefish/go-core/api"
	"github.com/pefish/go-core/api-strategy/type"
	global_api_strategy2 "github.com/pefish/go-core/driver/global-api-strategy"
	"github.com/pefish/go-core/driver/logger"
	global_api_strategy "github.com/pefish/go-core/global-api-strategy"
	"github.com/pefish/go-core/service"
	"github.com/pefish/go-error"
	"errors"
	go_logger "github.com/pefish/go-logger"
	"log"
	"time"
)

func main() {
	service.Service.SetName(`test service`) // set service name
	service.Service.SetPath(`/api/test`)
	logger.LoggerDriverInstance.Register(go_logger.Logger)
	global_api_strategy.ParamValidateStrategyInstance.SetErrorCode(2005)
	service.Service.SetRoutes([]*api.Api{
		{
			Description: "this is a test api",
			Path:        "/v1/test_api",
			Method:      `POST`,
			Strategies: []_type.StrategyData{
				{
					Strategy: &api_strategy.IpFilterStrategy,
					Param: api_strategy.IpFilterParam{
						GetValidIp: func(apiSession _type2.IApiSession) []string {
							return []string{`127.0.0.1`}
						},
					},
					Disable: true,
				},
			},
			ParamType: global_api_strategy.ALL_TYPE,
			Controller: func(apiSession _type2.IApiSession) (i interface{}, info *go_error.ErrorInfo) {
				var params struct {
					Test string `json:"test" validate:"is-mobile"`
				}
				apiSession.ScanParams(&params)
				return nil, go_error.Wrap(errors.New("haha"))
				//return params.Test, nil
			},
			Params: struct {
				Test string `json:"test" validate:"required,is-mobile"`
			}{},
		},
	})
	global_api_strategy3.GlobalRateLimitStrategy.SetErrorCode(10000)
	global_api_strategy2.GlobalApiStrategyDriverInstance.Register(global_api_strategy2.GlobalStrategyData{
		Strategy: &global_api_strategy3.GlobalRateLimitStrategy,
		Param: global_api_strategy3.GlobalRateLimitStrategyParam{
			FillInterval: 1000 * time.Millisecond,
		},
		Disable: false,
	})
	service.Service.SetPort(8080)

	err := service.Service.Run()
	if err != nil {
		log.Fatal(err)
	}
}

$ go run .
curl -H "Content-Type: application/json" http://0.0.0.0:8080/api/test/v1/test_api -d "{}"

{"msg":"Key: 'Test'; Error:Field validation for 'test' failed on the 'required' tag; sql-inject-check,required,is-mobile","code":2005,"data":{"field":"test"}}

curl -H "Content-Type: application/json" http://0.0.0.0:8080/api/test/v1/test_api -d "{\"test\": \"yrte\"}"

{"msg":"Key: 'Test'; Error:Field validation for 'test' failed on the 'is-mobile' tag; sql-inject-check,required,is-mobile","code":2005,"data":{"field":"test"}}

curl -H "Content-Type: application/json" http://0.0.0.0:8080/api/test/v1/test_api -d "{\"test\": \"18317034426\"}"

{"msg":"","code":0,"data":"18317034426"}

Document

doc

Security Vulnerabilities

If you discover a security vulnerability within Go-Core, please send an e-mail to pefish@qq.com. All security vulnerabilities will be promptly addressed.

License

This project is licensed under the BSD 3-clause license, just like the Go project itself.

Directories

Path Synopsis
api
type Module
type Module
api-strategy module
driver
mock
mock-api-session
Package mock_api_session is a generated GoMock package.
Package mock_api_session is a generated GoMock package.
mock-go-http
Package mock_go_http is a generated GoMock package.
Package mock_go_http is a generated GoMock package.
mock-http
Package mock_http is a generated GoMock package.
Package mock_http is a generated GoMock package.
model
pkg
api-strategy Module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL