-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
89 lines (70 loc) · 1.91 KB
/
server.go
File metadata and controls
89 lines (70 loc) · 1.91 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/julienschmidt/httprouter"
_ "github.com/lib/pq"
)
// Logger is a cool type
type Logger struct {
handler http.Handler
}
func (l *Logger) ServeHTTP(w http.ResponseWriter, req *http.Request) {
start := time.Now()
l.handler.ServeHTTP(w, req)
log.Printf("- %s %s (%s)", req.Method, req.URL.Path, time.Since(start).String())
}
func serveIndex(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
http.ServeFile(w, req, "static/index.html")
}
// HelloWorld is a cool function
func HelloWorld(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Simply write some test data for now
fmt.Fprintln(w, "Hello World!")
}
// App is a cool function
func App() http.Handler {
router := httprouter.New()
db := NewDB()
// Add a handler on /hello
router.GET("/hello", HelloWorld)
router.GET("/campaigns", CampaignsIndex(db))
// Serve static assets
router.GET("/", serveIndex)
router.ServeFiles("/stylesheets/*filepath", http.Dir("static/stylesheets"))
return &Logger{router}
}
func CampaignsIndex(db *sql.DB) func(http.ResponseWriter, *http.Request, httprouter.Params) {
return func(rw http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var title, author string
err := db.QueryRow("SELECT title, author FROM campaigns").Scan(&title, &author)
if err != nil && err == sql.ErrNoRows {
fmt.Fprintf(rw, "No campaigns found")
return
}
if err != nil {
panic(err)
}
fmt.Fprintf(rw, "The first campaign is '%s' by '%s'", title, author)
}
}
func NewDB() *sql.DB {
db, err := sql.Open("postgres", "postgres://gcappellotto@localhost/issuehunter?sslmode=disable")
if err != nil {
panic(err)
}
return db
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
// Fire up the server
fmt.Println("Server listening on port " + port)
http.ListenAndServe(":"+port, App())
}