solanum

package module
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2024 License: MIT Imports: 8 Imported by: 2

README

Solanum - Web Server Framework Based on Gin

  • This project provides Modulability to Gin Project.
  • You can implement Module, Controller, service to routes, handles, and intercept requests.

Annuum, Potato Can Change The World!

  • dev.whoan(싹난 감자) in Annuums
Run Solanum
Install Go Module
$ go get github.com/annuums/solanum
  • Fast Example
package main

import "github.com/annuums/solanum"

func main() {
  server := *solanum.NewSolanum(5050)
  
  var healthCheckModule solanum.Module
  healthCheckUri := "/ping"
  healthCheckModule = solanum.NewHealthCheckModule(
    healthCheckUri,
  )
  
  server.SetModules(healthCheckModule)
  
  server.Run()
}
Implements Modules, Controllers, Handlers
  • You should develop Module, Controller, Handler which are specified in module.interface.go. This example let you know how to implement Module, Controller, Handler.
Module
var healthCheckModule *SolaModule
var hzOnce sync.Once

func NewHealthCheckModule(uri string) *SolaModule {
  hzOnce.Do(func() {
    if healthCheckModule == nil {
      healthCheckModule = NewModule(uri)
      attachControllers()
      setPreMiddlewares()
      setPostMiddlewares()
    }
  })
  
  return healthCheckModule
}

func attachControllers() {
  // Attatching Controller Directly
  ctr := NewHealthCheckController()
  // ctr2, _ := NewAnotherController()
  //	...
  
  healthCheckModule.SetControllers(ctr)
}

func setPreMiddlewares() {
  healthCheckModule.SetPreMiddleware(
    func(ctx *gin.Context) {
      log.Println("Health Checking...")
    },
	// Add another pre middleware to handle 
  )
}

func setPostMiddlewares() {
  healthCheckModule.SetPostMiddleware(
    func(ctx *gin.Context) {
      log.Println("Health Check Done!")
    },
    // Add another post middleware to handle
  )
}
Controller
func NewHealthCheckController() *SolaController {
  healthCheckController := NewController()
  
  healthCheckController.SetHandlers(SolaService{
    Uri:        "",
    Method:     http.MethodGet,
    Handler:    hzHandler,
    Middleware: hzMiddleware,
  })
  
  return healthCheckController
}

func hzMiddleware(ctx *gin.Context) {
  log.Println("Health Checking...")
  ctx.Next()
}

Handler
  • For now, you should implement multiple handlers for multiple routings.
  • It means that if you want to routes / and /healthz, should implement two *service for each of those.
func hzHandler(ctx *gin.Context) {
    ctx.String(200, "pong")
}
  • Finally, you should composite Module, Controller, Handler. After you composited the modules, you can add it with calling SolanumRunner.Addmodule(module_name).

    • If you explicitly declare the Module, Controller, Handler, then you should attach Handler to Controller, and Controller to Module using functions.
    • helloWorldController.AddHandler(helloHandler), helloWorldModule.SetControllers(ctr)
func main() {
	server := *solanum.NewSolanum(5050)

	helloUri := "/"
	helloWorldModule, _ := solanum.NewHelloWorldModule(
		helloUri,
	)

	server.AddModule(helloWorldModule)

	server.Run()
}
  • You can connect to http://localhost:5050. There should be a message: "Hello, World! From HelloWorld Index Handler"

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	CorsDefaultMethods      = []string{"GET", "POST", "DELETE", "PUT", "PATCH", "OPTIONS"}
	CorsDefaultHeaders      = []string{"Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers"}
	CorsDefaultCredentials  = false
	CorsDefaultOriginalFunc = func(origin string) bool {
		return strings.Contains(origin, ":://localhost")
	}
)

Functions

func WithAllowCredentials added in v0.3.2

func WithAllowCredentials(allowCredentials bool) func(*CorsOption)

func WithHeaders added in v0.3.2

func WithHeaders(headers []string) func(*CorsOption)

func WithMaxAge added in v0.3.2

