-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
54 lines (39 loc) · 1.15 KB
/
app.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 (
"fmt"
"github.com/codegangsta/negroni"
"github.com/gorilla/mux"
"net/http"
"os"
"go-template/folder"
)
func HeaderMiddleware(res http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
res.Header().Set("Access-Control-Allow-Origin", "*")
res.Header().Set("Access-Control-Allow-Headers", "Origin, Accept, Content-Type")
res.Header().Set("Access-Control-Allow-Method", "GET, POST, PUT, DELETE")
// do some stuff before
next(res, req)
// do some stuff after
}
func main() {
port := os.Getenv("PORT")
if len(port) == 0 {
port = "3000"
}
// Call method in other file
other()
// Call method in other folder
// Name is package name (and usually same as folder)
folder.OtherFolder()
// gorilla/mux is a powerful URL router and dispatcher.
router := mux.NewRouter()
router.Methods("GET").Path("/").HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "text/plain")
res.Write([]byte("template application"))
})
fmt.Println("Starting server on port", port)
n := negroni.Classic()
n.Use(negroni.HandlerFunc(HeaderMiddleware))
n.UseHandler(router)
n.Run(":" + port)
}