Skip to content
This repository was archived by the owner on Nov 1, 2025. It is now read-only.

01. Package Structure & Basic Server Configuration

NaveenB2004 edited this page Dec 25, 2024 · 3 revisions

Package Structure

When using Nexus-HTTP, the package structure is a somewhat major setting. The point that you get is, your project needs at least one package that contains the endpoint classes. If you use build systems like Maven, it's best practice to use the default package structure. So, let's get started by organizing our package structure!

In here, we will use the Maven project structure to demonstrations.

Nexus-HTTP-Examples
|- src
    |- main
        |- java
            |- io
                |- github
                    |- lycoriscafe
                        |- Main.java
        |- resources
    |- test
        |- java
        |- resources
|- pom.xml

Server Configuration

In this API, we got two main configuration classes.

  • HttpServerConfiguration.java (for HTTP servers)
    io.github.lycoriscafe.nexus.http.helper.configuration.HttpServerConfiguration.java
  • HttpsServerConfiguration.java (for HTTPS servers)
    io.github.lycoriscafe.nexus.http.helper.configuration.HttpsServerConfiguration.java

We need an instance of one of these classes (it depends on the server, HTTP or HTTPS) to pass in to the server class when it's initializing (next chapter).

The basePackage

When initializing an instance of mentioned configuration class, you will be asked to pass String argument called basePackage. It simply means the package level point, where the library should start the endpoint classes scanning. In the Package Structure section, we made a project with specific file structure. Let's assume that we're going to put our endpoint classes in a new package called endpoints. Then the argument we should pass is like this.

io.github.lycoriscafe.endpoints

So now our project structure will be like this.

io.github.lycoriscafe.endpoints     <- new package
io.github.lycoriscafe.Main.java     <- he was there already!

The advantage of this is, if you want to initialize more than one server in the same source, you will be able to divide endpoint classes for each server by making them in separate packages. Nice, ha!

The tempDirectory

Other common parameter asked by the configuration is String type tempDirectory. This is the location where the library will store the temporary files. Like endpoint database (if you change the config), incoming request contents, outgoing response contents, etc. Pass a directory path that has proper read and write permissions!

When initializing the server, the passed directory will be wiped out and re-created. Also, the user doesn't need to manually create the directory if it does not exist, the library will do the job.

Defining a configuration instance

In here, we aren't going to discuss how the HttpsServerConfiguration going to be. You can go to the examples repo ( see the footer!) and find good examples from there.

Let's create our configuration instance in the main method of Main.java generated in Package Structure section. Make sure you added the endpoints package described in The basePackage.

// since we're initializing an HTTP server
var httpServerConfiguration = new HttpServerConfiguration("io.github.lycoriscafe.endpoints", "NexusTemp");

Server Classes

We got two major HTTP server initialization classes that match to the configuration classes.

  • HttpServer.java (for HTTP servers)
    io.github.lycoriscafe.nexus.http.HttpServer.java
  • HttpsServer.java (for HTTPS servers)
    io.github.lycoriscafe.nexus.http.HttpsServer.java

Starting a server

In here, we aren't going to discuss how the HttpsServer going to be. You can go to the examples repo (see the footer!) and find good examples from there.

The default port used in HttpServerConfiguration is 80 and in HttpsServerConfiguration is 443.

To start the defined server, there is a method initialize().

// since we're initializing an HTTP server
var httpServer = new HttpServer(httpServerConfiguration);
httpServer.initialize();

After initialization, you will be able to see a log like this (the log can be different with your SLF4j logger settings).

2024-12-25 : 03:25:03 [main] tid=1 [INFO] org.reflections.Reflections - Reflections took 18 ms to scan 0 urls, producing 0 keys and 0 values
2024-12-25 : 03:25:03 [main] tid=1 [INFO] io.github.lycoriscafe.nexus.http.HttpServer - NEXUS-HTTP :: _____ _____ __ __ _____ _____
2024-12-25 : 03:25:03 [main] tid=1 [INFO] io.github.lycoriscafe.nexus.http.HttpServer - NEXUS-HTTP :: |   | |   __|  |  |  |  |   __|
2024-12-25 : 03:25:03 [main] tid=1 [INFO] io.github.lycoriscafe.nexus.http.HttpServer - NEXUS-HTTP :: | | | |   __|-   -|  |  |__   |
2024-12-25 : 03:25:03 [main] tid=1 [INFO] io.github.lycoriscafe.nexus.http.HttpServer - NEXUS-HTTP :: |_|___|_____|__|__|_____|_____| HTTP (API v1.0)
2024-12-25 : 03:25:03 [Nexus-HTTP@80] tid=29 [INFO] io.github.lycoriscafe.nexus.http.HttpServer - NEXUS-HTTP :: Server initialized @ 0.0.0.0/0.0.0.0:80

To check if the server is properly initialized, open a web browser and go to the URL http://localhost:80. You will get a message like this.

{
  "exception": "endpoint not found"
}

The server is now up and running. The error message is okay for now because we don't define any endpoints yet.

Congratulations! You successfully create an instance of server configuration with minimum settings! By the way, if you want more flexibility over your server, it's recommended to go through the HttpServerConfiguration/ HttpsServerConfiguration sources and Javadoc.

You Got This!


< Home | 02. Defining Endpoints >

Central Repository
Minimum JDK version
License

Copyright 2025 Lycoris Café

Clone this wiki locally