@@ -23,10 +23,11 @@ const (
2323 DATA_FILE_URL = "https://s3.amazonaws.com/alexa-static/top-1m.csv.zip"
2424)
2525
26+ var a alexa
27+
2628type alexa struct {
2729 config config
2830 alexaList map [string ]int
29- output output
3031}
3132
3233type config struct {
@@ -36,12 +37,12 @@ type config struct {
3637 DataFileURL string
3738}
3839
39- type output struct {
40+ type outputAlexa struct {
4041 Web string `json:"web"`
4142 Rank int `json:"rank"`
4243}
4344
44- func ( a * alexa ) Router (w http.ResponseWriter , r * http.Request ) {
45+ func Router (w http.ResponseWriter , r * http.Request ) {
4546 params := strings .Split (strings .ToLower (r .URL .Path ), "/" )
4647 path := params [1 :len (params )]
4748 if path [len (path )- 1 ] == "" { // remove last empty slot after /
@@ -52,38 +53,41 @@ func (a *alexa) Router(w http.ResponseWriter, r *http.Request) {
5253 u .BadRequest (w , r )
5354 return
5455 }
56+ oa := outputAlexa {
57+ Web : "" ,
58+ Rank : 0 ,
59+ }
5560 r .ParseForm ()
56- web : = r .Form .Get ("web" )
57- if web == "" {
61+ oa . Web = r .Form .Get ("web" )
62+ if oa . Web == "" {
5863 u .BadRequest (w , r )
5964 return
6065 }
61- a .doAlexaRequest (w , web )
66+ oa .doAlexaRequest (w )
6267}
6368
64- func (a * alexa ) doAlexaRequest (w http.ResponseWriter , web string ) {
65- a .output .Web = web
66- a .output .Rank = a .alexaList [a .output .Web ]
67- if a .output .Rank != 0 {
68- u .SendJSONToClient (w , a .output , 200 )
69+ func (oa * outputAlexa ) doAlexaRequest (w http.ResponseWriter ) {
70+ oa .Rank = a .alexaList [oa .Web ]
71+ if oa .Rank != 0 {
72+ u .SendJSONToClient (w , oa , 200 )
6973 return
7074 }
71- if strings .HasPrefix (a . output .Web , "www." ) {
72- a . output . Web = a . output . Web [4 :len (a . output .Web )]
73- a . output . Rank = a .alexaList [a . output .Web ]
74- if a . output .Rank != 0 {
75- u .SendJSONToClient (w , a . output , 200 )
75+ if strings .HasPrefix (oa .Web , "www." ) {
76+ oa . Web = oa . Web [4 :len (oa .Web )]
77+ oa . Rank = a .alexaList [oa .Web ]
78+ if oa .Rank != 0 {
79+ u .SendJSONToClient (w , oa , 200 )
7680 return
7781 }
7882 }
79- if ! strings .HasPrefix (a . output .Web , "www." ) {
80- a . output . Rank = a .alexaList ["www." + a . output .Web ]
81- if a . output .Rank != 0 {
82- u .SendJSONToClient (w , a . output , 200 )
83+ if ! strings .HasPrefix (oa .Web , "www." ) {
84+ oa . Rank = a .alexaList ["www." + oa .Web ]
85+ if oa .Rank != 0 {
86+ u .SendJSONToClient (w , oa , 200 )
8387 return
8488 }
8589 }
86- msg := fmt .Sprintf ("%s not in alexa top 1 million" , a . output .Web )
90+ msg := fmt .Sprintf ("%s not in alexa top 1 million" , oa .Web )
8791 u .ErrorResponse (w , msg )
8892 return
8993}
@@ -108,7 +112,8 @@ func (a *alexa) loadDataInMemory() {
108112 }
109113}
110114
111- func (a * alexa ) OnceADayTask () {
115+ func OnceADayTask () {
116+ a = newAlexa (false )
112117 t := time .Now ()
113118 n := time .Date (t .Year (), t .Month (), t .Day (), 3 , 10 , 10 , 0 , t .Location ())
114119 d := n .Sub (t )
@@ -157,7 +162,7 @@ func (a *alexa) unzipCsv() {
157162 }
158163}
159164
160- func NewAlexa (test bool ) alexa {
165+ func newAlexa (test bool ) alexa {
161166 a := alexa {
162167 config : config {
163168 DataFilePath : DATA_FILE_PATH ,
@@ -166,10 +171,6 @@ func NewAlexa(test bool) alexa {
166171 DataFileURL : DATA_FILE_URL ,
167172 },
168173 alexaList : make (map [string ]int ),
169- output : output {
170- Web : "" ,
171- Rank : 0 ,
172- },
173174 }
174175 if test {
175176 a .config .DataFilePath = DATA_FILE_PATH_TEST
0 commit comments