Skip to content

sadeemhub/sadeem-web-framework

Repository files navigation

Sadeem web framework 

vertx based web framework

https://github.com/micromax/sadeem-egypt--web-framework

http://sadeem-egypt.com

An java web framework by sadeem-egypt.com , lightweight, builtin server, from developer to developers.

gradle project so you will be happy with it

small footprint support Java  Kotlin and scala .

MVC pattern 

very strong built-in web server i could handle 35k request per second based on vertx.io 

databse driver for MYsql is included

ORM and database migration included thanks to Ebean ORM , you could also use your own ORM .

akka java actor famework included

Non blocking :-) 

 

you find example for Controller and Models and view 

 

 

Recommended documentation

you could start build Web app , realtime app and IoT GetWay's or even blockchan , without reading and doc's for based technologys 

1 - https://vertx.io/ 

          Eclipse Vert.x is a tool-kit for building reactive applications on the JVM

 

 

2 - https://akka.io/ 

Akka, a set of open-source libraries for designing scalable, resilient systems that span processor cores and networks. Akka allows you to focus on meeting business needs instead of writing low-level code to provide reliable behavior, fault tolerance, and high performance.

Many common practices and accepted programming models do not address important challenges inherent in designing systems for modern computer architectures. To be successful, distributed systems must cope in an environment where components crash without responding, messages get lost without a trace on the wire, and network latency fluctuates. These problems occur regularly in carefully managed intra-datacenter environments - even more so in virtualized architectures.

 

3 - https://ebean.io/

 

    ORM 

 Ebean provides a simple programming model that developers can understand and master quickly.

 

 

4 - https://freemarker.apache.org/

 

Apache FreeMarker™ is a template engine: a Java library to generate text output (HTML web pages, e-mails, configuration files, source code, etc.) based on templates and changing data. Templates are written in the FreeMarker Template Language (FTL), which is a simple, specialized language (not a full-blown programming language like PHP). Usually, a general-purpose programming language (like Java) is used to prepare the data (issue database queries, do business calculations). Then, Apache FreeMarker displays that prepared data using templates. In the template you are focusing on how to present the data, and outside the template you are focusing on what data to present.

 

 

 

Get Start 

 

will clone & open Examples 

1 - import Gradle Project to your IDE

PS it may take time and if you need Scala you have to install it to your machine and enable it as plugin in Gradle file by uncomment it

