go-liziwei01-library

command module
v1.3.16 Latest Latest
Warning

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

Go to latest
Published: May 29, 2021 License: MIT Imports: 1 Imported by: 0

README

go-liziwei01-library

standard-readme compliant

This is a school project base written by liziwei from CUHK(SZ)

Clone & Install Hook

git clone https://github.com/liziwei01/go-liziwei01-library.git &&
wget https://github.com/liziwei01/hooks/archive/refs/tags/1.0.tar.gz &&
tar -xzvf 1.0.tar.gz &&
mv hooks-1.0/commit-msg go-liziwei01-library/.git/hooks &&
rm -rf hooks-1.0 &&
rm 1.0.tar.gz &&
cd go-liziwei01-library

Run

go run main.go

Use

env&conf
# write an app conf file like this
# app.toml
APPName = "go-liziwei01-appui"
[HTTPServer]
Listen="0.0.0.0:{env.LISTEN_PORT|8080}"
import (
    "path/filepath"
    "github.com/liziwei01/go-liziwei01-library/library/conf"
    "github.com/liziwei01/go-liziwei01-library/library/env"
)

const (
    appConfPath = "./conf/app.toml"
)

type Config struct {
    APPName string
    Env env.AppEnv
    HTTPServer struct {
        Listen       string
    }
}

func GetConfig(filePath string) (*Config, error) {
    confPath, err := filepath.Abs(filePath)
    if err != nil {
        return nil, err
    }
    var c *Config
    # here is the main part
    if err := conf.Parse(confPath, &c); err != nil {
        return nil, err
    }
    rootDir := filepath.Dir(filepath.Dir(confPath))
    opt := env.Option{
        AppName: c.APPName,
        RunMode: c.RunMode,
        RootDir: rootDir,
        DataDir: filepath.Join(rootDir, "data"),
        LogDir:  filepath.Join(rootDir, "log"),
        ConfDir: filepath.Join(rootDir, filepath.Base(filepath.Dir(confPath))),
    }
    c.Env = env.New(opt)
    return c, nil
}

ghttp
import (
    "context"
    "net/http"
    errBase "github.com/liziwei01/go-liziwei01-library/model/error"
    "github.com/liziwei01/go-liziwei01-library/model/ghttp"
)

type PaperSearchParams struct {
    StartTime  int64  `json:"start_time"`
    EndTime    int64  `json:"end_time"`
}

func init() {
    # Config.HTTPServer.Listen is 0.0.0.0:8080 in my conf file
    _ = http.ListenAndServe(Config.HTTPServer.Listen, nil)
    http.HandleFunc("/paperList", GetPaperList)
}

func GetPaperList(response http.ResponseWriter, request *http.Request) {
    # initialize ghttp
    g := ghttp.Default(&request, &response)
    # get front end params
    params, err := getPaperInput(ctx, g)
    if err != nil {
        g.Write(params, errBase.ErrorNoClient, err)
    }
    # GetPaperSlice get data from mysql and returns a data slice `res`
    res, err := GetPaperSlice(ctx, params)
    if err != nil {
        g.Write(res, errBase.ErrorNoServer, err)
    }
    # return
    g.Write(res, errBase.ErrorNoSuccess, err)
}

func getPaperInput(ctx context.Context, g ghttp.Ghttp) {
    return PaperSearchParams{
        # also support g.Post()
        EndTime:    g.Get("end_time"),
        Journal:    g.Get("start_time"),
    }
}

when you

curl localhost:8080?start_time=0&end_time=100000

you will get json return like this

{
    "data": # the data slice
        [{
            "title": "something",
            "author": "somebody",
            "publish_time": "10000000"
        }]
    "errno": # error number
    "errmsg": # error message
}
mysql
# write the database conf file like this
# ./conf/servicer/db_liziwei01.toml
Username = "root"
Password = "liziwei01"
DbName = "db_liziwei01"
DbDriver = "mysql"
Host = "localhost"
Port = 3306
import (
    "context"
    "github.com/liziwei01/go-liziwei01-library/model/mysql"
)

type PaperSearchParams struct {
    StartTime  int64  `json:"start_time"`
    EndTime    int64  `json:"end_time"`
}

type PaperInfo struct {
    Title           string `db:"title" json:"title"`
    Authors         string `db:"author" json:"authors"`
    PublishTime     int64  `db:"publish_time" json:"publish_time"`
}

func GetPaperSlice(ctx context.Context, params PaperSearchParams) ([]PaperInfo, error) {
    # init and link the mysql database db_liziwei01
    var res []PaperInfo
    mysql.GetMysqlClient(ctx, "db_liziwei01")
    where := map[string]interface{}{
            "_orderby":        "score desc",
            "_limit":          []uint{0, 10},
            "publish_time >=": params.StartTime,
            "publish_time <=": params.EndTime,
        }
    columns := []string{"title", "authors"}
    # query the 
    # table `tb_paper_info` 
    # for `columns` 
    # with `where` and
    # data will be stored in `res` slice
    err = client.Query(ctx, "tb_paper_info", where, columns, &res)
    if err != nil {
        return nil, err
    }
    return res, nil
}

