This an example of a simple HTTP server written in Go. It handles GET requests, matches the URL path to a function, and returns a HTML response from a global variable.
We are using the small FastHTTP library that adds a worker pool of goroutines for faster response time.
package main
import (
"github.com/valyala/fasthttp"
)
var x1 = `<!DOCTYPE html>
<html lang="en">
<head>
<title>This is a Website</title>
</head>
<body>
Xor The World!
</body>
</html>`
func main() {
h0 := func(ctx *fasthttp.RequestCtx) {
switch string(ctx.Path()) {
case "/":
ctx.SetContentType("text/html; charset=utf8")
ctx.WriteString(x1)
default:
ctx.SetStatusCode(404)
}
}
s := &fasthttp.Server{
Name: "XOR Server",
Handler: h0,
}
s.ListenAndServe(":80")
}
You can compile the program for any OS, and run it on the background. In Linux for example you will run it as nohup ./server > /dev/null 2>&1 & where standard output from the server log gets redirected to dev/null.