76 lines
1.6 KiB
Go
76 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"html/template"
|
|
"log"
|
|
"net"
|
|
"net/http"
|
|
)
|
|
|
|
func StartServer(configuration BlogConfiguration, blog *Blog) {
|
|
|
|
mux := http.NewServeMux()
|
|
/* Handle home page (/): list of blog entries */
|
|
mux.HandleFunc("GET /{$}",
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
log.Printf("%+v", r.Header)
|
|
tpl, err := template.New("index").Parse(indexTemplate)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
blog.mutex.Lock()
|
|
defer blog.mutex.Unlock()
|
|
err = tpl.ExecuteTemplate(w, "index", blog)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
})
|
|
/* Handle one post display(/post/{post ID}) */
|
|
mux.HandleFunc("GET /post/{id}",
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
id := r.PathValue("id")
|
|
post, found := blog.GetPost(id)
|
|
if !found {
|
|
w.WriteHeader(404)
|
|
}
|
|
tpl, err := template.New("entry").Parse(entryTemplate)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
err = tpl.ExecuteTemplate(w, "entry", post)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
})
|
|
|
|
mux.HandleFunc("GET /styles.css",
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Add("Content-Type", "text/css")
|
|
w.Write([]byte(nativeCSS))
|
|
})
|
|
|
|
mux.HandleFunc("GET /favicon.ico",
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
log.Println("favicon.ico")
|
|
w.WriteHeader(200)
|
|
})
|
|
|
|
addr := net.JoinHostPort(configuration.Service.Address, configuration.Service.Port)
|
|
|
|
server := &http.Server{
|
|
Addr: addr,
|
|
Handler: mux,
|
|
}
|
|
|
|
server.ListenAndServe()
|
|
//http.ListenAndServe(addr, server)
|
|
}
|
|
|
|
/*
|
|
func isCodeServerProxy(r *http.Request) bool {
|
|
return len(r.Header.Get("code-server-session")) > 0
|
|
}
|
|
*/
|