A lightweight http server
Find a file
minemobs 32710110a3
Added a warning in README.md
And made code block inside of collapsible blocks
2023-04-16 19:45:38 +02:00
.github/workflows Update gradle.yml 2023-02-27 21:09:38 +01:00
gradle/wrapper Added tests, and stuff (I don't remember) 2023-02-10 15:44:40 +00:00
src Added regex support 2023-03-05 15:09:45 +00:00
.gitattributes Added tests, and stuff (I don't remember) 2023-02-10 15:44:40 +00:00
.gitignore Added tests, and stuff (I don't remember) 2023-02-10 15:44:40 +00:00
build.gradle Added regex support 2023-03-05 15:09:45 +00:00
gradlew Added tests, and stuff (I don't remember) 2023-02-10 15:44:40 +00:00
gradlew.bat Added tests, and stuff (I don't remember) 2023-02-10 15:44:40 +00:00
LICENSE Initial commit 2023-02-09 20:45:56 +01:00
README.md Added a warning in README.md 2023-04-16 19:45:38 +02:00
settings.gradle Added tests, and stuff (I don't remember) 2023-02-10 15:44:40 +00:00
Taskfile.yml Added tests, and stuff (I don't remember) 2023-02-10 15:44:40 +00:00

PicoHTTP

A lightweight http server

⚠️ YOU SHOULD NOT use this library in production due to https://github.com/thesimpleteam/picohttp/issues/2.

Install

Using Gradle
repositories {
  //...
  maven {
    url 'https://maven.thesimpleteam.net/snapshots'
  }
}

dependencies {
  //...
  implementation "net.thesimpleteam:picoHTTP:1.3-SNAPSHOT"
}

Usage

Usage
public class Server {
  public static void main(String[] args) {
    try(PicoHTTP http = new PicoHTTP(8080)) {
      http.addRoutes(Server.class, new Server());
      http.addRoute("/test.js", (client) -> client.send(200, "OK", ContentTypes.JS, "console.log('Hello World')"));
      http.run();
      while(true) {} //It's a way to avoid closing the server
    }
  }

  //Automatically added
  @Path("/")
  public void helloWorld(Client client) throws IOException {
    client.send(200, "Ok", ContentTypes.PLAIN, "Hello World");
  }

  @Path(value = "/", method = HTTPMethods.POST)
  public void postExamle(Client client) throws IOException {
    String data = client.data();
    String contentType = client.getHeaders().get("Content-Type");
    //Your code
    client.send(501, "Not Implemented");
  }

  @Path("/hello/\\w+") //Regex example
  public void helloSomeone(Client client) throws IOException {
    String name = client.path().split("/")[2]; //When you split the path it should return something like {"", "hello", "(theName)"}
    client.send(200, "Ok", ContentTypes.PLAIN, "Hello " + name);
  }
}