Skip to content
Permalink
Browse files

Adding configurable server

  • Loading branch information...
Depado committed Jan 15, 2016
1 parent 75ff64d commit a047289a3afdb8669c1c87af65d65c461263e2bc
Showing with 58 additions and 23 deletions.
  1. +1 −0 .gitignore
  2. +1 −8 assets/app.js
  3. +28 −0 conf/conf.go
  4. +9 −4 main.go
  5. +19 −11 templates/index.html
@@ -26,3 +26,4 @@ _testmain.go
govue
data.db
node_modules
conf.yml
@@ -15,21 +15,15 @@ new Vue({
this.fetchEntries();
},
methods: {
// We dedicate a method to retrieving and setting some data
fetchEntries: function() {
this.entryEndpoint.get().then(function(response) {
this.$set('entries', response.data.data);
}, function(response) {
console.log(response);
});
},

// Adds an event to the existing events array
postEntry: function() {
var wrapper = {
data: this.entry
}
this.entryEndpoint.save(wrapper).then(function(response) {
this.entryEndpoint.save({data: this.entry}).then(function(response) {
this.entries.push(response.data.data);
}, function(response) {
console.log(response);
@@ -39,7 +33,6 @@ new Vue({
markdown: '',
};
},

deleteEntry: function(index) {
this.entryEndpoint.delete({id: this.entries[index].attributes.id}).then(function(response) {
this.entries.splice(index, 1);
@@ -0,0 +1,28 @@
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"`
APIVersion int `yaml:"api_version"`
}

// 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
}
13 main.go
@@ -1,23 +1,28 @@
package main

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

"github.com/Depado/govue/conf"
"github.com/Depado/govue/database"
"github.com/Depado/govue/models/entry"
"github.com/gin-gonic/gin"
)

const currentAPIVersion = "1"

func index(c *gin.Context) {
http.ServeFile(c.Writer, c.Request, "templates/index.html")
}

func main() {
var err error

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

// Database initialization
if err = database.Main.Open(); err != nil {
log.Fatal(err)
@@ -30,7 +35,7 @@ func main() {

r.GET("/", index)

currentAPI := r.Group("/api/v" + currentAPIVersion)
currentAPI := r.Group("/api/v" + strconv.Itoa(conf.C.APIVersion))
entryEndpoint := currentAPI.Group("/entry")
{
entryEndpoint.POST("/", entry.Post)
@@ -40,5 +45,5 @@ func main() {
entryEndpoint.DELETE("/:id", entry.Delete)
}

r.Run(":8080")
r.Run(fmt.Sprintf(":%s", strconv.Itoa(conf.C.Port)))
}
@@ -19,10 +19,7 @@
</div>
</nav>

<!-- main body of our application -->
<div class="container" id="entries">

<!-- add an event form -->
<div class="col-sm-6">
<div class="panel panel-default">
<div class="panel-heading text-center">
@@ -40,23 +37,34 @@ <h4>Submit an Entry</h4>
</div>
</div>

<!-- show the events -->
<div class="col-sm-6">
<div class="list-group">
<a href="#" class="list-group-item" v-for="entry in entries">
<h4 class="list-group-item-heading">
{{ entry.attributes.title }}
<span class="pull-right">
<button class="btn btn-xs btn-danger" v-on:click="deleteEntry($index)">Delete</button>
</span>
<div class="list-group-item" v-for="entry in entries">
<span class="dropdown pull-right">
<button class="btn btn-xs dropdown-toggle" type="button" id="dropdown{{$index}}" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<i class="glyphicon glyphicon-cog"></i>
</button>
<ul class="dropdown-menu" aria-labelledby="dropdown{{$index}}">
<li><a href="#" v-on:click="deleteEntry($index)"><i class="glyphicon glyphicon-pencil"></i> Edit</a></li>
<!-- <li role="separator" class="divider"></li>
<li><a href="#" v-on:click="deleteEntry($index)"><i class="glyphicon glyphicon-trash"></i> Delete</a></li> -->
</ul>
</span>
<h4 class="list-group-item-heading">{{ entry.attributes.title }}
</h4>
<!-- <div class="btn-group pull-right" role="group">
<button class="btn btn-xs btn-primary" v-on:click="deleteEntry($index)"><i class="glyphicon glyphicon-pencil"></i> Edit</button>
<button class="btn btn-xs btn-danger" </button>
</div> -->
<p class="list-group-item-text">{{ entry.attributes.markdown }}</p>
</a>
</div>
</div>
</div>
</div>

<!-- JS -->
<script src="vendor/jquery/dist/jquery.min.js"></script>
<script src="vendor/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="vendor/vue/dist/vue.js"></script>
<script src="vendor/vue-resource/dist/vue-resource.js"></script>
<script src="static/app.js"></script>

0 comments on commit a047289

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