Created
January 5, 2013 18:53
-
-
Save tomnomnom/4463052 to your computer and use it in GitHub Desktop.
POC Go web server for online CV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net/http" | |
"html/template" | |
"runtime" | |
"os" | |
"os/signal" | |
"syscall" | |
"log" | |
) | |
type Page struct { | |
DevMode bool | |
} | |
var templates = template.Must(template.ParseFiles("cv.html")) | |
func cvHandler(w http.ResponseWriter, r *http.Request) { | |
p := newPage() | |
err := templates.ExecuteTemplate(w, "cv.html", p) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
} | |
func procSetup() { | |
// We only ever want one process | |
runtime.GOMAXPROCS(1) | |
} | |
var mode = os.Getenv("CV_MODE") | |
func newPage() *Page { | |
devMode := false | |
if mode == "dev" { | |
devMode = true | |
} | |
p := &Page{DevMode: devMode} | |
return p | |
} | |
func signalHandler() { | |
c := make(chan os.Signal) | |
signal.Notify(c, syscall.SIGTERM, syscall.SIGINT, syscall.SIGHUP) | |
sig := <-c | |
switch sig { | |
case syscall.SIGTERM: | |
log.Println("Cought SIGTERM; shutting down") | |
case syscall.SIGINT: | |
log.Println("Cought SIGINT; shutting down") | |
case syscall.SIGHUP: | |
log.Println("Cought SIGHUP; shutting down") | |
} | |
os.Exit(0) | |
} | |
func main() { | |
go signalHandler() | |
procSetup() | |
http.HandleFunc("/", cvHandler) | |
http.HandleFunc("/index.html", cvHandler) | |
http.HandleFunc("/index.php", cvHandler) | |
http.Handle("/css/", http.FileServer(http.Dir("."))) | |
http.Handle("/images/", http.FileServer(http.Dir("."))) | |
log.Println("Server starting") | |
http.ListenAndServe(":8080", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment