-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkorugo.go
72 lines (58 loc) · 1.4 KB
/
korugo.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package korugo
import (
"fmt"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
_ "github.com/jackc/pgx/v4"
)
// Config defines the App configuration
type Config struct {
// http route prefix. Default: "v1"
Prefix string
// http port. Default: 8080
Port uint16
// http router. Default: mux.NewRouter()
Router *mux.Router
}
type App struct {
Config *Config
log *log.Logger
}
var defaultConfig = Config{
Prefix: "v1",
Port: 8080,
}
// Run initializes and starts the korugo application.
// Any unset config values are automatically filled.
// See Config struct for more information.
func (app *App) Run() {
app.log = log.New(os.Stdout, "korugo", log.LstdFlags|log.Lshortfile)
// Initialize config defaults
cnf := app.Config
if cnf == nil {
cnf = &defaultConfig
}
if cnf.Port == 0 {
cnf.Port = defaultConfig.Port
}
if cnf.Prefix == "" {
cnf.Prefix = defaultConfig.Prefix
}
if cnf.Router == nil {
cnf.Router = mux.NewRouter()
}
app.Config = cnf
// Begin the GraphQL server
r := cnf.Router.PathPrefix(fmt.Sprintf("/%s", app.Config.Prefix)).Subrouter()
r.Handle("/gql", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
r = r.WithContext(ctx)
// srv.ServeHTTP(w, r)
}))
app.Config = cnf
app.log.Printf("Starting http server on %v", app.Config.Port)
err := http.ListenAndServe(fmt.Sprintf(":%v", app.Config.Port), r)
app.log.Fatalf("%+v", err)
}