-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
50 lines (40 loc) · 1.07 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
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
func main() {
r := mux.NewRouter()
port, _ := getPort()
r.HandleFunc("/", indexHandler).Methods("GET")
fmt.Printf("Server up and running . Running PORT: %s\n", port)
r.HandleFunc("/webhook", webhookGetHandler).Methods("GET")
r.HandleFunc("/webhook", webhookPostHandler).Methods("POST")
r.HandleFunc("/add-message", addMessageHandler).Methods("GET")
err := http.ListenAndServe(port, r)
if err != nil {
log.Fatal("Error listening and server", err)
}
}
func addMessageHandler(w http.ResponseWriter, r *http.Request) {
t := template.New("add")
t, _ = t.ParseFiles("tmpl/add.html")
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Got my server up and running in Go. Yay!")
}
func getPort() (string, error) {
port := os.Getenv("PORT")
if port == "" {
// run local
// return ":3500", fmt.Errorf("$PORT not set")
// deploy
return "", fmt.Errorf("$PORT not set")
}
return ":" + port, nil
}