func WithMaxAge(maxAge int) func(*CorsOption)

func WithMethods added in v0.3.2

func WithMethods(methods []string) func(*CorsOption)

func WithOriginFunc added in v0.3.2

func WithOriginFunc(originFunc func(origin string) bool) func(*CorsOption)

func WithUrls added in v0.3.2

func WithUrls(urls []string) func(*CorsOption)

Types

type Controller

type Controller interface {
	Handlers() []SolaService
	SetHandlers(handler ...SolaService)
}

type CorsOption added in v0.3.2

type CorsOption struct {
	Urls             []string
	Headers          []string
	Methods          []string
	AllowCredentials bool
	OriginFunc       func(origin string) bool
	MaxAge           int
}

func CorsOptions added in v0.3.2

func CorsOptions(opts ...func(*CorsOption)) *CorsOption

type Module

type Module interface {
	//* Middlewares
	PreMiddlewares() []gin.HandlerFunc
	AddPreMiddleware(middleware gin.HandlerFunc)
	SetPreMiddleware(middlewares ...gin.HandlerFunc)
	PostMiddlewares() []gin.HandlerFunc
	AddPostMiddleware(middleware gin.HandlerFunc)
	SetPostMiddleware(middlewares ...gin.HandlerFunc)

	//* Controllers
	Controllers() []Controller
	SetControllers(c ...Controller)

	//* Controllers -> Handlers
	SetRoutes(router *gin.RouterGroup)

	// Properties
	Uri() string
}

type Runner

type Runner interface {
	InitModules()
	InitGlobalMiddlewares()
	Modules() []*Module
	SetModules(m ...Module)
	GinEngine() *gin.Engine
	Cors(opts ...func(*CorsOption))

	Run()
}
var SolanumRunner Runner

func NewSolanum

func NewSolanum(port int) *Runner

type SolaController

type SolaController struct {
	// contains filtered or unexported fields
}

func NewController

func NewController() *SolaController

NewController 새로운 Controller를 생성합니다.

func NewHealthCheckController

func NewHealthCheckController() *SolaController

func (*SolaController) Handlers

func (ctr *SolaController) Handlers() []SolaService

func (*SolaController) SetHandlers

func (ctr *SolaController) SetHandlers(handlers ...SolaService)

type SolaModule

type SolaModule struct {
	// contains filtered or unexported fields
}

func NewHealthCheckModule

func NewHealthCheckModule(uri string) *SolaModule

func NewModule

func NewModule(uri string) *SolaModule

NewModule 새로운 모듈을 만듭니다. 이 때, 요청받은 router의 uri가 이미 등록되어 있다면 panic

func (*SolaModule) AddPostMiddleware added in v0.3.2

func (m *SolaModule) AddPostMiddleware(middleware gin.HandlerFunc)

func (*SolaModule) AddPreMiddleware added in v0.3.2

func (m *SolaModule) AddPreMiddleware(middleware gin.HandlerFunc)

func (*SolaModule) Controllers

func (m *SolaModule) Controllers() []Controller

func (*SolaModule) PostMiddlewares added in v0.3.0

func (m *SolaModule) PostMiddlewares() []gin.HandlerFunc

func (*SolaModule) PreMiddlewares added in v0.3.0

func (m *SolaModule) PreMiddlewares() []gin.HandlerFunc

func (*SolaModule) SetControllers

func (m *SolaModule) SetControllers(c ...Controller)

func (*SolaModule) SetPostMiddleware added in v0.3.0

func (m *SolaModule) SetPostMiddleware(middlewares ...gin.HandlerFunc)

func (*SolaModule) SetPreMiddleware added in v0.3.0

func (m *SolaModule) SetPreMiddleware(middlewares ...gin.HandlerFunc)

func (*SolaModule) SetRoutes

func (m *SolaModule) SetRoutes(router *gin.RouterGroup)

func (*SolaModule) Uri

func (m *SolaModule) Uri() string

type SolaService

type SolaService struct {
	Uri     string
	Method  string
	Handler gin.HandlerFunc
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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