Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
load(
"@io_bazel_rules_go//proto:def.bzl",
"go_proto_library",
)
proto_library(
name = "render_proto",
srcs = ["pb.proto"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"@com_google_protobuf//:any_proto",
"@gogo_special_proto//github.com/gogo/protobuf/gogoproto",
],
)
go_proto_library(
name = "render_go_proto",
compilers = ["@io_bazel_rules_go//proto:gogo_proto"],
importpath = "go-common/library/net/http/blademaster/render",
proto = ":render_proto",
tags = ["manual"],
visibility = ["//visibility:public"],
deps = [
"@com_github_gogo_protobuf//gogoproto:go_default_library",
"@com_github_golang_protobuf//ptypes/any:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"data.go",
"json.go",
"protobuf.go",
"redirect.go",
"render.go",
"string.go",
"xml.go",
],
embed = [":render_go_proto"],
importpath = "go-common/library/net/http/blademaster/render",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//vendor/github.com/pkg/errors:go_default_library",
"@com_github_gogo_protobuf//proto:go_default_library",
"@com_github_gogo_protobuf//types:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,5 @@
### net/http/blademaster/render
##### Version 1.0.0
> 1. 完成基本功能

View File

@@ -0,0 +1,8 @@
# Author
maojian
lintnaghui
caoguoliang
zhoujiahui
# Reviewer
maojian

View File

@@ -0,0 +1,12 @@
# See the OWNERS docs at https://go.k8s.io/owners
approvers:
- caoguoliang
- lintnaghui
- maojian
- zhoujiahui
reviewers:
- caoguoliang
- lintnaghui
- maojian
- zhoujiahui

View File

@@ -0,0 +1,13 @@
#### net/http/blademaster/render
##### 项目简介
> blademaster 的 render package
##### 编译环境
> - **请只用 Golang v1.8.x 以上版本编译执行**
##### 依赖包
> - No other dependency

View File

@@ -0,0 +1,30 @@
package render
import (
"net/http"
"github.com/pkg/errors"
)
// Data common bytes struct.
type Data struct {
ContentType string
Data [][]byte
}
// Render (Data) writes data with custom ContentType.
func (r Data) Render(w http.ResponseWriter) (err error) {
r.WriteContentType(w)
for _, d := range r.Data {
if _, err = w.Write(d); err != nil {
err = errors.WithStack(err)
return
}
}
return
}
// WriteContentType writes data with custom ContentType.
func (r Data) WriteContentType(w http.ResponseWriter) {
writeContentType(w, []string{r.ContentType})
}

View File

@@ -0,0 +1,58 @@
package render
import (
"encoding/json"
"net/http"
"github.com/pkg/errors"
)
var jsonContentType = []string{"application/json; charset=utf-8"}
// JSON common json struct.
type JSON struct {
Code int `json:"code"`
Message string `json:"message"`
TTL int `json:"ttl"`
Data interface{} `json:"data,omitempty"`
}
func writeJSON(w http.ResponseWriter, obj interface{}) (err error) {
var jsonBytes []byte
writeContentType(w, jsonContentType)
if jsonBytes, err = json.Marshal(obj); err != nil {
err = errors.WithStack(err)
return
}
if _, err = w.Write(jsonBytes); err != nil {
err = errors.WithStack(err)
}
return
}
// Render (JSON) writes data with json ContentType.
func (r JSON) Render(w http.ResponseWriter) error {
// FIXME(zhoujiahui): the TTL field will be configurable in the future
if r.TTL <= 0 {
r.TTL = 1
}
return writeJSON(w, r)
}
// WriteContentType write json ContentType.
func (r JSON) WriteContentType(w http.ResponseWriter) {
writeContentType(w, jsonContentType)
}
// MapJSON common map json struct.
type MapJSON map[string]interface{}
// Render (MapJSON) writes data with json ContentType.
func (m MapJSON) Render(w http.ResponseWriter) error {
return writeJSON(w, m)
}
// WriteContentType write json ContentType.
func (m MapJSON) WriteContentType(w http.ResponseWriter) {
writeContentType(w, jsonContentType)
}

View File

@@ -0,0 +1,89 @@
// Code generated by protoc-gen-gogo. DO NOT EDIT.
// source: pb.proto
/*
Package render is a generated protocol buffer package.
It is generated from these files:
pb.proto
It has these top-level messages:
PB
*/
package render
import proto "github.com/gogo/protobuf/proto"
import fmt "fmt"
import math "math"
import google_protobuf "github.com/gogo/protobuf/types"
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.GoGoProtoPackageIsVersion2 // please upgrade the proto package
type PB struct {
Code int64 `protobuf:"varint,1,opt,name=Code,proto3" json:"Code,omitempty"`
Message string `protobuf:"bytes,2,opt,name=Message,proto3" json:"Message,omitempty"`
TTL uint64 `protobuf:"varint,3,opt,name=TTL,proto3" json:"TTL,omitempty"`
Data *google_protobuf.Any `protobuf:"bytes,4,opt,name=Data" json:"Data,omitempty"`
}
func (m *PB) Reset() { *m = PB{} }
func (m *PB) String() string { return proto.CompactTextString(m) }
func (*PB) ProtoMessage() {}
func (*PB) Descriptor() ([]byte, []int) { return fileDescriptorPb, []int{0} }
func (m *PB) GetCode() int64 {
if m != nil {
return m.Code
}
return 0
}
func (m *PB) GetMessage() string {
if m != nil {
return m.Message
}
return ""
}
func (m *PB) GetTTL() uint64 {
if m != nil {
return m.TTL
}
return 0
}
func (m *PB) GetData() *google_protobuf.Any {
if m != nil {
return m.Data
}
return nil
}
func init() {
proto.RegisterType((*PB)(nil), "render.PB")
}
func init() { proto.RegisterFile("pb.proto", fileDescriptorPb) }
var fileDescriptorPb = []byte{
// 154 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x28, 0x48, 0xd2, 0x2b,
0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x2b, 0x4a, 0xcd, 0x4b, 0x49, 0x2d, 0x92, 0x92, 0x4c, 0xcf,
0xcf, 0x4f, 0xcf, 0x49, 0xd5, 0x07, 0x8b, 0x26, 0x95, 0xa6, 0xe9, 0x27, 0xe6, 0x55, 0x42, 0x94,
0x28, 0xe5, 0x71, 0x31, 0x05, 0x38, 0x09, 0x09, 0x71, 0xb1, 0x38, 0xe7, 0xa7, 0xa4, 0x4a, 0x30,
0x2a, 0x30, 0x6a, 0x30, 0x07, 0x81, 0xd9, 0x42, 0x12, 0x5c, 0xec, 0xbe, 0xa9, 0xc5, 0xc5, 0x89,
0xe9, 0xa9, 0x12, 0x4c, 0x0a, 0x8c, 0x1a, 0x9c, 0x41, 0x30, 0xae, 0x90, 0x00, 0x17, 0x73, 0x48,
0x88, 0x8f, 0x04, 0xb3, 0x02, 0xa3, 0x06, 0x4b, 0x10, 0x88, 0x29, 0xa4, 0xc1, 0xc5, 0xe2, 0x92,
0x58, 0x92, 0x28, 0xc1, 0xa2, 0xc0, 0xa8, 0xc1, 0x6d, 0x24, 0xa2, 0x07, 0xb1, 0x4f, 0x0f, 0x66,
0x9f, 0x9e, 0x63, 0x5e, 0x65, 0x10, 0x58, 0x45, 0x12, 0x1b, 0x58, 0xcc, 0x18, 0x10, 0x00, 0x00,
0xff, 0xff, 0x7a, 0x92, 0x16, 0x71, 0xa5, 0x00, 0x00, 0x00,
}

View File

@@ -0,0 +1,14 @@
// use under command to generate pb.pb.go
// protoc --proto_path=.:$GOPATH/src/github.com/gogo/protobuf --gogo_out=Mgoogle/protobuf/any.proto=github.com/gogo/protobuf/types:. *.proto
syntax = "proto3";
package render;
import "google/protobuf/any.proto";
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
message PB {
int64 Code = 1;
string Message = 2;
uint64 TTL = 3;
google.protobuf.Any Data = 4;
}

View File

@@ -0,0 +1,38 @@
package render
import (
"net/http"
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
)
var pbContentType = []string{"application/x-protobuf"}
// Render (PB) writes data with protobuf ContentType.
func (r PB) Render(w http.ResponseWriter) error {
if r.TTL <= 0 {
r.TTL = 1
}
return writePB(w, r)
}
// WriteContentType write protobuf ContentType.
func (r PB) WriteContentType(w http.ResponseWriter) {
writeContentType(w, pbContentType)
}
func writePB(w http.ResponseWriter, obj PB) (err error) {
var pbBytes []byte
writeContentType(w, pbContentType)
if pbBytes, err = proto.Marshal(&obj); err != nil {
err = errors.WithStack(err)
return
}
if _, err = w.Write(pbBytes); err != nil {
err = errors.WithStack(err)
}
return
}

View File

@@ -0,0 +1,26 @@
package render
import (
"net/http"
"github.com/pkg/errors"
)
// Redirect render for redirect to specified location.
type Redirect struct {
Code int
Request *http.Request
Location string
}
// Render (Redirect) redirect to specified location.
func (r Redirect) Render(w http.ResponseWriter) error {
if (r.Code < 300 || r.Code > 308) && r.Code != 201 {
return errors.Errorf("Cannot redirect with status code %d", r.Code)
}
http.Redirect(w, r.Request, r.Location, r.Code)
return nil
}
// WriteContentType noneContentType.
func (r Redirect) WriteContentType(http.ResponseWriter) {}

View File

@@ -0,0 +1,30 @@
package render
import (
"net/http"
)
// Render http reponse render.
type Render interface {
// Render render it to http response writer.
Render(http.ResponseWriter) error
// WriteContentType write content-type to http response writer.
WriteContentType(w http.ResponseWriter)
}
var (
_ Render = JSON{}
_ Render = MapJSON{}
_ Render = XML{}
_ Render = String{}
_ Render = Redirect{}
_ Render = Data{}
_ Render = PB{}
)
func writeContentType(w http.ResponseWriter, value []string) {
header := w.Header()
if val := header["Content-Type"]; len(val) == 0 {
header["Content-Type"] = value
}
}

View File

@@ -0,0 +1,40 @@
package render
import (
"fmt"
"io"
"net/http"
"github.com/pkg/errors"
)
var plainContentType = []string{"text/plain; charset=utf-8"}
// String common string struct.
type String struct {
Format string
Data []interface{}
}
// Render (String) writes data with custom ContentType.
func (r String) Render(w http.ResponseWriter) error {
return writeString(w, r.Format, r.Data)
}
// WriteContentType writes string with text/plain ContentType.
func (r String) WriteContentType(w http.ResponseWriter) {
writeContentType(w, plainContentType)
}
func writeString(w http.ResponseWriter, format string, data []interface{}) (err error) {
writeContentType(w, plainContentType)
if len(data) > 0 {
_, err = fmt.Fprintf(w, format, data...)
} else {
_, err = io.WriteString(w, format)
}
if err != nil {
err = errors.WithStack(err)
}
return
}

View File

@@ -0,0 +1,31 @@
package render
import (
"encoding/xml"
"net/http"
"github.com/pkg/errors"
)
// XML common xml struct.
type XML struct {
Code int
Message string
Data interface{}
}
var xmlContentType = []string{"application/xml; charset=utf-8"}
// Render (XML) writes data with xml ContentType.
func (r XML) Render(w http.ResponseWriter) (err error) {
r.WriteContentType(w)
if err = xml.NewEncoder(w).Encode(r.Data); err != nil {
err = errors.WithStack(err)
}
return
}
// WriteContentType write xml ContentType.
func (r XML) WriteContentType(w http.ResponseWriter) {
writeContentType(w, xmlContentType)
}