-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
46 lines (36 loc) · 1.14 KB
/
main.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
package main
import (
"context"
"flag"
"log"
"net/http"
"time"
"go.formulabun.club/index-site/common"
"go.formulabun.club/index-site/files"
"go.formulabun.club/index-site/maps"
"go.formulabun.club/metadatadb"
"go.formulabun.club/storage"
"github.com/gorilla/mux"
)
var host = flag.String("host", ":8080", "Hostname on which to bind")
func main() {
flag.Parse()
r := mux.NewRouter()
connectContext, _ := context.WithTimeout(context.Background(), time.Second*10)
dbc, err := metadatadb.NewClient(connectContext)
if err != nil {
panic(err)
}
// files
r.PathPrefix(common.FilePath).Handler(http.StripPrefix(common.FilePath, storage.StoreHandler{}))
// api
apiRouter := r.Methods("GET", "POST").PathPrefix("/api").Subrouter()
files.SetupRouter(apiRouter, dbc)
maps.SetupRouter(apiRouter, dbc)
// pages
staticRoute := r.Methods("GET").Subrouter()
staticRoute.PathPrefix("/public/").Handler(http.StripPrefix("/public/", http.FileServer(http.Dir("static/public/"))))
staticRoute.PathPrefix("/").Handler(http.FileServer(http.Dir("static/html/")))
log.Printf("hosting on http://%s\n", *host)
log.Fatal(http.ListenAndServe(*host, r))
}