Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lafriakh committed Oct 21, 2018
0 parents commit 914fafd
Show file tree
Hide file tree
Showing 63 changed files with 4,513 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Rachid Lafriakh

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
71 changes: 71 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Kira
Kira micro framework

# Default example
```
package main
import (
"github.com/Lafriakh/kira"
)
func main() {
app := kira.New()
app.GET("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello kira :)"))
})
app.Run()
}
```
# Response
json
```
app.JSON(w, data, 200)
```
render template
```
app.Render(w, data,"template/path")
```
# Middleware
```
// Log - log middleware
type Log struct{}
func NewLogger() *Log {
return &Log{}
}
// Handler - middleware handler
func (l *Log) Handler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var now = time.Now()
// logger message
log.Printf(
"%s\t%s\t%s",
r.Method,
r.RequestURI,
time.Since(now),
)
next.ServeHTTP(w, r)
})
}
// Pattern - middleware
func (l *Log) Pattern() []string {
return []string{"*"}
}
// Name - middleware
func (l *Log) Name() string {
return "logger"
}
```
## TODO

- [ ] Command-line interface (CLI)
- [ ] Live Reload
147 changes: 147 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package config

import (
"bytes"
"errors"
"io/ioutil"
"path/filepath"

"github.com/go-kira/kog"

yaml "gopkg.in/yaml.v2"
)

var errInvalidEnv = errors.New("invalid environment, use only: development, production, test")

// DefaultPath to the configs folder.
var DefaultPath = "./config/"

// DefaultVariablesPath enviroments path.
var DefaultVariablesPath = "./config/environments/"

// Env type
type Env int

const (
// Development enviroment
Development Env = iota
// Production enviroment
Production
// Test enviroment
Test
)

var envStrings = map[string]Env{
"development": Development,
"production": Production,
"test": Test,
}
var envNames = [...]string{
Production: "production",
Development: "development",
Test: "test",
}

// to store all config files
var globalData map[string]interface{}

// New return Config instance
func New(env string) map[string]interface{} {
// first check if the env supported.
if _, ok := envStrings[env]; !ok {
kog.Panic(errInvalidEnv)
}

// load configs
configs := load(env)

// return configs
if configs == nil {
// panic here
return globalData
}
// panic if there an error.
kog.Panic(configs)

return globalData
}

// Load config file data
func load(env string) error {
var buf bytes.Buffer

// to store all config files
var files []string

// first get all config files
configs, err := filepath.Glob(DefaultPath + "*.yaml")
if err != nil {
return err
}
files = append(files, configs...)

// add env file to the config files.
// this will change any config value.
if env != "" {
files = append(files, DefaultVariablesPath+env+".yaml")
}

// loop throw all
for _, config := range files {
// read file
data, err := ioutil.ReadFile(config)
if err != nil {
return err
}

buf.Write(data)
buf.WriteString("\n") // add new line to the end of the file
}

// Unmarshal toml data
yaml.Unmarshal(buf.Bytes(), &globalData)

return nil
}

// Get ...
func Get(key string) interface{} {
return globalData[key]
}

// GetDefault return value by key, if the value empty set a default value.
func GetDefault(key string, def interface{}) interface{} {
val := globalData[key]
if val == nil {
return def
}

return val
}

// GetString - return config as string type.
func GetString(key string) string {
if globalData[key] == nil {
return ""
}

return globalData[key].(string)
}

// GetInt - return config as int type.
func GetInt(key string) int {
if globalData[key] == nil {
return 0
}

return globalData[key].(int)
}

// GetBool - return config as int type.
func GetBool(key string) bool {
if globalData[key] == nil {
return false
}

return globalData[key].(bool)
}
25 changes: 25 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package config

import (
"testing"
)

func TestLoad(t *testing.T) {
DefaultPath = "./testdata"
DefaultVariablesPath = "./testdata/enviroments"

New("production")
t.Fail()
}

func TestGetDefault(t *testing.T) {
DefaultPath = "./testdata/"
DefaultVariablesPath = "./testdata/environments/"

New("development")
def := GetDefault("SESSION_NAMEe", "default")

if def != "default" {
t.Error("the value not 'default'")
}
}
Empty file.
Empty file.
Empty file.
1 change: 1 addition & 0 deletions config/testdata/session.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SESSION_NAME: kira
58 changes: 58 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package kira

import (
"fmt"
"net/http"

"github.com/go-kira/kog"
)

// Example:
// app.Get("/", func (ctx *kira.Context) {
//
// })

// ContextFunc - Type to define context function
type ContextFunc func(*Context)

// Context ...
type Context struct {
request *http.Request
response http.ResponseWriter
Logger *kog.Logger
// The data assocaited with the request.
data map[string]interface{}
// Will hold the response status code.
statusCode int
}

// NewContext - Create new instance of Context
func NewContext(res http.ResponseWriter, req *http.Request, logger *kog.Logger) *Context {
return &Context{
request: req,
response: res,
Logger: logger,
data: make(map[string]interface{}),
}
}

// Request - get the request
func (c *Context) Request() *http.Request {
return c.request
}

// Response - get the response
func (c *Context) Response() http.ResponseWriter {
return c.response
}

// Log - write the log
func (c *Context) Log() *kog.Logger {
return c.Logger
}

// Error - stop the request with panic
func (c *Context) Error(msg ...interface{}) {
// Just panic and the recover will come to save us :)
panic(fmt.Sprint(msg...))
}
11 changes: 11 additions & 0 deletions context_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package kira

// SetData ...
func (c *Context) SetData(key string, data interface{}) {
c.data[key] = data
}

// GetData ...
func (c *Context) GetData(key string) interface{} {
return c.data[key]
}
Loading

0 comments on commit 914fafd

Please sign in to comment.