-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
54 lines (45 loc) · 1.22 KB
/
Copy pathserver.go
File metadata and controls
54 lines (45 loc) · 1.22 KB
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
package main
import (
"log"
"net/http"
"os"
"github.com/WillNigel23/portfolio/db"
"github.com/WillNigel23/portfolio/controllers"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/gotailwindcss/tailwind/twembed"
"github.com/gotailwindcss/tailwind/twhandler"
)
type Server struct {
server *http.Server
router *mux.Router
}
func NewServer(db *db.Database) *Server {
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
s := Server{
server: &http.Server{
Addr: ":" + port,
},
router: mux.NewRouter().StrictSlash(true),
}
// Static Files
s.router.PathPrefix("/static/").Handler(
http.StripPrefix("/static/", http.FileServer(http.Dir("static"))),
)
s.router.PathPrefix("/css/").Handler(
twhandler.New(http.Dir("static/css"), "/css", twembed.New()),
)
// Routes
s.router.HandleFunc("/", controllers.SectionsIndexHandler(db)).Methods("GET")
s.router.HandleFunc("/sections/{id}", controllers.SectionsShowHandler(db)).Methods("GET")
s.server.Handler = handlers.LoggingHandler(os.Stdout, s.router)
return &s
}
func (s *Server) RunServer() {
if err := s.server.ListenAndServe(); err != nil {
log.Fatal(err)
}
}