GithubHelp home page GithubHelp logo

sauldoescode / transplacer Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 80 KB

it reads files & suspends them in memory for performant serving/access

License: Mozilla Public License 2.0

Go 100.00%
http2 cache assetcache static serving memory-database http2-push push

transplacer's Introduction

GoDoc

Features

  • auto http2 push, when available
  • auto watching and updating
  • gzips and sets etags
  • works concurrently, no deadlocks
  • it's a bit of rough magic & ducktape, but, it works

Diy AssetCache for super fast static asset serving straight from the memory

package main

import (
  "log"
  "net/http"
  "time"

  tr "github.com/SaulDoesCode/transplacer"
)

func main() {
  cache, err := tr.Make(&tr.AssetCache{
    Dir:     "./assets",
    Watch:   true,
    Expire:  time.Minute * 30,
    DevMode: true, // extra logs
  })
  if err != nil {
    panic(err.Error())
  }
  defer cache.Close()

  server := &http.Server{
    Addr:    ":http",
    Handler: cache,
  }

  log.Fatal(server.ListenAndServe())
}

With Echo

package main

import (
  "time"

  tr "github.com/SaulDoesCode/transplacer"
  "github.com/labstack/echo"
)

func main() {
  cache, err := tr.Make(&tr.AssetCache{
    Dir:    "./assets",
    Watch:  true,
    Expire: time.Minute * 30,
  })
  if err != nil {
    panic(err.Error())
  }
  defer cache.Close()

  e := echo.New()

  e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
    return func(c echo.Context) error {
      req := c.Request()
      err := next(c)
      if err == nil || req.Method[0] != 'G' {
        return err
      }

      return cache.Serve(c.Response().Writer, req)
    }
  })

  e.Logger.Fatal(e.Start(":http"))
}

transplacer's People

Contributors

sauldoescode avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

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.