summaryrefslogtreecommitdiff
path: root/pkg/web/server.go
blob: cde347c8eed2cca478a06a9f70770a4a09aac452 (plain)
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
package web

import (
	"net/http"

	"github.com/xlgmokha/x/pkg/serde"
	"gitlab.com/mokhax/sparkled/pkg/db"
)

type Server struct {
	db         db.Repository
	fileserver http.Handler
}

func NewServer(storage db.Repository) *Server {
	http.NewServeMux()
	return &Server{
		db:         storage,
		fileserver: http.FileServer(http.Dir("public")),
	}
}

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	switch r.Method {
	case "GET":
		switch r.URL.String() {
		case "/health":
			w.WriteHeader(http.StatusOK)
		case "/sparkles":
			serde.ToHTTP(w, r, s.db.All())
		}
		break
	default:
		s.fileserver.ServeHTTP(w, r)
		break
	}
}