Create go backend for site SSR and Dockerfile for dockerization
This commit is contained in:
83
main.go
Normal file
83
main.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
type HeaderData struct {
|
||||
MarqueeItems []MarqueeItem `toml:"marquee_items"`
|
||||
Links Links `toml:"links"`
|
||||
}
|
||||
type MarqueeItem struct {
|
||||
Text string `toml:"text"`
|
||||
URL template.URL `toml:"url"`
|
||||
}
|
||||
type Links struct {
|
||||
TopLevel []Link `toml:"top_level"`
|
||||
Categories []LinkCategory `toml:"categories"`
|
||||
}
|
||||
type Link struct {
|
||||
Name string `toml:"name"`
|
||||
URL template.URL `toml:"url"`
|
||||
}
|
||||
type LinkCategory struct {
|
||||
Name string `toml:"name"`
|
||||
MainLink string `toml:"main_link"`
|
||||
Links []Link `toml:"links"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
HeaderData HeaderData `toml:"header_data"`
|
||||
}
|
||||
|
||||
type PageData struct {
|
||||
Header HeaderData
|
||||
Body template.HTML
|
||||
Posts []Post
|
||||
Post Post
|
||||
StatusCode int
|
||||
StatusMessage string
|
||||
}
|
||||
|
||||
var debug *bool
|
||||
var configDir string
|
||||
var contentDir string
|
||||
|
||||
var config Config
|
||||
|
||||
func main() {
|
||||
debug = flag.Bool("debug", false, "Run in debug mode")
|
||||
var httpAddress string
|
||||
flag.StringVar(&httpAddress, "address", ":8080", "HTTP address to listen on")
|
||||
flag.StringVar(&configDir, "config", "", "The directory containing the server config")
|
||||
flag.StringVar(&contentDir, "content", "", "The directory containing content")
|
||||
flag.Parse()
|
||||
|
||||
_, err := toml.DecodeFile(filepath.Join(configDir, "config.toml"), &config)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
mux := &http.ServeMux{}
|
||||
|
||||
mux.HandleFunc("/", notFoundHandler)
|
||||
mux.HandleFunc("/{$}", homeHandler)
|
||||
mux.HandleFunc("/page/{page}", staticPageHandler)
|
||||
mux.HandleFunc("/page/{category}/{page}", staticCategoryHandler)
|
||||
mux.HandleFunc("/blog/{post}", postHandler)
|
||||
mux.HandleFunc("/blog", blogHandler)
|
||||
fs := http.FileServer(http.Dir(filepath.Join(contentDir, "static")))
|
||||
mux.Handle("/static/", http.StripPrefix("/static/", fs))
|
||||
mux.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, filepath.Join(contentDir, "/static/favicon.ico"))
|
||||
})
|
||||
err = http.ListenAndServe(httpAddress, mux)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user