GO language learning by examples of Web development transplant Netease cloud music API project

Netease cloud music API

In open source projects on github NetEase cloud music series in the most star should be NeteaseCloudMusicApi a lot of fun Netease cloud music projects are based on this project, today will lead us familiar with this excellent project simultaneously transplanted to GO language, more familiar go through transplant language and gin framework.

project address:

  go Version: https://github.com/Jackkakaya/NeteaseCloudMusicGoApi

  original node: https://github.com/Binaryify/NeteaseCloudMusicApi

Enter the node version github Home

 

 

go home version

 

 

 I found true multi-node version of the stars


node version because there are more mature doc / test and other documents as well as test directory, go after all is to extract the core. app.go is the entrance of the project can start reading from here. Similar logic due to the project, only to learn the language analysis go here

go version.

func InitRouter() *gin.Engine {
    //gin.SetMode(gin.ReleaseMode)
    var musi Cobain models.MusicObain
    store := persistence.NewInMemoryStore(time.Second)
    r: = gin.New ()
     // Registration middleware
    r.Use(gin.Logger())
    r.Use(gin.Recovery())
    r.Use(RequestModifyMiddleware)
    files, err := ioutil.ReadDir(ModelPath)
    if err != nil {
        log.Fatalln(err)
    }
    filter := map[string]bool{
        "music_obtain.go": true,
    }
    // traverse models folder, the file name is formed routing
     // such as files: album_sublist.go is a path to Album / subList Router path request
     // request to AlbumSublist album_sublist.go method of treatment
     for _, File: = Files {Range
         IF filename: file.name = (); filter [filename] == to false {
            filename = strings.TrimSuffix(filename, ".go")
            path := fmt.Sprintf("/%v", strings.ReplaceAll(filename, "_", "/"))
            Method: = strings.ReplaceAll (strings.Title (strings.ReplaceAll (filename, " _ " , "  " )), "  " , "" ) 
       // Accept any request method r.Any(path, cache.CachePage(store,
2*time.Minute, func(context *gin.Context) { query := map[string]interface{}{} data, _ := ioutil.ReadAll(context.Request.Body) _ = json.Unmarshal(data, &query) CookieParseMiddleware(context, &query) for key, val := range context.Request.URL.Query() { query[key] = val[0] } for key, val := range context.Request.PostForm { query[key] = val }
          // call the specified method of radiation through the structure under the package respRaw :
= reflect.ValueOf(&musicObain).MethodByName(method).Call([]reflect.Value{reflect.ValueOf(query)}) resp := respRaw[0].Interface().(map[string]interface{}) for _, val := range resp["cookie"].([]string) { context.Writer.Header().Add("Set-Cookie", val) } context.JSON(200, resp) })) } } return r }

 

 Entrance project core function InitRouter , automatic route formation, the formation of routing rules are as follows:

  请求 demo:

    localhost:8080/login/cellphone?phone=xxxx&password=xxxx

    It will hit the LoginCellphone method for processing the request in login_cellphone.go.

  

  Request Core: pkg / request / request.go

    This package implements all requests package, this package is recommended perusal

    Business process: models / *

  Example Business Logic:

package models

import "github.com/Jackkakaya/NeteaseCloudMusicGoApi/pkg/request"

func (m *MusicObain) ActivateInitProfile(query map[string]interface{}) map[string]interface{} {
	data := map[string]interface{}{
		"nickname": query["nickname"],
	}
	options := map[string]interface{}{
		"crypto": "eapi",
		"cookie": query["cookie"],
		"proxy":  query["proxy"],
		"url":    "/api/activate/initProfile",
	}
	return request.CreateRequest(
		"POST", "http://music.163.com/eapi/activate/initProfile",
		data,
		options)
}

  Here business logic, mainly handling parameters, call CreateRequest method in pkg / request to request.

 

to sum up

  The entire project is the core routing handler gin InitRouter, automated form processing middleware routing added simultaneously, in addition to that CreateRequest pkg is encapsulated in the request for all requests

   The models are all business logic is service logic for each call request / CreateRequest request.

 

At last

  Project Address: https://github.com/Jackkakaya/NeteaseCloudMusicGoApi

  Welcome to download the study. Follow the recommended source of learning. Welcome star! !

 

  

  

 

  

Guess you like

Origin www.cnblogs.com/jake9402/p/12525290.html