Config Files

  • application.properties
  • database.properties
  • route.properties
  • application.properties

    this file hold main config for Host and Port Ebean(ORM) conf ... etc change port for what you want 9999 for example

    run Gradle task Build from IDEA or command Line
    thank hit run task 
    or Run boot.java (this main class )
    

    you are ready now go to http://localhost:8977/Hellojava

    if it's work than can go also to Kotlin example http://localhost:8977/HelloKotlin

    if every thing is Ok now you need a real life example

    CRUD example

    create database i use Mysql user your own or even use H2 bout don't forget to add your driver in Gradle file

    Make your database Config correct at this files

  • /resources/application.properties
  • /resources/application.yaml
  • /resources/database.properties
  • also you need to config same files in project root 'used when i need configuration outside the Jar '

  • application.properties
  • database.properties
  • First if you know OOP and MVC concepts you are good to go else {read about them first }

    Code First

    if need to go and build your database Like old school it's ok go and do it

    otherwise under app.models created file call Admin.java
          package app.models;
          import javax.persistence.Entity;
        import javax.persistence.Id;
        import javax.persistence.Table;
        import java.sql.Timestamp;
    
        import io.ebean.Ebean;
        import io.ebean.Model;
        import io.ebean.annotation.Index;
        @Entity
        @Table(name="admin")
        public class Admin extends Model {
        
        
            @Id
            public long userId;
        
        
            @Index(unique=true)
            public String userName;
        
            public String userPassword;
        
            public Timestamp dateCreated;
        
            public String lastIP;
        
            public String token;
        
            public Timestamp lastLogin;
        
            public String getAPI_token() {
                return API_token;
            }
        
            public void setAPI_token(String API_token) {
                this.API_token = API_token;
            }
        
            public String API_token;
        
            public String getAPI_Key() {
                return API_Key;
            }
        
            public void setAPI_Key(String API_Key) {
                this.API_Key = API_Key;
            }
        
            public String API_Key;
        
            public Admin() {
                super();
            }
        
            public long getUserId() {
                return userId;
            }
        
            public void setUserId(Long userId) {
                this.userId = userId;
            }
        
            public String getUserName() {
                return userName;
            }
        
            public void setUserName(String userName) {
                this.userName = userName;
            }
        
            public String getUserPassword() {
                return userPassword;
            }
        
            public void setUserPassword(String userPassword) {
                this.userPassword = userPassword;
            }
        
            public Timestamp getDateCreated() {
                return dateCreated;
            }
        
            public void setDateCreated(Timestamp dateCreated) {
                this.dateCreated = dateCreated;
            }
        
            public String getLastIP() {
                return lastIP;
            }
        
            public void setLastIP(String lastIP) {
                this.lastIP = lastIP;
            }
        
            public String getToken() {
                return token;
            }
        
            public void setToken(String token) {
                this.token = token;
            }
        
            public Timestamp getLastLogin() {
                return lastLogin;
            }
        
            public void setLastLogin(Timestamp lastLogin) {
                this.lastLogin = lastLogin;
            }
        
            public Admin  finder(long id){
                return Ebean.find(Admin.class , id);
        
            }
    

    your find this file and it's related files in repo code

    database migration

    if you did't use Mysql go to DBmarge file and change database config to what you are using

    than run DBmarge it will create migration file than restart your server you will see the table admin :-)

        public class DBmarge {
           public static void main(String arg[]){
                try {
                    DbMigration dbMigration = DbMigration.create();
                    dbMigration.setPlatform(Platform.MYSQL); // change to your database type
                    dbMigration.generateMigration();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    

    Models Helper and finder

    in models we create 2 dir called 
    1 - logic // for CRUD helpers 
    2 - finder // to search and finde 
    

    in logic create Admin Helper by creating AdminHelper.java

      package app.models.logic;
      
      import app.models.Admin;
      import com.cloudsgen.system.core.DataBase.CGModel;
      import com.cloudsgen.system.core.DataBase.ICrud;
      import io.ebean.Ebean;
      import io.vertx.ext.web.RoutingContext;
    
    
        
      public class AdminHelper extends CGModel implements ICrud {
      
            //the constractor with RoutingContext
             public AdminHelper(RoutingContext rx)
                {
                    super(rx);
                }
                
      }
    

    than we add CRUD method add , edit and delete

      @Override
          public void add(Object tClass) {
          if(tClass instanceof Admin)
          {
              Admin c = (Admin) tClass;
              c.save();
    
    
          }
      }
    
      @Override
      public void edit(Object id ) {
          if(id instanceof Admin) {
    
              Admin c = (Admin) id;
    
    
              c.update();
    
          }
      }
    
      @Override
      public void delete(Object id) {
    
    
              Admin c = Ebean.getDefaultServer().find(Admin.class).where().eq("user_id" , id).findOne();
             if(c != null) {
    
    
                 Ebean.getDefaultServer().delete(c);
             }
    
      }
    

    you will find the code in the Repo..

    now one More step we need a finder to search our data you could use Ebean query

    The code for that will be in "/models.finder"

    package app.models.finder;
    import app.models.Admin;
    import io.ebean.Ebean;
    import io.ebean.Finder;
    import java.util.List;
    
    public class AuthModel extends Finder<Long, Admin>  {
    
        private String Username;
        private String Password;
        private int isValid;
    
    
    
    
    
        public AuthModel(){
            super(Admin.class);
    
        }
        public Admin byId(long username)
        {
            return  query().where().eq("user_id" , username ).findOne();
        }
    
        public Admin byName(String username)
        {
            return  query().where().eq("user_name" , username ).findOne();
        }
    
    
        public Admin byNameAndPassword(String un , String pw) {
            return Ebean.getDefaultServer().find(Admin.class).where()
                    .eq("user_name", un)
                    .and()
                    .eq("user_password" , pw)
                    .findOne();
        }
    
    
        public Admin byIdAndTookenAndIp(long id , String token , String ip)  {
            return Ebean.getDefaultServer().find(Admin.class).where()
                    .eq("user_id", id)
                    .and()
                    .eq("token" , token)
                    .and()
                    .eq("last_ip" , ip)
                    .findOne();
        }
    
        public Admin byAPI( String token , String key) {
            return Ebean.getDefaultServer().find(Admin.class).where()
                    .eq("api_token", token)
                    .and()
                    .eq("api_key" , key)
                    .findOne();
        }
        public List<Admin> GetAll()
        {
            return  query().findList();
        }
        public int GetAllCount()
        {
            return  query().findList().size();
        }
    
    }
    

    controller and router : HowTo

    create your controller in language you know Kotlin , java , Scala

    ps : scala controller should be in Scala Folder not in java 
    kotlin could be in same java class Folder 
    
            public class HelloJava extends CG_Controller {
            
            
              public HelloJava(RoutingContext rxtx) {
                    super(rxtx);
                }
            
                /**
                 *   index : this default method for every controller
                 *   Render :  will Print any thing to response
                 *   simply this will print $String "Hello world ! from java"
                 *
                 * @return void
                 * @throws IOException
                 */
                @Override
                public void index() throws IOException
                {
                    super.index();
                    Render("Hello world ! from java");
                }
            
            }
    

    this very basic controller we have to add it to our router File like this Hellojava = app.controller.HelloJava

    in app.controller.HelloJava you will find how to Render View and pass data to it

    how to USE GET , POST , REQUEST and Signal Methods to get users input :

    this.input.POST("var_name"); its take String Parameter with name in query

    this.input.GET("var_name"); its take String Parameter with name in query

    this.input.REQUEST("var_name"); its take String Parameter with name in query work with GET and POST requites

    this.input.Signal(1); its take Integer Parameter with offset for requites path like /controller/metho/data1/data2

    this.input.setCookie("Cookie_name", "Cookie_val"); set Cookie 2 Parameter , Key_name and Value

    this.input.GetCookie("Cookie_name"); Get Cookie 1 Parameter , Key_name return Value

    Redirect("path/to/redirect"); this method will Redirect user to a path of your chaise

    annotation for Auth and Preloading

    we have built-in annotation parser for authenticate some action like user requites and ... etc

    you will find example called AdminAuth in models/logic

    package app.models.logic;
    
    import app.models.Admin;
    import app.models.finder.AuthModel;
    import com.cloudsgen.system.core.DataBase.CGModel;
    import io.vertx.ext.web.RoutingContext;
    
    public class AdminAuth  extends CGModel {
    
    
    
    
    
    
        public AdminAuth(RoutingContext rx) {
            super(rx);
            String Tokin =  this.input.GetCookie("_cgmain");
            String UUID =  this.input.GetCookie("_cgalphas");
    
            String UserIp = this.input.getAgentIp();
            AuthModel authModel  = new AuthModel();
    
            Admin admin = null;
            if(Tokin == null || UUID == null)
            {
                this.setLook(true);
                Redirect("login");
            }else {
    
    
                long lid = 0;
    
                try {
                    lid = Long.parseLong(UUID);
                }catch (NumberFormatException ex)
                {
    
                   lid = 0 ;
                }
    
                try {
                    admin = authModel.byIdAndTookenAndIp(lid , Tokin , UserIp);
                    if(admin == null)
                    {
    
    
                        Redirect("login");
                    }else
                    {
                        this.setLook(false);
    
                    }
                }catch ( ExceptionInInitializerError e)
                {
                    Redirect("syserrors/misconf");
                }
    
    
            }
    
    
        }
    
    
    
    
    
    
    
    
    
    }
    

    Ok how to use to Auth any controller Simply like that "before controller class deceleration " or before method

    @Iauthentication(AuthModel = AdminAuth.class )
    public class Administration  extends CG_Controller {
    
    
    
        public Administration(RoutingContext rxtx) {
            super(rxtx);
        }
    
    
    
        @Override
        public void index() throws IOException {
        }
    }
    

    Views : HowTo

  • we use Freemarker as template engine
  • view located in /resources/views

    static files served from /resources/webroot

    called in view like that

    <link href="/static/plugins/bootstrap/css/bootstrap.css" rel="stylesheet">
    

    we scaffolding view's part in

    0 - /views/ 1 - /views/layout/ 2 - /views/partia/

    go to view and discover it and read Freemarker documentation for Var , Lists , Array , Loop it's literally will take no time to master it

    I hope this Help some one need fast and high performance APP the powerful point of this Work "The Built in Web server "

    TODO : add dashboard to control from it add more tutorial and library

    finally feel free to contact me

    linkedin

    m.elqrwash@sadeem-egypt.com

    m.elqrwash@gmail.com

    info@sadeem-egypt.com

     

     

     

    stay tuned 

     

    About

    vertx based web framework support java , kotlin , scala

    Resources

    License

    Stars

    Watchers

    Forks

    Releases

    No releases published

    Packages

    No packages published