template

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2022 License: MIT Imports: 0 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var KControllerAction = `

func (s *${class}Controller) Do${action}${class}() error {

	return nil
}

`
View Source
var KControllerDefine = `` /* 171-byte string literal not displayed */
View Source
var KGinExampleTemplate = `// For more information, please check the official document
// https://gin-gonic.com/zh-cn/docs/examples/

var db = make(map[string]string)

func setupRouter() *gin.Engine {
	// Disable Console Color
	// gin.DisableConsoleColor()
	r := gin.Default()

	// Ping test
	r.GET("/ping", func(c *gin.Context) {
		c.String(http.StatusOK, "pong")
	})

	// Get user value
	r.GET("/user/:name", func(c *gin.Context) {
		user := c.Params.ByName("name")
		value, ok := db[user]
		if ok {
			c.JSON(http.StatusOK, gin.H{"user": user, "value": value})
		} else {
			c.JSON(http.StatusOK, gin.H{"user": user, "status": "no value"})
		}
	})

	// Authorized group (uses gin.BasicAuth() middleware)
	// Same than:
	// authorized := r.Group("/")
	// authorized.Use(gin.BasicAuth(gin.Credentials{
	//	  "foo":  "bar",
	//	  "manu": "123",
	//}))
	authorized := r.Group("/", gin.BasicAuth(gin.Accounts{
		"foo":  "bar", // user:foo password:bar
		"manu": "123", // user:manu password:123
	}))

	/* example curl for /admin with basicauth header
	   Zm9vOmJhcg== is base64("foo:bar")

		curl -X POST \
	  	http://localhost:8080/admin \
	  	-H 'authorization: Basic Zm9vOmJhcg==' \
	  	-H 'content-type: application/json' \
	  	-d '{"value":"bar"}'
	*/
	authorized.POST("admin", func(c *gin.Context) {
		user := c.MustGet(gin.AuthUserKey).(string)

		// Parse JSON
		var json struct {
			Value string ` + "`json:\"value\" binding:\"required\"`" + `
		}

		if c.Bind(&json) == nil {
			db[user] = json.Value
			c.JSON(http.StatusOK, gin.H{"status": "ok"})
		}
	})

	return r
}`
View Source
var KHandlerBind = `` /* 141-byte string literal not displayed */
View Source
var KNoResponseHandlerByAction = `

func ${action}${class}Handler(c *gin.Context) {
	_ = New${class}Controller(c).Do${action}${class}() 
}

`
View Source
var KResponseHandlerByAction = `` /* 160-byte string literal not displayed */
View Source
var KResponseTemplate = `
const (
	KStatusOk = iota
	KStatusErr
)

type ResponseMsg[T any] struct {
	StatusCode int    ` + "`json:\"status_code\"`" + `
	StatusMsg  string ` + "`json:\"status_msg\"`" + `
	Msg        *T     ` + "`json:\"msg\"`" + `
}

func BadResponse(c *gin.Context, msg string) {
	c.JSON(http.StatusOK, gin.H{
		"status_code": KStatusErr,
		"status_msg":  msg,
	})
}

func GoodResponse[T any](c *gin.Context, msg *T) {
	c.JSON(http.StatusOK, &ResponseMsg[T]{
		StatusCode: KStatusOk,
		StatusMsg:  "response OK!",
		Msg:        msg,
	})
}
`

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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