All Projects → rakyll → Statik

rakyll / Statik

Licence: apache-2.0
Embed files into a Go executable

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Statik

Nodemcu Espress
Ultra-Lightweight and modular Node.js express like http server for NodeMCU. web - ESP8266
Stars: ✭ 39 (-98.84%)
Mutual labels:  static, http-server
Binserve
A blazingly fast static web server with routing, templating, and security in a single binary you can set up with zero code. ⚡️🦀
Stars: ✭ 401 (-88.02%)
Mutual labels:  static, http-server
tinyhttp
🦕 Deno port of tinyhttp, 0-legacy, tiny & fast web framework
Stars: ✭ 84 (-97.49%)
Mutual labels:  http-server
Webserver
A C++ High Performance Web Server
Stars: ✭ 4,164 (+24.37%)
Mutual labels:  http-server
Pegasus.lua
🚀 Pegasus.lua is an http server to work with web applications written in Lua language.
Stars: ✭ 274 (-91.82%)
Mutual labels:  http-server
graphql-ssg
GraphQL data based Static Site Generator.
Stars: ✭ 30 (-99.1%)
Mutual labels:  static
Firefly
Firefly is an asynchronous web framework for rapid development of high-performance web application.
Stars: ✭ 277 (-91.73%)
Mutual labels:  http-server
node-split-file
🌱 NodeJS Module to split and merge files for several purposes like transporting over unstable networks.
Stars: ✭ 33 (-99.01%)
Mutual labels:  files
Oak
A middleware framework for handling HTTP with Deno 🐿️ 🦕
Stars: ✭ 3,799 (+13.47%)
Mutual labels:  http-server
Copy
Go copy directory recursively
Stars: ✭ 264 (-92.11%)
Mutual labels:  files
Usrefl
Header-only, tiny (99 lines) and powerful C++20 static reflection library.
Stars: ✭ 287 (-91.43%)
Mutual labels:  static
Vedetta
OpenBSD Router Boilerplate
Stars: ✭ 260 (-92.23%)
Mutual labels:  http-server
Python Atomicwrites
Powerful Python library for atomic file writes.
Stars: ✭ 253 (-92.44%)
Mutual labels:  files
Beast
HTTP and WebSocket built on Boost.Asio in C++11
Stars: ✭ 3,241 (-3.2%)
Mutual labels:  http-server
mapdns
A simple, static DNS server.
Stars: ✭ 78 (-97.67%)
Mutual labels:  static
Armeria
Your go-to microservice framework for any situation, from the creator of Netty et al. You can build any type of microservice leveraging your favorite technologies, including gRPC, Thrift, Kotlin, Retrofit, Reactive Streams, Spring Boot and Dropwizard.
Stars: ✭ 3,392 (+1.31%)
Mutual labels:  http-server
weborf
Shares files using the HTTP protocol. Provides CLI and GUI. Allows using webdav.
Stars: ✭ 38 (-98.86%)
Mutual labels:  http-server
Framework
Swoole, PSR-15, PSR-7, PSR-11 lightweight modular anti-framework for REST micro-services.
Stars: ✭ 259 (-92.26%)
Mutual labels:  http-server
Http
Host These Things Please - a basic http server for hosting a folder fast and simply
Stars: ✭ 275 (-91.79%)
Mutual labels:  http-server
Jetty.project
Eclipse Jetty® - Web Container & Clients - supports HTTP/2, HTTP/1.1, HTTP/1.0, websocket, servlets, and more
Stars: ✭ 3,260 (-2.63%)
Mutual labels:  http-server

statik

Build Status

statik allows you to embed a directory of static files into your Go binary to be later served from an http.FileSystem.

Is this a crazy idea? No, not necessarily. If you're building a tool that has a Web component, you typically want to serve some images, CSS and JavaScript. You like the comfort of distributing a single binary, so you don't want to mess with deploying them elsewhere. If your static files are not large in size and will be browsed by a few people, statik is a solution you are looking for.

Usage

Install the command line tool first.

go get github.com/rakyll/statik

statik is a tiny program that reads a directory and generates a source file that contains its contents. The generated source file registers the directory contents to be used by statik file system.

The command below will walk on the public path and generate a package called statik under the current working directory.

$ statik -src=/path/to/your/project/public

The command below will filter only files on listed extensions.

$ statik -include=*.jpg,*.txt,*.html,*.css,*.js

In your program, all your need to do is to import the generated package, initialize a new statik file system and serve.

import (
  "github.com/rakyll/statik/fs"

  _ "./statik" // TODO: Replace with the absolute import path
)

  // ...

  statikFS, err := fs.New()
  if err != nil {
    log.Fatal(err)
  }
  
  // Serve the contents over HTTP.
  http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(statikFS)))
  http.ListenAndServe(":8080", nil)

Visit http://localhost:8080/public/path/to/file to see your file.

You can also read the content of a single file:

import (
  "github.com/rakyll/statik/fs"

  _ "./statik" // TODO: Replace with the absolute import path
)

  // ...

  statikFS, err := fs.New()
  if err != nil {
    log.Fatal(err)
  }
  
  // Access individual files by their paths.
  r, err := statikFS.Open("/hello.txt")
  if err != nil {
    log.Fatal(err)
  }    
  defer r.Close()
  contents, err := ioutil.ReadAll(r)
  if err != nil {
    log.Fatal(err)
  }

  fmt.Println(string(contents))

There is also a working example under example directory, follow the instructions to build and run it.

Note: The idea and the implementation are hijacked from camlistore. I decided to decouple it from its codebase due to the fact I'm actively in need of a similar solution for many of my projects.

Deterministic output

By default, statik includes the "last modified" (mtime) time on files that it packs. This allows an HTTP FileServer to present the correct file modification times to clients.

However, if you have a continuous integration task that checks that your checked-in static files in a git repository match the code that is generated on your CI system, you'll run into a problem: The mtime on the git checkout does not match what you have locally, causing tests to fail.

You can fix the test in one of two ways:

  1. In CI, manually set the mtime on the freshly checked out tree: here's a stackoverflow answer that provides a shell command to do that; or,
  2. Instruct statik not to store the "last modified" time.

To ignore the last modified time, use the -m to statik, like so:

$ statik -m -include=*.jpg,*.txt,*.html,*.css,*.js

Note that this will cause http.FileServer to consider the file to always have changed & serve it with a "Last-Modified" of the time of the request.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].