EasyGoServer
Author: Lin Guanhong/The Ghost at my Fingertips
The Denver nuggets: juejin. Cn/user / 178526…
Blog: www.cnblogs.com/linguanh/
Making: github.com/af913337456…
Contact: [email protected]
[TOC]
— — — — — an overview
—– Script Introduction
——— Linux
——— Windows
——— Mac
—– Usage process
—– Code description
—– TODO
An overview of the
A Go Server framework that automatically generates basic server framework code for you just by relying on the SQL files you create. Contains:
1, basic increase, deletion, change and check
2. Extensible APIS
3. The data incoming from the client and the output from the server all depend on struct
-
For example, you have an inputStruct inputStruct set to
type inputStruct struct { // nullTag==1 specifies that id must be passed to the client {"id":123} Id int64 `json:"id" nullTag:"1"` // nullTag==0 Specifies that the name cannot be used in client input Name string `json:"name" nullTag:"0"` }Copy the code
Json {“id”:666, “name”:” LGH “}
-
Select user. id, user. age from User, then your outputStruct should be:
type outputStruct struct { Id int64 `json:"id"` Age int64 `json:"age"` }Copy the code
Output to the client :[{“id”: XXX,”age”: XXX}]
4. There is very little code you really need to write. For example, in point 3, there is only so much code you need to write, and the default struct will automatically generate it for you
The script is introduced
According to the SQL file, automatically generate code file, including struct.go, each table corresponding to generate a basic method file one_key_create_code containing add, delete, change and check
Compile and run the default go server program from the built-in makefile or.bat. Note the default make_server
Linux OS
one_key_create_code.sh
make_server.sh
Makefile
Windows OS
one_key_create_code.bat
make_server.bat
Mac OS
With reference to the Linux
Using the process
1. Install mysql or Mariadb on your server
2, prepare your SQL file, you can refer to my source code in this.sql
3. Run the SQL file prepared in Step 2
4, change the name of the MAIN SQL file in sql_2_api.go
5. Run the one_key_create_code script. If the one_key_create_code script succeeds, the following files will be generated in the same directory
struct.go
, which contains the annotation specification- Create a function file that corresponds to the name of the table in your SQL file.
Func_ Table name. Go
Go or use the default LghSampleMain. Go I provided and add your own route to it
router.HandleFunc("/insert",insert_luser_sample).Methods("POST")
router.HandleFunc("/select",select_luser_sample).Methods("GET")
router.HandleFunc("/update",update_luser_sample).Methods("POST")
router.HandleFunc("/delete",delete_luser_sample).Methods("POST")Copy the code
7. Configure the conf.json file. I have examples in it
// Host is the absolute path // Port is the Port to listen on {"Host": "127.0.0.1", "Port": ":8884", "FilePort":":8885", "DbName":"database", "DbUser":"root", "DbPw":"123456", "DbPort":"3306" }Copy the code
8, now execute the make_server script and observe the output of the console.
Part code description
Core parameter structure
type LghRequest struct {
w http.ResponseWriter
r *http.Request
// The tag uses the current method name
funcName string
// The input structure corresponds to the json input from the client
inputStruct interface{}
// Custom callbacks with slices so you can do parameter processing, returning true means the operation is terminated, such as update
slicesCallBack func(slices []interface{}) bool// According to the incomingjsonObjThe generatedslicesCall back and forth, method generated customsql
getSqlCallBack func(slices []interface{},inputStruct interface{}) string
}Copy the code
Example method
1, the demonstration does not need the form of parameters
/** Demo does not require the form of arguments */
func update_0(w http.ResponseWriter,r *http.Request) {
request := LghRequest{
w,
r,
"update_luser".nil./** nil means no input structure */
func(slices *[]interface{}) bool{
return false
},
func(slices *[]interface{},inputStruct interface{}) string {
return "update LUser set u_user_id='444' where id='1'"
}}
updateDataByStruct(request)
}Copy the code
2. Demonstrate the case that when parameters are entered, parameters are only judged, but do not need to be combined into SQL
/** shows the case where parameters are entered, but do not need to be combined into SQL */
func update_1(w http.ResponseWriter,r *http.Request) {
type testS struct {
Id int64 `json:"id" nullTag:"1"` // nullTag==1 specifies that id must be passed to the client {"id":123}
}
request := LghRequest{
w,
r,
"update_luser".new (testS),
func(slices []interface{}) bool{
// Here, do what you want to do with slices, add or delete, etc
if slices[0] = =- 1{
return true /** returns true, terminates insertion, prompts error, or other */
}
slices = append(slices[:0].nil) /** delete */
return false
},
func(slices []interface{},inputStruct interface{}) string {
// If you want to specifically generate SQL based on input JSON data, you can do this using slices here
return "update LUser set u_user_id='444' where id='2'"
}}
updateDataByStruct(request)
}Copy the code
3. Demonstrate the use of input parameters
/** demonstrates the use of input parameters */
func update_luser_sample(w http.ResponseWriter,r *http.Request) {
type testS struct {
Id int64 `json:"id" nullTag:"1"`
}
request := LghRequest{
w,
r,
"update_luser".new (testS),
func(slices []interface{}) bool{
return false
},
func(slices []interface{},inputStruct interface{}) string {
return "update LUser set u_user_id='444' where id=?" /** Corresponds to id */
}}
updateDataByStruct(request)
}Copy the code
Open source address
Github.com/af913337456…