-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
54 lines (42 loc) · 1.22 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
47
48
49
50
51
52
53
54
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
_ "github.com/joho/godotenv/autoload"
"github.com/seigaalghi/e-library/pkg/zaplog"
"github.com/seigaalghi/e-library/transport"
)
func main() {
logger := zaplog.WithContext(context.Background())
defer logger.Sync()
port := os.Getenv("PORT")
if port == "" {
port = "8000"
}
logger.Info("starting backend server at port: " + port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), initRouter()))
}
func initRouter() *mux.Router {
r := mux.NewRouter()
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")
json.NewEncoder(w).Encode(map[string]string{
"message": "hello world",
})
})
v1 := r.PathPrefix("/api/v1").Subrouter()
go transport.NewHttpHandler(v1)
return r
}
func corsHandler() http.Handler {
header := handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "Authorization"})
methods := handlers.AllowedMethods([]string{"GET", "POST", "PUT", "HEAD", "DELETE", "OPTIONS"})
origins := handlers.AllowedOrigins([]string{"*"})
return handlers.CORS(header, methods, origins)(initRouter())
}