Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Latest commit

 

History

History
62 lines (52 loc) · 1.56 KB

README.md

File metadata and controls

62 lines (52 loc) · 1.56 KB

PicoHTTP

A lightweight http server

⚠️ YOU SHOULD NOT use this library in production due to #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);
  }
}