16
									
								
								gorilla.go
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								gorilla.go
									
									
									
									
									
								
							| @@ -36,24 +36,24 @@ func (g *Generator) gorillaGenerate(component string, plugin *protogen.Plugin) e | |||||||
| 		gfile.Import(microApiPackage) | 		gfile.Import(microApiPackage) | ||||||
| 		gfile.Import(gorillaMuxPackage) | 		gfile.Import(gorillaMuxPackage) | ||||||
|  |  | ||||||
| 		gfile.P("func RegisterHandlers(r *mux.Router, h interface{}, eps []*micro_api.Endpoint) error {") | 		gfile.P("func RegisterHandlers(r *", gorillaMuxPackage.Ident("Router"), ", h interface{}, eps []*", microApiPackage.Ident("Endpoint"), ") error {") | ||||||
| 		gfile.P("v := reflect.ValueOf(h)") | 		gfile.P("v := ", reflectPackage.Ident("ValueOf"), "(h)") | ||||||
| 		gfile.P("if v.NumMethod() < 1 {") | 		gfile.P("if v.NumMethod() < 1 {") | ||||||
| 		gfile.P(`return fmt.Errorf("handler has no methods: %T", h)`) | 		gfile.P(`return `, fmtPackage.Ident("Errorf"), `("handler has no methods: %T", h)`) | ||||||
| 		gfile.P("}") | 		gfile.P("}") | ||||||
| 		gfile.P("for _, ep := range eps {") | 		gfile.P("for _, ep := range eps {") | ||||||
| 		gfile.P(`idx := strings.Index(ep.Name, ".")`) | 		gfile.P(`idx := `, stringsPackage.Ident("Index"), `(ep.Name, ".")`) | ||||||
| 		gfile.P("if idx < 1 || len(ep.Name) <= idx {") | 		gfile.P("if idx < 1 || len(ep.Name) <= idx {") | ||||||
| 		gfile.P(`return fmt.Errorf("invalid api.Endpoint name: %s", ep.Name)`) | 		gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid `, microApiPackage.Ident("Endpoint"), ` name: %s", ep.Name)`) | ||||||
| 		gfile.P("}") | 		gfile.P("}") | ||||||
| 		gfile.P("name := ep.Name[idx+1:]") | 		gfile.P("name := ep.Name[idx+1:]") | ||||||
| 		gfile.P("m := v.MethodByName(name)") | 		gfile.P("m := v.MethodByName(name)") | ||||||
| 		gfile.P("if !m.IsValid() || m.IsZero() {") | 		gfile.P("if !m.IsValid() || m.IsZero() {") | ||||||
| 		gfile.P(`return fmt.Errorf("invalid handler, method %s not found", name)`) | 		gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler, method %s not found", name)`) | ||||||
| 		gfile.P("}") | 		gfile.P("}") | ||||||
| 		gfile.P("rh, ok := m.Interface().(func(http.ResponseWriter, *http.Request))") | 		gfile.P("rh, ok := m.Interface().(func(", httpPackage.Ident("ResponseWriter"), ", *", httpPackage.Ident("Request"), "))") | ||||||
| 		gfile.P("if !ok {") | 		gfile.P("if !ok {") | ||||||
| 		gfile.P(`return fmt.Errorf("invalid handler: %#+v", m.Interface())`) | 		gfile.P(`return `, fmtPackage.Ident("Errorf"), `("invalid handler: %#+v", m.Interface())`) | ||||||
| 		gfile.P("}") | 		gfile.P("}") | ||||||
| 		gfile.P("r.HandleFunc(ep.Path[0], rh).Methods(ep.Method...).Name(ep.Name)") | 		gfile.P("r.HandleFunc(ep.Path[0], rh).Methods(ep.Method...).Name(ep.Name)") | ||||||
| 		gfile.P("}") | 		gfile.P("}") | ||||||
|   | |||||||
							
								
								
									
										2
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								main.go
									
									
									
									
									
								
							| @@ -18,7 +18,7 @@ var ( | |||||||
|  |  | ||||||
| func init() { | func init() { | ||||||
| 	flagDebug = flags.Bool("debug", false, "") | 	flagDebug = flags.Bool("debug", false, "") | ||||||
| 	flagComponents = flags.String("components", "micro", "") | 	flagComponents = flags.String("components", "micro|rpc", "") | ||||||
| 	flagPaths = flag.String("paths", "", "") | 	flagPaths = flag.String("paths", "", "") | ||||||
| 	flagModule = flag.String("module", "", "") | 	flagModule = flag.String("module", "", "") | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user