-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
56 lines (48 loc) · 1.28 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
55
56
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"net/http"
"personal-site-backend/src/models"
)
func hello(w http.ResponseWriter, req *http.Request) {
_, err := fmt.Fprintf(w, "hello\n")
if err != nil {
log.Errorf("Unable to write message to an output")
}
}
func headers(w http.ResponseWriter, req *http.Request) {
for name, headers := range req.Header {
for _, h := range headers {
_, err := fmt.Fprintf(w, "%v: %v\n", name, h)
if err != nil {
log.Errorf("Unable to write message to an output")
}
}
}
}
func main() {
viper.AutomaticEnv()
viper.SetDefault("PORT", 8080)
// TODO: another drivers?
db, err := gorm.Open(postgres.Open(viper.GetString("DATABASE_URL")), &gorm.Config{})
if err != nil {
panic("failed to connect to a database")
}
// Run migrations
// Not a separate file for now due to project context
err = models.AutoMigrate(db)
if err != nil {
panic(err)
}
http.HandleFunc("/hello", hello)
http.HandleFunc("/headers", headers)
log.Infof("Server started at port %s...", viper.GetString("PORT"))
err = http.ListenAndServe(":"+viper.GetString("PORT"), nil)
if err != nil {
panic(fmt.Sprintf("Unable to start a web server on port %s", viper.GetString("PORT")))
}
}