Code critique requested - Golang Web Server

Hello, I’ve put together a first attempt at a web server written in Golang. It is designed to server a site using HTTPS so it uses port 443 (I’ve used the CAP_NET_BIND_SERVICE to grant low numbered port access to my process). I’m also trying to use the acme/autocert library to provide the necessary HTTPS encryption. The problem is that it doesn’t work at the moment. This is on my test Windows box at home and on my leased Linux box (code has config for both). In both cases I’m getting a refusal to run error. Before I start ripping the code apart, I thought I would post it here to seen if any of you bright sparks can see any glaringly obvious errors. The code is at https://github.com/carlca/webserver
Thanks in advance for any help,
Carl

Can you elaborate on ‘refusal to run’? Surely you receive a specific error message?

You could also try setcap on your binary directly and run it manually:

setcap 'cap_net_bind_service=+ep' /path/to/program

Things to try first:

  • Remove the file serving, for now just serve hello world.
  • Serve on a high port, verify that works.
  • Serve with known certs, verify that works on a low port running manually
  • Add the autocert stuff, verify that works.

NB for autocert to work you must be running on the ip which corresponds to your domain, so 46.101.21.192 in this case.

From a quick glance I don’t see anything wrong, but here is some minimal example code which might help as you can compare it:

Thanks a lot, Kenny. You’re right about breaking the code down into smaller, hopefully working chunks. I had planned to do this, though as I say, I thought someone might take one look at my code as it is, and say “that’s your problem”. It’s great that you have posted working code with the same acme/autocert stuff as mine, though I can see some, hopefully significant differences already. I’ll give it some serious study, probably Saturday now, tonight is relaxation night! Thanks again, I’ll let you know how I get on, and I’ll see if I can discover the exact error message.
Cheers,
Carl

1 Like

Oh BTW, Kudos to you for the https://golangnews.com/ site. There’s a wealth of informative stuff there.

1 Like

For learning about Go, what you’re doing is a great exercise. However, if you’re interested in a great Go based web server with instant HTTPS (Let’s Encrypt), you could also check out Caddy. It has a lot of features, actively developed and is open source.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.