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

vladimirok5959/golang-server-static

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)
}