3 ms·
I run a simple Go server behind Apache for my weekend project (http://www.thespanishsite.com http://www.thespanishsite.com). I started with this blog: http://
by melling 12y ago
I run a simple Go server behind Apache for my weekend project (http://www.thespanishsite.com http://www.thespanishsite.com). I started with this blog:
http://www.jeffreybolle.com/blog/run-google-go-web-apps-behind-apache http://www.jeffreybolle.com/blog/run-google-go-web-apps-behi...
I also use MySql on Digital Ocean with a $10/month droplet. The few issues at first where that I started with a $5/month which didn't enough RAM so I'd run out of memory until I created swap:
https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04 https://www.digitalocean.com/community/tutorials/how-to-add-...
Still need to make it a daemon, but I'm not finished. I have one big method to set up my pages. I could write a blog, github repo or create a summary page on my site, if there's any interest.
func runWeb() {
serveSingle("/robots.txt", "./robots.txt")
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("./css/"))))
http.Handle("/resources/", http.StripPrefix("/resources/", http.FileServer(http.Dir("./resources/"))))
http.Handle("/static", http.FileServer(http.Dir("./static/")))
http.HandleFunc("/chinese", chineseHomeHandler)
http.HandleFunc("/french", frenchHomeHandler)
http.HandleFunc("/chinese/numbers", chineseNumbersHomeHandler)
// Many handlers deleted ...
http.HandleFunc("/", homeHandler)
// http.ListenAndServe("localhost:9999", nil)
port := GetPort()
fmt.Println("listening...", port)
err := http.ListenAndServe(port, nil)
if err != nil {
panic(err)
}
}
/*
http://stackoverflow.com/questions/14086063/serve-homepage-and-static-content-from-root http://stackoverflow.com/questions/14086063/serve-homepage-a...
/
func serveSingle(pattern string, filename string) {
http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filename)
})
}
- melling 12y agoUPDATE: I can longer edit my original comment. I realize that a bunch of unformatted code on HN is not that useful so I created a quick starter Go Webserver on github: https://github.com/melling/GoWebServer https://github.com/melling/GoWebServer There are a couple of scripts to build and run the server.
- andrewksl 12y agoCheck out supervisor (http://supervisord.org/ http://supervisord.org/). Super painless daemons if you're like me and hate writing init scripts.
- lastofus 12y agoWhat was causing you to run out of RAM? I've been running a Django app w/ Postgres on a $5 droplet for months without memory issues, despite Django/Gunicorn being sort of a memory hog.