License

MIT License

Documentation

Overview

* @Author: liziwei01 * @Date: 2021-04-19 15:00:00 * @LastEditTime: 2021-05-30 02:32:09 * @LastEditors: liziwei01 * @Description: main * @FilePath: /github.com/liziwei01/go-liziwei01-library/main.go

Directories

Path Synopsis
* @Author: liziwei01 * @Date: 2021-04-29 15:14:03 * @LastEditors: liziwei01 * @LastEditTime: 2021-05-30 02:29:01 * @Description: bootstrap * @FilePath: /github.com/liziwei01/go-liziwei01-library/bootstrap/app.go * @Author: liziwei01 * @Date: 2021-04-29 15:14:03 * @LastEditors: liziwei01 * @LastEditTime: 2021-05-30 02:29:22 * @Description: bootstrap * @FilePath: /github.com/liziwei01/go-liziwei01-library/bootstrap/bootstrap.go
* @Author: liziwei01 * @Date: 2021-04-29 15:14:03 * @LastEditors: liziwei01 * @LastEditTime: 2021-05-30 02:29:01 * @Description: bootstrap * @FilePath: /github.com/liziwei01/go-liziwei01-library/bootstrap/app.go * @Author: liziwei01 * @Date: 2021-04-29 15:14:03 * @LastEditors: liziwei01 * @LastEditTime: 2021-05-30 02:29:22 * @Description: bootstrap * @FilePath: /github.com/liziwei01/go-liziwei01-library/bootstrap/bootstrap.go
* @Author: liziwei01 * @Date: 2021-04-19 15:00:00 * @LastEditTime: 2021-05-30 02:29:51 * @LastEditors: liziwei01 * @Description: start http server and start listening * @FilePath: /github.com/liziwei01/go-liziwei01-library/httpapi/routers.go
* @Author: liziwei01 * @Date: 2021-04-19 15:00:00 * @LastEditTime: 2021-05-30 02:29:51 * @LastEditors: liziwei01 * @Description: start http server and start listening * @FilePath: /github.com/liziwei01/go-liziwei01-library/httpapi/routers.go
library
env
model
error
* @Author: liziwei01 * @Date: 2021-04-29 15:14:03 * @LastEditors: liziwei01 * @LastEditTime: 2021-05-30 02:44:12 * @Description: error model * @FilePath: /github.com/liziwei01/go-liziwei01-library/model/error/base.go
* @Author: liziwei01 * @Date: 2021-04-29 15:14:03 * @LastEditors: liziwei01 * @LastEditTime: 2021-05-30 02:44:12 * @Description: error model * @FilePath: /github.com/liziwei01/go-liziwei01-library/model/error/base.go
ghttp
* @Author: liziwei01 * @Date: 2021-04-29 15:14:03 * @LastEditors: liziwei01 * @LastEditTime: 2021-05-30 02:35:36 * @Description: http model * @FilePath: /github.com/liziwei01/go-liziwei01-library/model/ghttp/base.go
* @Author: liziwei01 * @Date: 2021-04-29 15:14:03 * @LastEditors: liziwei01 * @LastEditTime: 2021-05-30 02:35:36 * @Description: http model * @FilePath: /github.com/liziwei01/go-liziwei01-library/model/ghttp/base.go
logit
* @Author: liziwei01 * @Date: 2021-05-29 15:14:03 * @LastEditors: liziwei01 * @LastEditTime: 2021-05-30 02:33:19 * @Description: log model * @FilePath: /github.com/liziwei01/go-liziwei01-library/model/logit/base.go
* @Author: liziwei01 * @Date: 2021-05-29 15:14:03 * @LastEditors: liziwei01 * @LastEditTime: 2021-05-30 02:33:19 * @Description: log model * @FilePath: /github.com/liziwei01/go-liziwei01-library/model/logit/base.go
mysql
* @Author: liziwei01 * @Date: 2021-05-29 15:14:03 * @LastEditors: liziwei01 * @LastEditTime: 2021-05-30 02:32:54 * @Description: mysql model * @FilePath: /github.com/liziwei01/go-liziwei01-library/model/mysql/base.go
* @Author: liziwei01 * @Date: 2021-05-29 15:14:03 * @LastEditors: liziwei01 * @LastEditTime: 2021-05-30 02:32:54 * @Description: mysql model * @FilePath: /github.com/liziwei01/go-liziwei01-library/model/mysql/base.go

Jump to

Keyboard shortcuts

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