50 lines
1.4 KiB
Go
50 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
func readConfig(path string) (map[string]string, error) {
|
|
var config map[string]string
|
|
|
|
if cnt_raw, err := ioutil.ReadFile(path); err != nil {
|
|
return nil, err
|
|
} else if err := json.Unmarshal(cnt_raw, &config); err != nil {
|
|
return nil, err
|
|
} else {
|
|
return config, nil
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
var port = flag.Int("port", 8080, "Listening port")
|
|
var sitedir = flag.String("sitedir", "./contents/", "Directory containing site content and pictures")
|
|
flag.Parse()
|
|
|
|
log.Println("Reading site configuration...")
|
|
if config, err := readConfig(filepath.Join(*sitedir, "config.json")); err != nil {
|
|
log.Fatal(err)
|
|
os.Exit(1)
|
|
} else {
|
|
log.Println("Registering handlers...")
|
|
mux := http.NewServeMux()
|
|
mux.Handle("/favicon.ico", http.FileServer(http.Dir("./static/")))
|
|
mux.Handle("/static/", http.FileServer(http.Dir("./")))
|
|
mux.Handle("/doc/", http.FileServer(http.Dir("./")))
|
|
mux.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir(filepath.Join(*sitedir, "current")))))
|
|
mux.HandleFunc("/api/", ApiRouting)
|
|
mux.HandleFunc("/", SeeRouting)
|
|
http.HandleFunc("/", mux.ServeHTTP)
|
|
|
|
log.Println(fmt.Sprintf("Site ready (n=%s), listening on port %d", config["name"], *port))
|
|
http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)
|
|
}
|
|
}
|