c2c0c0576cdf624b018adfabe13ae1497402957b.svn-base 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "github.com/longjoy/micro-go-course/section32/config"
  7. "github.com/longjoy/micro-go-course/section32/endpoint"
  8. "github.com/longjoy/micro-go-course/section32/service"
  9. "github.com/longjoy/micro-go-course/section32/transport"
  10. "net/http"
  11. "os"
  12. "os/signal"
  13. "strconv"
  14. "syscall"
  15. )
  16. func main() {
  17. var (
  18. servicePort = flag.Int("service.port", 10099, "service port")
  19. )
  20. flag.Parse()
  21. ctx := context.Background()
  22. errChan := make(chan error)
  23. var tokenService service.ResourceServerTokenService
  24. var tokenEnhancer service.TokenEnhancer
  25. var tokenStore service.TokenStore
  26. var srv service.Service
  27. tokenEnhancer = service.NewJwtTokenEnhancer("secret")
  28. tokenStore = service.NewJwtTokenStore(tokenEnhancer.(*service.JwtTokenEnhancer))
  29. tokenService = service.NewTokenService(tokenStore, tokenEnhancer)
  30. srv = service.NewCommonService()
  31. indexEndpoint := endpoint.MakeIndexEndpoint(srv)
  32. sampleEndpoint := endpoint.MakeSampleEndpoint(srv)
  33. sampleEndpoint = endpoint.MakeOAuth2AuthorizationMiddleware(config.KitLogger)(sampleEndpoint)
  34. adminEndpoint := endpoint.MakeAdminEndpoint(srv)
  35. adminEndpoint = endpoint.MakeOAuth2AuthorizationMiddleware(config.KitLogger)(adminEndpoint)
  36. adminEndpoint = endpoint.MakeAuthorityAuthorizationMiddleware("Admin", config.KitLogger)(adminEndpoint)
  37. //创建健康检查的Endpoint
  38. healthEndpoint := endpoint.MakeHealthCheckEndpoint(srv)
  39. endpts := endpoint.OAuth2Endpoints{
  40. HealthCheckEndpoint: healthEndpoint,
  41. IndexEndpoint: indexEndpoint,
  42. SampleEndpoint:sampleEndpoint,
  43. AdminEndpoint:adminEndpoint,
  44. }
  45. //创建http.Handler
  46. r := transport.MakeHttpHandler(ctx, endpts, tokenService, config.KitLogger)
  47. go func() {
  48. config.Logger.Println("Http Server start at port:" + strconv.Itoa(*servicePort))
  49. handler := r
  50. errChan <- http.ListenAndServe(":" + strconv.Itoa(*servicePort), handler)
  51. }()
  52. go func() {
  53. c := make(chan os.Signal, 1)
  54. signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
  55. errChan <- fmt.Errorf("%s", <-c)
  56. }()
  57. error := <-errChan
  58. config.Logger.Println(error)
  59. }