Git Product home page Git Product logo

tinyweb's Introduction

TinyWeb

TinyWeb is a simple http server library for Android.

For settings.gradle

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven { url 'https://jitpack.io' }
    }
}

For settings.gradle.kts

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven("https://jitpack.io")
    }
}

Install gradle dependencies

dependencies {
    implementation 'com.github.tejmagar:tinyweb:v1.0.1-alpha'
}

Creating a View

Create public class for each route. Note: if the class is not public, the server may not behave as expected.

import androidx.annotation.NonNull;

import org.tinyweb.request.Request;
import org.tinyweb.response.HttpResponse;
import org.tinyweb.response.Response;
import org.tinyweb.views.View;

public class HomeView extends View {
    @NonNull
    @Override
    public Response response(Request request) {
        return new HttpResponse("Hello World");
    }
}

Creating a server instance

    // Other imports...

    import org.tinyweb.Server;
    import org.tinyweb.TinyWebLogging;
    import org.tinyweb.paths.Path;

    public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            // To see logs
            // TinyWebLogging.enableLogging = true;
            Server server = new Server(8080);
            server.addRoute(new Path("/", HomeView.class));
            server.run();
        }
    }

Handling form upload

TinyWeb currently supports only multipart/form-data. Other encoding types will be added soon.

import androidx.annotation.NonNull;

import org.tinyweb.forms.Files;
import org.tinyweb.parsers.MultipartParser;
import org.tinyweb.request.Request;
import org.tinyweb.response.HttpResponse;
import org.tinyweb.response.Response;
import org.tinyweb.stream.Stream;
import org.tinyweb.views.View;

import java.io.IOException;

public class HomeView extends View {
    @NonNull
    @Override
    public Response response(Request request) {
        if (request.method.equals("POST")) {
            MultipartParser multipartParser = new MultipartParser(request.headers, request.stream);
            try {
                MultipartParser.ParseResult parseResult = multipartParser.parse();
                Files.TempFile tempFile = parseResult.files.value("file");

                return new HttpResponse(parseResult.formData.toString() +
                        "\n" + "filename: " + tempFile.fileName + " path: " + tempFile.file.getAbsolutePath());
            } catch (MultipartParser.MultipartParseException | Stream.StreamReadException |
                     IOException e) {
                return new HttpResponse(e.getMessage());
            }
        }
        return new HttpResponse("Hello World");
    }
}

Tips

Running server in foreground service helps to serve pages in background even after closing app.

tinyweb's People

Contributors

tejmagar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

meimingle it212

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.