-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
executable file
·54 lines (46 loc) · 1.54 KB
/
server.go
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 server
import (
"fmt"
"net/http"
"path/filepath"
"time"
"./api/v1"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
"github.com/jordan-wright/unindexed"
)
// HelloWorld is a sample handler
func HelloWorld(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello world!")
}
// NewRouter returns a new HTTP handler that implements the main server routes
func NewRouter() http.Handler {
router := chi.NewRouter()
cors := cors.New(cors.Options{
// AllowedOrigins: []string{"https://foo.com"}, // Use this to allow specific origin hosts
AllowedOrigins: []string{"*"},
// AllowOriginFunc: func(r *http.Request, origin string) bool { return true },
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link"},
AllowCredentials: true,
MaxAge: 300, // Maximum value not ignored by any of major browsers
})
router.Use(cors.Handler)
// Set up our middleware with sane defaults
router.Use(middleware.RealIP)
router.Use(middleware.Logger)
router.Use(middleware.Recoverer)
router.Use(middleware.DefaultCompress)
router.Use(middleware.Timeout(60 * time.Second))
// Set up our root handlers
// router.Get("/", HelloWorld)
// Set up our API
router.Mount("/api/v1/", v1.NewRouter())
// Set up static file serving
staticPath, _ := filepath.Abs("../../../static/")
fs := http.FileServer(unindexed.Dir(staticPath))
router.Handle("/*", fs)
return router
}