dhruvrajvanshi / Moonshine

Web framework for Crystal language [DEPRECATED in favour of kemal]
MIT License
121 stars 11 forks source link

Mix matched types #27

Closed 5nyper closed 8 years ago

5nyper commented 8 years ago

here is my code:

require "moonshine"
require "./renders.cr"
include Moonshine
include Moonshine::Utils::Shortcuts
include Moonshine::Base

app = App.new(static_dirs = ["public"])
vc = 0
app.define do

    get "/", do |req|
        vc += 1
        ok(Cal.new(vc).to_s)
    end
    get "/updates", do |req|
        File.open("public/js/up.js", "w") { |f|
        f << Uptime.new(`uptime`.gsub(/\n/, "")).to_s
        }
        ok(File.read("views/up.html"))

    end
    get "/passgen", do |req|
        text = ""
        str = "ABDECGFHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!$%&"
        (0..22).each do
        text += str[rand(str.size)]
        end
        ok(Pass.new(text).to_s)
    end
end
app.run(80)

and here is the error:

in ./server.cr:11: instantiating 'get(String)'                                                                                                                                                        

 get "/", do |req|                                                                                                                                                                                    
 ^~~                                                                                                                                                                                                  

in macro 'macro_54783264' /home/vikaton/Crystal/cubaa/src/libs/moonshine/moonshine/base/app.cr:94, line 3:                                                                                            

  1.                                                                                                                                                                                                  
  2.       def get(path, &block : Request -> Response)                                                                                                                                                
  3.         @routes[Route.new("get".upcase, path.to_s)] = block                                                                                                                                      
  4.         self                                                                                                                                                                                     
  5.       end                                                                                                                                                                                        
  6.                                                                                                                                                                                                  
  7.       def post(path, &block : Request -> Response)                                                                                                                                               
  8.         @routes[Route.new("post".upcase, path.to_s)] = block                                                                                                                                     
  9.         self                                                                                                                                                                                     
 10.       end                                                                                                                                                                                        
 11.                                                                                                                                                                                                  
 12.       def put(path, &block : Request -> Response)                                                                                                                                                
 13.         @routes[Route.new("put".upcase, path.to_s)] = block                                                                                                                                      
 14.         self                                                                                                                                                                                     
 15.       end                                                                                                                                                                                        
 16.                                                                                                                                                                                                  
 17.       def delete(path, &block : Request -> Response)                                                                                                                                             
 18.         @routes[Route.new("delete".upcase, path.to_s)] = block                                                                                                                                   
 19.         self                                                                                                                                                                                     
 20.       end                                                                                                                                                                                        
 21.                                                                                                                                                                                                  
 22.       def patch(path, &block : Request -> Response)                                                                                                                                              
 23.         @routes[Route.new("patch".upcase, path.to_s)] = block                                                                                                                                    
 24.         self                                                                                                                                                                                     
 25.       end                                                    
       @routes[Route.new("get".upcase, path.to_s)] = block                                                                                                                                           
               ^                                                                                                                                                                                      

no overload matches 'Array(String)#[]=' with types Moonshine::Base::Route, (Moonshine::Http::Request -> Moonshine::Http::Response)                                                                    
Overloads are:                                                                                                                                                                                        
 - Array(String)#[]=(index : Int, count : Int, value : String)                                                                                                                                        
 - Array(String)#[]=(index : Int, count : Int, values : Array(T))                                                                                                                                     
 - Array(String)#[]=(index : Int, value : String)                                                                                                                                                     
 - Array(String)#[]=(range : Range(Int, Int), value : String)                                                                                                                                         
 - Array(String)#[]=(range : Range(Int, Int), values : Array(T))       

Its been a while since I compiled my server since Crystal was 0.7.1, so I had to fix alot of problem :( Not sure how to deal with this one though.

dhruvrajvanshi commented 8 years ago

This is caused by passing static_routes to App.new. I had removed the static file handling from base app because that should be implemented using Middleware. I haven't yet written middleware for static file handling. I'll do it on the weekend. You'll need to do it yourself if you need it earlier. Sorry about that (I have a day job). Basically, you need to make a class that inherits from Moonshine::Middleware::Base. Implement it's process_request method (it's type is Request -> Response?). You can match the request url with your static dirs. If url doesn't look like "/*", you should return nil so that the other routes handle the request. In other cases, read the file and return the response (or 404 if not found). Tell me if you need further help. P.S. If you do it, make sure you put it in a repo so everyone can use it. Thanks.

dhruvrajvanshi commented 8 years ago

I've added Moonshine::Utils::StaticDirs middleware class. Call it with a url prefix and some directories to serve the directories at that path

app.middleware_object(Moonshine::Utils::StaticDirs.new("static", ["directory/path"])