1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
package main
import (
"context"
"embed"
"log"
"net/http"
"xengineering.eu/ceres/controller"
"xengineering.eu/ceres/model"
"xengineering.eu/ceres/view"
)
type Server http.Server
//go:embed view/static/simple.css/simple.css view/static/ceres.js
var static embed.FS
func NewServer(addr string, db *model.DB) *Server {
mux := http.NewServeMux()
mux.Handle(
"GET /static/",
http.StripPrefix("/static/", http.FileServer(http.FS(static))),
)
mux.Handle("GET /version", view.VersionRead(version))
mux.Handle("GET /recipes", view.RecipesRead(db))
mux.Handle("GET /recipe/create", view.RecipeCreate())
mux.Handle("POST /recipe", controller.RecipeCreate(db))
mux.Handle("GET /recipe/{id}", view.RecipeRead(db))
mux.Handle("POST /recipe/{id}", controller.RecipeUpdate(db))
mux.Handle("DELETE /recipe/{id}", controller.RecipeDelete(db))
mux.Handle("GET /favicon.ico", view.FaviconRead())
mux.Handle("GET /", view.IndexRead())
var srv http.Server
srv.Addr = addr
srv.Handler = mux
log.Printf("Configured server to listen on http://%s\n", srv.Addr)
return (*Server)(&srv)
}
func (s *Server) Start() {
(*http.Server)(s).ListenAndServe()
}
func (s *Server) Stop() {
err := (*http.Server)(s).Shutdown(context.Background())
if err != nil {
log.Printf("Failed to shutdown HTTP server: %v\n", err)
} else {
log.Println("Stopped HTTP server")
}
}
|