vladimirok5959 / golang-server-static

Http helper for static files

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ARCHIVED: because Go has build in mechanism for including any static file into compiled application, google for import '_ "embed"' and '//go:embed'

golang-server-static

Http helper for static files

How to use

go get github.com/vladimirok5959/golang-server-static
package main

import (
	"net/http"

	"github.com/vladimirok5959/golang-server-static/static"
)

func main() {
	stat := static.New("index.html")
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		if stat.Response("./htdocs", w, r, nil, nil) {
			return
		}
		w.WriteHeader(http.StatusNotFound)
		w.Header().Set("Content-Type", "text/html; charset=utf-8")
		w.Write([]byte(`<div>Error 404!</div>`))
	})

	http.ListenAndServe(":8080", nil)
}

About

Http helper for static files

License:MIT License


Languages

Language:Go 95.6%Language:HTML 3.5%Language:Makefile 0.9%