@@ -14,10 +14,14 @@ import (
1414type ServerOptions struct {
1515 ListenAddress string
1616 Silent bool
17+
18+ GetOptions func () (Options , error )
1719}
1820
1921// Server creates http proxy server
2022func (proxy Proxy ) Server (options ServerOptions ) * http.Server {
23+ gin .SetMode ("release" )
24+
2125 router := gin .New ()
2226
2327 if options .Silent {
@@ -28,55 +32,60 @@ func (proxy Proxy) Server(options ServerOptions) *http.Server {
2832 router .Use (ginzap .RecoveryWithZap (logger , true ))
2933 }
3034
31- router .GET ("/:scope/:name" , proxy .getPackageHandler )
32- router .GET ("/:scope" , proxy .getPackageHandler )
33- router .NoRoute (proxy .noRouteHandler )
35+ router .GET ("/:scope/:name" , proxy .getPackageHandler ( options ) )
36+ router .GET ("/:scope" , proxy .getPackageHandler ( options ) )
37+ router .NoRoute (proxy .noRouteHandler ( options ) )
3438
3539 return & http.Server {
3640 Handler : router ,
3741 Addr : options .ListenAddress ,
3842 }
3943}
4044
41- func (proxy Proxy ) getPackageHandler (c * gin.Context ) {
42- pkg , err := proxy .GetCachedPath (c .Request .URL .Path , c .Request )
43-
44- if err != nil {
45- c .AbortWithError (500 , err )
46- } else {
47- // c.Header("Content-Encoding", "gzip")
48- c .Data (200 , "application/json" , pkg )
49- }
50- }
45+ func (proxy Proxy ) getPackageHandler (options ServerOptions ) gin.HandlerFunc {
46+ return func (c * gin.Context ) {
47+ options , err := options .GetOptions ()
5148
52- func (proxy Proxy ) getTarballHabdler (c * gin.Context ) {
53- pkg , err := proxy .GetCachedPath (c .Request .URL .Path , c .Request )
49+ if err != nil {
50+ c .AbortWithError (500 , err )
51+ } else {
52+ pkg , err := proxy .GetCachedPath (options , c .Request .URL .Path , c .Request )
5453
55- if err != nil {
56- c .AbortWithError (500 , err )
57- } else {
58- c .Data (200 , "application/json" , pkg )
54+ if err != nil {
55+ c .AbortWithError (500 , err )
56+ } else {
57+ c .Header ("Cache-Control" , "public, max-age=" + string (int (options .DatabaseExpiration .Seconds ())))
58+ c .Data (200 , "application/json" , pkg )
59+ }
60+ }
5961 }
6062}
6163
62- func (proxy Proxy ) noRouteHandler (c * gin.Context ) {
63- if strings .Contains (c .Request .URL .Path , ".tgz" ) {
64- proxy .getTarballHabdler (c )
65- } else if c .Request .URL .Path == "/" {
66- err := proxy .Database .Health ()
64+ func (proxy Proxy ) noRouteHandler (options ServerOptions ) gin.HandlerFunc {
65+ tarballHandler := proxy .getPackageHandler (options )
6766
68- if err != nil {
69- c . AbortWithStatusJSON ( 503 , err )
70- } else {
71- c . AbortWithStatusJSON ( 200 , gin. H { "ok" : true } )
72- }
73- } else {
74- options , err := proxy .GetOptions ()
67+ return func ( c * gin. Context ) {
68+ if strings . Contains ( c . Request . URL . Path , ".tgz" ) {
69+ // get tarball
70+ tarballHandler ( c )
71+ } else if c . Request . URL . Path == "/" {
72+ // get health
73+ err := proxy .Database . Health ()
7574
76- if err != nil {
77- c .AbortWithStatusJSON (500 , err )
75+ if err != nil {
76+ c .AbortWithStatusJSON (503 , err )
77+ } else {
78+ c .AbortWithStatusJSON (200 , gin.H {"ok" : true })
79+ }
7880 } else {
79- c .Redirect (http .StatusTemporaryRedirect , options .UpstreamAddress + c .Request .URL .Path )
81+ // redirect
82+ options , err := options .GetOptions ()
83+
84+ if err != nil {
85+ c .AbortWithStatusJSON (500 , err )
86+ } else {
87+ c .Redirect (http .StatusTemporaryRedirect , options .UpstreamAddress + c .Request .URL .Path )
88+ }
8089 }
8190 }
8291}
0 commit comments