Help us shape the future of Go! Take the 2026 Go Developer Survey

solanum

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 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 hzOnce sync.Once

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

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

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

This section is empty.

Types

type Controller

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

type Module

type Module interface {
	//* Middlewares
	GlobalMiddlewares() []gin.HandlerFunc
	SetGlobalMiddleware(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(url, headers, methods []string, allowCredentials bool, originFunc func(origin string) bool, maxAge int)

	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) Controllers

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

func (*SolaModule) GlobalMiddlewares

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

func (*SolaModule) SetControllers

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

func (*SolaModule) SetGlobalMiddleware

func (m *SolaModule) SetGlobalMiddleware(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
	Middleware gin.HandlerFunc
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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