├── sample01.jpeg ├── feign ├── Makefile ├── go.mod ├── lbc.go ├── log.go ├── feign_test.go └── feign.go ├── samples ├── sample_use_urls.go ├── sample_use_discover_client.go └── sample_mixed_use.go ├── README.md └── LICENSE /sample01.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HikoQiu/go-feign/HEAD/sample01.jpeg -------------------------------------------------------------------------------- /feign/Makefile: -------------------------------------------------------------------------------- 1 | test: 2 | GOCACHE=off go test -v github.com/HikoQiu/go-feign/feign -run=Test_$(func) 3 | 4 | -------------------------------------------------------------------------------- /feign/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/HikoQiu/go-feign/feign 2 | 3 | replace ( 4 | golang.org/x/crypto v0.0.0-20180820150726-614d502a4dac => github.com/golang/crypto v0.0.0-20180820150726-614d502a4dac 5 | golang.org/x/net v0.0.0-20180821023952-922f4815f713 => github.com/golang/net v0.0.0-20180826012351-8a410e7b638d 6 | golang.org/x/text v0.3.0 => github.com/golang/text v0.3.0 7 | ) 8 | -------------------------------------------------------------------------------- /samples/sample_use_urls.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/HikoQiu/go-feign/feign" 5 | "log" 6 | ) 7 | 8 | 9 | func main() { 10 | // 1.1 configure maps, app => app's urls 11 | feign.DefaultFeign.UseUrls(map[string][]string{ 12 | "MS-REGISTRY-URLS": {"http://192.168.20.236:9001", "http://192.168.20.237:9001"}, 13 | }) 14 | 15 | // 2.1 Use app name to load balance request all instances 16 | res, err := feign.DefaultFeign.App("MS-REGISTRY-URLS").R().SetHeaders(map[string]string{ 17 | "Content-Type": "application/json", 18 | }).Get("/eureka/apps/MS-REGISTRY") 19 | if err != nil { 20 | log.Println("err: ", err.Error()) 21 | return 22 | } 23 | log.Println("res=", string(res.Body())) 24 | 25 | // test: request to another url balancely. 26 | res, err = feign.DefaultFeign.App("MS-REGISTRY-URLS").R().SetHeaders(map[string]string{ 27 | "Content-Type": "application/json", 28 | }).Get("/eureka/apps/MS-REGISTRY") 29 | if err != nil { 30 | log.Println("err: ", err.Error()) 31 | return 32 | } 33 | log.Println("res=", string(res.Body())) 34 | 35 | select{} 36 | } 37 | -------------------------------------------------------------------------------- /feign/lbc.go: -------------------------------------------------------------------------------- 1 | package feign 2 | 3 | import ( 4 | "gopkg.in/resty.v1" 5 | "sync/atomic" 6 | ) 7 | 8 | // Feign load balance client 9 | type Lbc struct { 10 | feign *Feign 11 | app string 12 | index uint32 13 | 14 | // resty.Client 15 | client *resty.Client 16 | } 17 | 18 | // pick a server to send request 19 | func (t *Lbc) pickUrl() string { 20 | urls, ok := t.feign.GetAppUrls(t.app) 21 | if !ok || len(urls) == 0 { 22 | log.Errorf("Failed to pick server, reason: no available urls for app=%s", t.app) 23 | 24 | // no need to panic 25 | // coz return empty string won't panic while calls like "DefaultFeign.App("APP_NAME").R().Post()" 26 | // it will only get request failed 27 | return "" 28 | } 29 | 30 | idx := atomic.AddUint32(t.feign.appNextUrlIndex[t.app], 1) 31 | idx %= uint32(len(urls)) 32 | t.index = idx 33 | atomic.CompareAndSwapUint32(t.feign.appNextUrlIndex[t.app], uint32(len(urls)), 0) 34 | 35 | return urls[idx] 36 | } 37 | 38 | // pick target url and new resty.Client 39 | func (t *Lbc) pick() *Lbc { 40 | t.client = resty.New() 41 | t.client.HostURL = t.pickUrl() 42 | log.Debugf("Picked url=%s", t.client.HostURL) 43 | return t 44 | } 45 | -------------------------------------------------------------------------------- /samples/sample_use_discover_client.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/HikoQiu/go-feign/feign" 5 | "log" 6 | "github.com/HikoQiu/go-eureka-client/eureka" 7 | ) 8 | 9 | func runEurekaClient() *eureka.Client { 10 | config := eureka.GetDefaultEurekaClientConfig() 11 | config.UseDnsForFetchingServiceUrls = true 12 | config.Region = "region-cn-hd-1" 13 | config.AvailabilityZones = map[string]string{ 14 | "region-cn-hd-1": "zone-cn-hz-1", 15 | } 16 | config.EurekaServerDNSName = "dev.ms-registry.xf.io" 17 | config.EurekaServerUrlContext = "eureka" 18 | config.EurekaServerPort = "9001" 19 | 20 | // run eureka client async 21 | eureka.DefaultClient.Config(config). 22 | Register("APP_ID_CLIENT_FROM_DNS", 9000). 23 | Run() 24 | 25 | return eureka.DefaultClient 26 | } 27 | 28 | func main() { 29 | runEurekaClient() 30 | 31 | res, err := feign.DefaultFeign.App("MS-REGISTRY").R(). 32 | SetHeaders(map[string]string{ 33 | "Content-Type": "application/json", 34 | }).Get("/eureka/apps/WM") 35 | if err != nil { 36 | log.Println("err=", err.Error()) 37 | return 38 | } 39 | 40 | log.Println("res=", string(res.Body())) 41 | 42 | select {} 43 | } 44 | -------------------------------------------------------------------------------- /feign/log.go: -------------------------------------------------------------------------------- 1 | package feign 2 | 3 | import ( 4 | . "log" 5 | "runtime" 6 | "fmt" 7 | "strings" 8 | ) 9 | 10 | const ( 11 | LevelDebug = 1 12 | LevelInfo = 2 13 | LevelError = 3 14 | ) 15 | 16 | type LogFunc func(level int, format string, a ...interface{}) 17 | 18 | var log LogFunc 19 | 20 | func SetLogger(logFunc LogFunc) { 21 | log = logFunc 22 | } 23 | 24 | func (t LogFunc) Debugf(format string, a ...interface{}) { 25 | t(LevelDebug, format, a...) 26 | } 27 | 28 | func (t LogFunc) Infof(format string, a ...interface{}) { 29 | t(LevelInfo, format, a...) 30 | } 31 | 32 | func (t LogFunc) Errorf(format string, a ...interface{}) { 33 | t(LevelError, format, a...) 34 | } 35 | 36 | func init() { 37 | if log == nil { 38 | log = func(level int, format string, a ...interface{}) { 39 | switch level { 40 | case LevelDebug: 41 | format = "[debug] " + format 42 | case LevelInfo: 43 | format = "[info] " + format 44 | case LevelError: 45 | format = "[error] " + format 46 | } 47 | 48 | funcName, file, line, _ := runtime.Caller(2) 49 | fullFuncName := runtime.FuncForPC(funcName).Name() 50 | arr := strings.Split(fullFuncName, "/") 51 | arrFile := strings.Split(file, "/") 52 | 53 | Printf(fmt.Sprintf("%s %s:%d ", arr[len(arr)-1], arrFile[len(arrFile)-1], line)+format, a...) 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /samples/sample_mixed_use.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/HikoQiu/go-feign/feign" 5 | "github.com/HikoQiu/go-eureka-client/eureka" 6 | "log" 7 | ) 8 | 9 | func main() { 10 | 11 | // 1.1 configure maps, app => app's urls 12 | feign.DefaultFeign.UseUrls(map[string][]string{ 13 | "MS-REGISTRY-URLS": {"http://192.168.20.236:9001", "http://192.168.20.237:9001"}, 14 | }) 15 | 16 | // 1.2 use discovery client 17 | config := eureka.GetDefaultEurekaClientConfig() 18 | config.UseDnsForFetchingServiceUrls = true 19 | config.Region = "region-cn-hd-1" 20 | config.AvailabilityZones = map[string]string{ 21 | "region-cn-hd-1": "zone-cn-hz-1", 22 | } 23 | config.EurekaServerDNSName = "dev.ms-registry.xf.io" 24 | config.EurekaServerUrlContext = "eureka" 25 | config.EurekaServerPort = "9001" 26 | eureka.DefaultClient.Config(config). 27 | Register("APP_ID_CLIENT_FROM_DNS", 9000). 28 | Run() 29 | 30 | // 2.1 request by registry apps 31 | log.Println("----> request by registy apps") 32 | // assign a eureka client explicitly, or use the eureka.DefaultClient by default. 33 | //feign.DefaultFeign.UseDiscoveryClient(eureka.DefaultClient) 34 | res, err := feign.DefaultFeign.App("MS-REGISTRY").R().SetHeaders(map[string]string{ 35 | "Content-Type": "application/json", 36 | }).Get("/eureka/apps/WM") 37 | if err != nil { 38 | log.Println("err=", err.Error()) 39 | return 40 | } 41 | 42 | log.Println("res=", string(res.Body())) 43 | 44 | // 2.2 request by configuration apps 45 | log.Println("----> request by configuration apps") 46 | res, err = feign.DefaultFeign.App("MS-REGISTRY-URLS").R().SetHeaders(map[string]string{ 47 | "Content-Type": "application/json", 48 | }).Get("/eureka/apps/WM") 49 | if err != nil { 50 | log.Println("err=", err.Error()) 51 | return 52 | } 53 | 54 | log.Println("res=", string(res.Body())) 55 | 56 | select {} 57 | } 58 | -------------------------------------------------------------------------------- /feign/feign_test.go: -------------------------------------------------------------------------------- 1 | package feign 2 | 3 | import ( 4 | "testing" 5 | "github.com/HikoQiu/go-eureka-client/eureka" 6 | ) 7 | 8 | func Test_AppUrls(t *testing.T) { 9 | config := eureka.GetDefaultEurekaClientConfig() 10 | config.UseDnsForFetchingServiceUrls = true 11 | config.Region = "region-cn-hd-1" 12 | config.AvailabilityZones = map[string]string{ 13 | "region-cn-hd-1": "zone-cn-hz-1", 14 | } 15 | config.EurekaServerDNSName = "dev.ms-registry.xf.io" 16 | config.EurekaServerUrlContext = "eureka" 17 | config.EurekaServerPort = "9001" 18 | 19 | // run eureka client async 20 | eureka.DefaultClient.Config(config). 21 | Register("APP_ID_CLIENT_FROM_DNS", 9000). 22 | Run() 23 | 24 | lbc := DefaultFeign.App("MS-REGISTRY").R() 25 | res, err := lbc.SetHeaders(map[string]string{ 26 | "Content-Type": "application/json", 27 | }).Get("/eureka/apps/WM") 28 | if err != nil { 29 | log.Errorf("err=%s", err.Error()) 30 | } 31 | //log.Debugf("res=%v", res) 32 | 33 | res, err = DefaultFeign.App("MS-REGISTRY").R().SetHeaders(map[string]string{ 34 | "Content-Type": "application/json", 35 | }).Get("/eureka/apps/SVR-CHANNEL") 36 | if err != nil { 37 | log.Errorf("err=%s", err.Error()) 38 | } 39 | 40 | //log.Debugf("res=%v", res) 41 | 42 | res, err = DefaultFeign.App("MS-REGISTRY").R().SetHeaders(map[string]string{ 43 | "Content-Type": "application/json", 44 | }).Get("/eureka/apps/SVR-CHANNEL") 45 | if err != nil { 46 | log.Errorf("err=%s", err.Error()) 47 | } 48 | 49 | //log.Debugf("res=%v", res) 50 | 51 | res, err = DefaultFeign.App("MS-REGISTRY").R().SetHeaders(map[string]string{ 52 | "Content-Type": "application/json", 53 | }).Get("/eureka/apps/SVR-CHANNEL") 54 | if err != nil { 55 | log.Errorf("err=%s", err.Error()) 56 | } 57 | 58 | log.Debugf("res=%v", res) 59 | 60 | res, err = DefaultFeign.App("NOT_EXIST_SERVICE").R().SetHeaders(map[string]string{ 61 | "Content-Type": "application/json", 62 | }).Get("/eureka/apps/SVR-CHANNEL") 63 | if err != nil { 64 | log.Errorf("err=%s", err.Error()) 65 | } 66 | 67 | //res, err = DefaultFeign.App("SVR-CHANNEL").R().SetHeaders(map[string]string{ 68 | // "Content-Type": "application/json", 69 | //}).Get("/eureka/apps/SVR-CHANNEL") 70 | //if err != nil { 71 | // log.Errorf("err=%s", err.Error()) 72 | //} 73 | 74 | log.Debugf("res=%v", res) 75 | 76 | } 77 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### Go-feign 2 | 3 | Non-offical implementation of Spring Cloud feign. Tips: Non-full-features, only some basic and useful features implemented. 4 | 5 | Use [go-eureka-client](https://github.com/HikoQiu/go-eureka-client) together, coz go-feign uses go-eureka-client to discover registry apps and gets all apps' instances. 6 | 7 | 8 | Supported features. 9 | 10 | #### Sample 1 11 | 12 | Request urls from configuration. 13 | 14 | ```` 15 | // 1.1 configure maps, app => app's urls 16 | feign.DefaultFeign.UseUrls(map[string][]string{ 17 | "MS-REGISTRY-URLS": {"http://192.168.20.236:9001", "http://192.168.20.237:9001"}, 18 | }) 19 | 20 | // 2.1 Use app name to balancely request all instances 21 | res, err := feign.DefaultFeign.App("MS-REGISTRY-URLS").R().SetHeaders(map[string]string{ 22 | "Content-Type": "application/json", 23 | }).Get("/eureka/apps/MS-REGISTRY") 24 | if err != nil { 25 | log.Println("err: ", err.Error()) 26 | return 27 | } 28 | log.Println("res=", string(res.Body())) 29 | ```` 30 | 31 | Full sample code, refer to: [samples/sample_use_urls.go](./samples/sample_use_urls.go) 32 | 33 | #### Sample 2 34 | 35 | Use discovery client to get all registry apps, and then request app's urls from registry balancely. 36 | 37 | ```` 38 | // use go-eureka-client to communicate with registry 39 | runEurekaClient() 40 | 41 | // feign.DefaultFeign use eureka.DefaultClient to get registry apps 42 | res, err := feign.DefaultFeign.App("MS-REGISTRY").R(). 43 | SetHeaders(map[string]string{ 44 | "Content-Type": "application/json", 45 | }).Get("/eureka/apps/WM") 46 | if err != nil { 47 | log.Println("err=", err.Error()) 48 | return 49 | } 50 | 51 | log.Println("res=", string(res.Body())) 52 | 53 | ```` 54 | 55 | Full sample code, refer to: [samples/sample_use_discover_client.go](./samples/sample_use_discover_client.go) 56 | 57 | #### Sample 3 58 | 59 | Mixed use of UseUrls() and UseDiscoveryClient(). 60 | 61 | ```` 62 | // 1.1 configure maps, app => app's urls 63 | feign.DefaultFeign.UseUrls(map[string][]string{ 64 | "MS-REGISTRY-URLS": {"http://192.168.20.236:9001", "http://192.168.20.237:9001"}, 65 | }) 66 | 67 | // 1.2 use discovery client 68 | config := eureka.GetDefaultEurekaClientConfig() 69 | config.UseDnsForFetchingServiceUrls = true 70 | config.Region = "region-cn-hd-1" 71 | config.AvailabilityZones = map[string]string{ 72 | "region-cn-hd-1": "zone-cn-hz-1", 73 | } 74 | config.EurekaServerDNSName = "dev.ms-registry.xf.io" 75 | config.EurekaServerUrlContext = "eureka" 76 | config.EurekaServerPort = "9001" 77 | eureka.DefaultClient.Config(config). 78 | Register("APP_ID_CLIENT_FROM_DNS", 9000). 79 | Run() 80 | 81 | // 2.1 request by registry apps 82 | log.Println("----> request by registy apps") 83 | // assign a eureka client explicitly, or use the eureka.DefaultClient by default. 84 | //feign.DefaultFeign.UseDiscoveryClient(eureka.DefaultClient) 85 | res, err := feign.DefaultFeign.App("MS-REGISTRY").R().SetHeaders(map[string]string{ 86 | "Content-Type": "application/json", 87 | }).Get("/eureka/apps/WM") 88 | if err != nil { 89 | log.Println("err=", err.Error()) 90 | return 91 | } 92 | 93 | log.Println("res=", string(res.Body())) 94 | 95 | // 2.2 request by configuration apps 96 | log.Println("----> request by configuration apps") 97 | res, err = feign.DefaultFeign.App("MS-REGISTRY-URLS").R().SetHeaders(map[string]string{ 98 | "Content-Type": "application/json", 99 | }).Get("/eureka/apps/WM") 100 | if err != nil { 101 | log.Println("err=", err.Error()) 102 | return 103 | } 104 | 105 | log.Println("res=", string(res.Body())) 106 | ```` 107 | 108 | ### Sample screenshots 109 | 110 | ![Sample pic](sample01.jpeg) 111 | -------------------------------------------------------------------------------- /feign/feign.go: -------------------------------------------------------------------------------- 1 | package feign 2 | 3 | import ( 4 | "github.com/HikoQiu/go-eureka-client/eureka" 5 | "strings" 6 | "net/url" 7 | "time" 8 | "sync" 9 | "gopkg.in/resty.v1" 10 | ) 11 | 12 | const ( 13 | DEFAULT_REFRESH_APP_URLS_INTERVALS = 30 14 | ) 15 | 16 | var DefaultFeign = &Feign{ 17 | discoveryClient: eureka.DefaultClient, 18 | appUrls: make(map[string][]string), 19 | appNextUrlIndex: make(map[string]*uint32), 20 | } 21 | 22 | type Feign struct { 23 | // Discovery client to get Apps and instances 24 | discoveryClient eureka.DiscoveryClient 25 | 26 | // assign app => urls 27 | appUrls map[string][]string 28 | 29 | // Counter for calculate next url'index 30 | appNextUrlIndex map[string]*uint32 31 | 32 | // seconds of updating app's urls periodically 33 | refreshAppUrlsIntervals int 34 | 35 | // ensure some daemon task only run one time 36 | once sync.Once 37 | 38 | mu sync.RWMutex 39 | } 40 | 41 | // use discovery client to get all registry app => instances 42 | func (t *Feign) UseDiscoveryClient(client eureka.DiscoveryClient) *Feign { 43 | t.discoveryClient = client 44 | return t 45 | } 46 | 47 | // assign static app => urls 48 | func (t *Feign) UseUrls(appUrls map[string][]string) *Feign { 49 | t.mu.Lock() 50 | defer t.mu.Unlock() 51 | 52 | //v := uint32(time.Now().UnixNano()) 53 | //appNextUrlIndex[t.app] = &v 54 | for app, urls := range appUrls { 55 | 56 | // reset app'urls 57 | tmpAppUrls := make([]string, 0) 58 | for _, u := range urls { 59 | _, err := url.Parse(u) 60 | if err != nil { 61 | log.Errorf("Invalid url=%s, parse err=%s", u, err.Error()) 62 | continue 63 | } 64 | 65 | tmpAppUrls = append(tmpAppUrls, u) 66 | } 67 | 68 | if len(tmpAppUrls) == 0 { 69 | log.Errorf("Empty valid urls for app=%s, skip to set app's urls", app) 70 | continue 71 | } 72 | 73 | t.appUrls[app] = tmpAppUrls 74 | if t.appNextUrlIndex[app] == nil { 75 | v := uint32(time.Now().UnixNano()) 76 | t.appNextUrlIndex[app] = &v 77 | } 78 | } 79 | 80 | return t 81 | } 82 | 83 | func (t *Feign) SetRefreshAppUrlsIntervals(intervals int) { 84 | t.refreshAppUrlsIntervals = intervals 85 | } 86 | 87 | // return resty.Client 88 | func (t *Feign) App(app string) *resty.Client { 89 | defer func() { 90 | if err := recover(); err != nil { 91 | log.Errorf("App(%s) catch panic err=%v", app, err) 92 | } 93 | }() 94 | 95 | // daemon to update app urls periodically 96 | // only execute once globally 97 | t.once.Do(func() { 98 | if t.discoveryClient == nil || 99 | t.discoveryClient.GetInstance() == nil { 100 | log.Infof("no discovery client, no need to update appUrls periodically.") 101 | return 102 | } 103 | 104 | t.updateAppUrlsIntervals() 105 | }) 106 | 107 | // try update app's urls. 108 | // if app's urls is exist, do nothing 109 | t.tryRefreshAppUrls(app) 110 | 111 | lbc := &Lbc{ 112 | feign: t, 113 | app: app, 114 | } 115 | return lbc.pick().client 116 | } 117 | 118 | // try update app's urls 119 | // if app's urls is exist, do nothing 120 | func (t *Feign) tryRefreshAppUrls(app string) { 121 | if _, ok := t.GetAppUrls(app); ok { 122 | return 123 | } 124 | 125 | if t.discoveryClient == nil || 126 | len(t.discoveryClient.GetRegistryApps()) == 0 { 127 | log.Debugf("no discovery client, no need to update app'urls.") 128 | return 129 | } 130 | 131 | t.updateAppUrls() 132 | } 133 | 134 | // update app urls periodically 135 | func (t *Feign) updateAppUrlsIntervals() { 136 | if t.refreshAppUrlsIntervals <= 0 { 137 | t.refreshAppUrlsIntervals = DEFAULT_REFRESH_APP_URLS_INTERVALS 138 | } 139 | 140 | go func() { 141 | for { 142 | t.updateAppUrls() 143 | 144 | time.Sleep(time.Second * time.Duration(t.refreshAppUrlsIntervals)) 145 | log.Debugf("Update app urls interval...ok") 146 | for app, urls := range t.appUrls { 147 | log.Debugf("app=> %s, urls => %v", app, urls) 148 | } 149 | 150 | } 151 | }() 152 | } 153 | 154 | // Update app urls from registry apps 155 | func (t *Feign) updateAppUrls() { 156 | registryApps := t.discoveryClient.GetRegistryApps() 157 | tmpAppUrls := make(map[string][]string) 158 | 159 | for app, appVo := range registryApps { 160 | var isAppAlreadyExist bool 161 | var curAppUrls []string 162 | var isUpdate bool 163 | 164 | // if app is already exist in t.appUrls, check whether app's urls are updated. 165 | // if app's urls are updated, t.appUrls 166 | if curAppUrls, isAppAlreadyExist = t.GetAppUrls(app); isAppAlreadyExist { 167 | if len(curAppUrls) != len(appVo.Instances) { 168 | isUpdate = true 169 | }else { 170 | for _, insVo := range appVo.Instances { 171 | isExist := false 172 | for _, v := range curAppUrls { 173 | insHomePageUrl := strings.TrimRight(insVo.HomePageUrl, "/") 174 | if v == insHomePageUrl { 175 | isExist = true 176 | break 177 | } 178 | } 179 | 180 | if !isExist { 181 | isUpdate = true 182 | break 183 | } 184 | } 185 | } 186 | } 187 | 188 | // app are not exist in t.appUrls or app's urls has been update 189 | if !isAppAlreadyExist || isUpdate { 190 | tmpAppUrls[app] = make([]string, 0) 191 | for _, insVo := range appVo.Instances { 192 | tmpAppUrls[app] = append(tmpAppUrls[app], strings.TrimRight(insVo.HomePageUrl, "/")) 193 | } 194 | } 195 | } 196 | 197 | // update app's urls to feign 198 | t.UseUrls(tmpAppUrls) 199 | } 200 | 201 | // get app's urls 202 | func (t *Feign) GetAppUrls(app string) ([]string, bool) { 203 | t.mu.RLock() 204 | defer t.mu.RUnlock() 205 | 206 | if _, ok := t.appUrls[app]; !ok { 207 | return nil, false 208 | } 209 | 210 | return t.appUrls[app], true 211 | } 212 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------