first
This commit is contained in:
commit
0dc1db02b0
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
vendor/
|
||||
config.yml
|
1
config.default.yml
Normal file
1
config.default.yml
Normal file
@ -0,0 +1 @@
|
||||
file_path: /tmp
|
19
go.mod
Normal file
19
go.mod
Normal file
@ -0,0 +1,19 @@
|
||||
module git.trj.tw/golang/go-file-serve
|
||||
|
||||
require (
|
||||
git.trj.tw/golang/utils v0.0.0-20190225142552-b019626f0349
|
||||
github.com/gin-contrib/sse v0.0.0-20190125020943-a7658810eb74 // indirect
|
||||
github.com/gin-gonic/gin v1.3.0
|
||||
github.com/golang/protobuf v1.2.0 // indirect
|
||||
github.com/json-iterator/go v1.1.5 // indirect
|
||||
github.com/mattn/go-isatty v0.0.5 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.1 // indirect
|
||||
github.com/stretchr/testify v1.3.0 // indirect
|
||||
github.com/ugorji/go/codec v0.0.0-20190204201341-e444a5086c43 // indirect
|
||||
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd // indirect
|
||||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 // indirect
|
||||
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
|
||||
gopkg.in/go-playground/validator.v8 v8.18.2 // indirect
|
||||
gopkg.in/yaml.v2 v2.2.2
|
||||
)
|
43
go.sum
Normal file
43
go.sum
Normal file
@ -0,0 +1,43 @@
|
||||
git.trj.tw/golang/utils v0.0.0-20190116070516-b266ebeb2d82 h1:to19C8t9z6z4b1EMnMm8JIB4qDBCkBjEAhHH5yopSZ0=
|
||||
git.trj.tw/golang/utils v0.0.0-20190116070516-b266ebeb2d82/go.mod h1:yE+qbsUsijCTdwsaQRkPT1CXYk7ftMzXsCaaYx/0QI0=
|
||||
git.trj.tw/golang/utils v0.0.0-20190225142552-b019626f0349 h1:V6ifeiJ3ExnjaUylTOz37n6z5uLwm6fjKjnztbTCaQI=
|
||||
git.trj.tw/golang/utils v0.0.0-20190225142552-b019626f0349/go.mod h1:yE+qbsUsijCTdwsaQRkPT1CXYk7ftMzXsCaaYx/0QI0=
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/gin-contrib/sse v0.0.0-20190125020943-a7658810eb74 h1:FaI7wNyesdMBSkIRVUuEEYEvmzufs7EqQvRAxfEXGbQ=
|
||||
github.com/gin-contrib/sse v0.0.0-20190125020943-a7658810eb74/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
|
||||
github.com/gin-gonic/gin v1.3.0 h1:kCmZyPklC0gVdL728E6Aj20uYBJV93nj/TkwBTKhFbs=
|
||||
github.com/gin-gonic/gin v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y=
|
||||
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/json-iterator/go v1.1.5 h1:gL2yXlmiIo4+t+y32d4WGwOjKGYcGOuyrg46vadswDE=
|
||||
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
|
||||
github.com/mattn/go-isatty v0.0.5 h1:tHXDdz1cpzGaovsTB+TVB8q90WEokoVmfMqoVcrLUgw=
|
||||
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
|
||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/ugorji/go v1.1.2 h1:JON3E2/GPW2iDNGoSAusl1KDf5TRQ8k8q7Tp097pZGs=
|
||||
github.com/ugorji/go v1.1.2/go.mod h1:hnLbHMwcvSihnDhEfx2/BzKp2xb0Y+ErdfYcrs9tkJQ=
|
||||
github.com/ugorji/go/codec v0.0.0-20190204201341-e444a5086c43 h1:BasDe+IErOQKrMVXab7UayvSlIpiyGwRvuX3EKYY7UA=
|
||||
github.com/ugorji/go/codec v0.0.0-20190204201341-e444a5086c43/go.mod h1:iT03XoTwV7xq/+UGwKO3UbC1nNNlopQiY61beSdrtOA=
|
||||
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd h1:HuTn7WObtcDo9uEEU7rEqL0jYthdXAmZ6PP+meazmaU=
|
||||
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw=
|
||||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
|
||||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
|
||||
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
|
||||
gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ=
|
||||
gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
31
main.go
Normal file
31
main.go
Normal file
@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"git.trj.tw/golang/go-file-serve/module/config"
|
||||
"git.trj.tw/golang/go-file-serve/module/flags"
|
||||
"git.trj.tw/golang/go-file-serve/routes"
|
||||
)
|
||||
|
||||
func init() {
|
||||
flags.RegFlags()
|
||||
}
|
||||
|
||||
func main() {
|
||||
var err error
|
||||
|
||||
flag := flags.GetFlags()
|
||||
|
||||
err = config.LoadConfig(flag.ConfigPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
conf := config.GetConf()
|
||||
|
||||
engine := routes.NewEngine()
|
||||
routes.SetRoutes(engine)
|
||||
|
||||
engine.Run(fmt.Sprintf(":%d", conf.Port))
|
||||
}
|
75
module/config/config.go
Normal file
75
module/config/config.go
Normal file
@ -0,0 +1,75 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
|
||||
"git.trj.tw/golang/utils"
|
||||
)
|
||||
|
||||
// Config struct
|
||||
type Config struct {
|
||||
Port int `yaml:"port"`
|
||||
FilePath string `yaml:"file_path"`
|
||||
}
|
||||
|
||||
var conf *Config
|
||||
|
||||
// LoadConfig -
|
||||
func LoadConfig(p ...interface{}) (err error) {
|
||||
var confPath string
|
||||
if len(p) > 0 {
|
||||
if str, ok := p[0].(string); ok && len(str) > 0 {
|
||||
confPath = str
|
||||
}
|
||||
}
|
||||
if len(confPath) == 0 {
|
||||
root, err := os.Getwd()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
confPath = path.Join(root, "config.yml")
|
||||
}
|
||||
if !utils.CheckExists(confPath, false) {
|
||||
return errors.New("config file not found")
|
||||
}
|
||||
|
||||
fileByte, err := ioutil.ReadFile(confPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
conf = &Config{}
|
||||
err = yaml.Unmarshal(fileByte, conf)
|
||||
|
||||
overrideConfgWithEnv()
|
||||
if !utils.IsDir(conf.FilePath) {
|
||||
return errors.New("file path not folder")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func overrideConfgWithEnv() {
|
||||
port := os.Getenv("PORT")
|
||||
if len(port) > 0 {
|
||||
if port, err := strconv.Atoi(port); err == nil {
|
||||
conf.Port = port
|
||||
}
|
||||
}
|
||||
filePath := os.Getenv("FILE_PATH")
|
||||
if len(filePath) > 0 {
|
||||
if utils.CheckExists(filePath, true) {
|
||||
conf.FilePath = filePath
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetConf -
|
||||
func GetConf() *Config {
|
||||
return conf
|
||||
}
|
88
module/context/context.go
Normal file
88
module/context/context.go
Normal file
@ -0,0 +1,88 @@
|
||||
package context
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
|
||||
// Context -
|
||||
type Context struct {
|
||||
*gin.Context
|
||||
}
|
||||
|
||||
// CustomMiddleware -
|
||||
type CustomMiddleware func(*Context)
|
||||
|
||||
// PatchContext -
|
||||
func PatchContext(handle CustomMiddleware) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
ctx := &Context{
|
||||
Context: c,
|
||||
}
|
||||
handle(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
func parseResponse(body interface{}) interface{} {
|
||||
switch body.(type) {
|
||||
case string:
|
||||
return map[string]interface{}{
|
||||
"message": body,
|
||||
}
|
||||
case map[string]interface{}:
|
||||
case map[string]string:
|
||||
return body
|
||||
default:
|
||||
return map[string]string{
|
||||
"message": "empty",
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// BindData client body data
|
||||
func (p *Context) BindData(i interface{}) error {
|
||||
b := binding.Default(p.Request.Method, p.ContentType())
|
||||
return p.ShouldBindWith(i, b)
|
||||
}
|
||||
|
||||
// Success -
|
||||
func (p *Context) Success(res ...interface{}) {
|
||||
var resBody interface{}
|
||||
if len(res) > 0 {
|
||||
resBody = parseResponse(res)
|
||||
} else {
|
||||
resBody = parseResponse("success")
|
||||
}
|
||||
p.AbortWithStatusJSON(http.StatusOK, resBody)
|
||||
}
|
||||
|
||||
// ClientError -
|
||||
func (p *Context) ClientError(res ...interface{}) {
|
||||
var resBody interface{}
|
||||
if len(res) > 0 {
|
||||
resBody = parseResponse(res)
|
||||
} else {
|
||||
resBody = parseResponse("bad request")
|
||||
}
|
||||
p.AbortWithStatusJSON(http.StatusBadRequest, resBody)
|
||||
}
|
||||
|
||||
// ServerError -
|
||||
func (p *Context) ServerError(res ...interface{}) {
|
||||
var resBody interface{}
|
||||
if len(res) > 0 {
|
||||
resBody = parseResponse(res)
|
||||
} else {
|
||||
resBody = parseResponse("internal error")
|
||||
}
|
||||
p.AbortWithStatusJSON(http.StatusInternalServerError, resBody)
|
||||
}
|
||||
|
||||
// CustomRes -
|
||||
func (p *Context) CustomRes(status int, res interface{}) {
|
||||
resBody := parseResponse(res)
|
||||
p.AbortWithStatusJSON(status, resBody)
|
||||
}
|
22
module/flags/flags.go
Normal file
22
module/flags/flags.go
Normal file
@ -0,0 +1,22 @@
|
||||
package flags
|
||||
|
||||
import "flag"
|
||||
|
||||
// Flags -
|
||||
type Flags struct {
|
||||
ConfigPath string
|
||||
}
|
||||
|
||||
var flags *Flags
|
||||
|
||||
// RegFlags -
|
||||
func RegFlags() {
|
||||
flags = &Flags{}
|
||||
flag.StringVar(&flags.ConfigPath, "config", "", "config path")
|
||||
flag.StringVar(&flags.ConfigPath, "f", "", "config path")
|
||||
}
|
||||
|
||||
// GetFlags -
|
||||
func GetFlags() *Flags {
|
||||
return flags
|
||||
}
|
24
routes/routes.go
Normal file
24
routes/routes.go
Normal file
@ -0,0 +1,24 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// NewEngine -
|
||||
func NewEngine () *gin.Engine {
|
||||
engine := gin.New()
|
||||
engine.Use(gin.Logger())
|
||||
engine.Use(gin.Recovery())
|
||||
|
||||
return engine
|
||||
}
|
||||
|
||||
// SetRoutes -
|
||||
func SetRoutes(r *gin.Engine) {
|
||||
r.GET("/", func (c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "success",
|
||||
})
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user