Skip to content
Permalink
Browse files

Adding configurable server

  • Loading branch information...
Depado committed Jan 15, 2016
1 parent 976d92e commit 839a675e119a56a96070311ba44b0d6311a57a9c
Showing with 41 additions and 2 deletions.
  1. +1 −0 .gitignore
  2. +27 −0 conf/conf.go
  3. +13 −2 main.go
@@ -24,3 +24,4 @@ _testmain.go
*.prof

jagon-api
conf.yml
@@ -0,0 +1,27 @@
package conf

import (
"io/ioutil"

"gopkg.in/yaml.v2"
)

// Configuration is the struct representing a configuration
type Configuration struct {
Port int `yaml:"port"`
Debug bool `yaml:"debug"`
}

// C is the main Configuration
var C Configuration

// Load loads the given fp (file path) to the C global configuration variable.
func Load(fp string) error {
var err error
conf, err := ioutil.ReadFile(fp)
if err != nil {
return err
}
err = yaml.Unmarshal(conf, &C)
return err
}
15 main.go
@@ -1,16 +1,27 @@
package main

import (
"log"
"net/http"
"strconv"

"github.com/Depado/jagon-api/conf"
"github.com/Depado/jagon-api/jagon"
"github.com/gin-gonic/gin"
)

const version = "1"

func main() {
gin.SetMode(gin.ReleaseMode)
var err error

if err = conf.Load("conf.yml"); err != nil {
log.Fatal(err)
}

if !conf.C.Debug {
gin.SetMode(gin.ReleaseMode)
}
r := gin.Default()
r.Static("/static", "./assets")
r.LoadHTMLGlob("./templates/*")
@@ -28,5 +39,5 @@ func main() {
apir.GET("/info/prophet", jagon.Prophet)
apir.GET("/info/apostles", jagon.Apostotles)
}
r.Run(":8080")
r.Run(":" + strconv.Itoa(conf.C.Port))
}

0 comments on commit 839a675

Please sign in to comment.
You can’t perform that action at this time.