Title photo
frugal technology, simple living and guerrilla large-appliance repair

Regular blog here, 'microblog' there

Many of my traditional blog post live on this site, but a great majority of my social-style posts can be found on my much-busier microbloging site at updates.passthejoe.net. It's busier because my BlogPoster "microblogging" script generates short, Twitter-style posts from the Linux or Windows (or anywhere you can run Ruby with too many Gems) command line, uploads them to the web server and send them out on my Twitter and Mastodon feeds.

I used to post to this blog via scripts and Unix/Linux utilities (curl and Unison) that helped me mirror the files locally and on the server. Since this site recently moved hosts, none of that is set up. I'm just using SFTP and SSH to write posts and manage the site.

Disqus comments are not live just yet because I'm not sure about what I'm going to do for the domain on this site. I'll probably restore the old domain at first just to have some continuity, but for now I like using the "free" domain from this site's new host, NearlyFreeSpeech.net.

Sun, 05 Jul 2015

Learning Go: create a web server in five lines

The documentation for Go (aka Golang) is peppered with examples, and one of those examples, for Go's net/http package, shows you how to easily create a file server.

net/http is part of Go's standard library Here is the example code from golang.org/pkg/net/http:

package main

import (
    "log"
    "net/http"
)

func main() {
    // Simple static webserver:
    log.Fatal(http.ListenAndServe(":8080", http.FileServer(http.Dir("/usr/share/doc"))))
}

When you drop this code into a file in a directory (in my case I made the directory web_server and named the file main.go), then either compile it with go build or run it with go run, it creates a web server on port 8080 that serves the contents of your /usr/share/doc directory, which always exists in Linux and Unix (and probably in the Mac OS X version of Unix).

To see the results, open a web browser and go to http://localhost:8080/, and you should see a directory listing. Just like any web page, you can click on the links and see what's in those files.

This example program -- a web server in five lines -- is fun to play around with. You can change the http.Dir and serve "real" web content. You can change the port from :8080 to something else.