Pastey A longer example of Euphoria MVC
- Posted by ghaberek (admin) Mar 09, 2019
include mvc/app.e include mvc/database.e include mvc/db_mysql.e include mvc/session.e include mvc/template.e include std/error.e include std/map.e constant CONFIG_FILE = "config.ini" -- -- [database] -- protocol = mysql -- hostname = localhost -- database = appdb -- username = appuser -- password = P_ssw0rd! -- function app_start() if load_config( CONFIG_FILE ) = 0 then return 1 end if sequence protocol = get_config( "database", "protocol", "mysql" ) sequence hostname = get_config( "database", "hostname", "localhost" ) sequence database = get_config( "database", "database" ) sequence username = get_config( "database", "username" ) sequence password = get_config( "database", "password" ) if find( "", {database,username,password} ) then -- one of these is blank! return 1 end if -- build the connection string, e.g. -- "mysql://appuser:P_ssw0rd!@localhost/appdb" sequence connectionString = sprintf( "%s://%s:%s@%s/%s", {protocol,username,password,hostname,database} ) if db_connect( connectionString ) = 0 then return 1 end if -- we're going to use sessions, so load -- the session with every run of the app start_session() -- return 0 to keep going return 0 end function insert_hook( HOOK_APP_START, "app_start" ) function app_end() db_disconnect() return 0 end function insert_hook( HOOK_APP_END, "app_end" ) function index( object request ) integer logged_in = get_session( "logged_in" ) integer user_id = get_session( "user_id" ) if not logged_in then return redirect( "/login" ) end if if db_query( "SELECT username, display_name FROM users WHERE user_id = %d", {user_id} ) = 0 then set_session( "logged_in", FALSE ) return redirect( "/login" ) end if object result = db_fetch() sequence username = map:get( result, "username" ) sequence display_name = map:get( result, "display_name" ) object response = map:new() map:put( response, "logged_in", logged_in ) map:put( response, "username", username ) map:put( response, "display_name", display_name ) return render_template( "index.html", response ) end function app:route( "/", "index" ) function login( object request ) sequence request_method = map:get( request, "REQUEST_METHOD" ) if equal( request_method, "POST" ) then -- user is trying to log in sequence username = map:get( request, "username" ) sequence password = map:get( request, "password" ) if db_query( "SELECT user_id, password_hash, password_salt FROM users WHERE username = '%s'", {username} ) = 0 then set_session( "error_message", "Invalid username or password" ) return redirect( url_for("login") ) end if object result = db_fetch() sequence user_id = map:get( result, "id" ) sequence password_hash = map:get( result, "password_hash" ) sequence password_salt = map:get( result, "password_salt" ) -- always store your passwords with salt -- they'll last longer! sequence attempt_hash = get_password_hash( password, password_salt ) if not equal( attempt_hash, password_hash ) then set_session( "error_message", "Invalid username or password" ) return redirect( url_for("login") ) end if set_session( "logged_in", TRUE ) set_session( "user_id", user_id ) return redirect( url_for("index") ) end if return render_template( "login.html" ) end function app:route( "/login", "login" ) function logout( object request ) integer logged_in = get_session( "logged_in" ) if logged_in then set_session( "logged_in", FALSE ) set_session( "user_id", 0 ) end if return redirect( url_for("index") ) end function app:route( "/logout", "logout" ) app:run()


