This commit is contained in:
Jay 2019-12-10 14:15:27 +00:00
commit 369e8d3f6e
12 changed files with 349 additions and 0 deletions

1
.dockerignore Symbolic link
View File

@ -0,0 +1 @@
.gitignore

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/config.yml
.env

4
config/config.yml Normal file
View File

@ -0,0 +1,4 @@
port: 10230
google:
api_key: ''

16
go.mod Normal file
View File

@ -0,0 +1,16 @@
module go-cal
go 1.13
require (
git.trj.tw/golang/gofb v0.0.0-20191210044940-169b79d0cbe2
git.trj.tw/golang/utils v0.0.0-20190225142552-b019626f0349
github.com/fogleman/gg v1.3.0
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/joho/godotenv v1.3.0
github.com/otakukaze/envconfig v1.0.0
github.com/sirupsen/logrus v1.4.2
github.com/ungerik/go-cairo v0.0.0-20191014050614-4a03f432a432 // indirect
golang.org/x/image v0.0.0-20191206065243-da761ea9ff43
gopkg.in/yaml.v2 v2.2.7
)

29
go.sum Normal file
View File

@ -0,0 +1,29 @@
git.trj.tw/golang/gofb v0.0.0-20191210044940-169b79d0cbe2 h1:P/3jKmWVSaXU9TbvdYMs1r5LbFG93cpHVBBXrxqLhBY=
git.trj.tw/golang/gofb v0.0.0-20191210044940-169b79d0cbe2/go.mod h1:UohCvL4t/inqH38WSGwHZ1jR4OfUKj0D8MJ42asQZXE=
git.trj.tw/golang/utils v0.0.0-20190225142552-b019626f0349 h1:V6ifeiJ3ExnjaUylTOz37n6z5uLwm6fjKjnztbTCaQI=
git.trj.tw/golang/utils v0.0.0-20190225142552-b019626f0349/go.mod h1:yE+qbsUsijCTdwsaQRkPT1CXYk7ftMzXsCaaYx/0QI0=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/otakukaze/envconfig v1.0.0 h1:VGu7NoDFbReFO72m5Z15h5PH5gVk5fVLZ6iHF58ggFk=
github.com/otakukaze/envconfig v1.0.0/go.mod h1:v2dNv5NX1Lakw3FTAkbxYURyaiOy68M8QpMTZz+ogfs=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/ungerik/go-cairo v0.0.0-20191014050614-4a03f432a432 h1:luu+HbKrZKlIjiaclwrldreEEvVPYX/ujRbTGkKz61c=
github.com/ungerik/go-cairo v0.0.0-20191014050614-4a03f432a432/go.mod h1:0ErpLiOxxE1oY+R4stiKut6/DbUJHnOp6U+e4d8zcTs=
golang.org/x/image v0.0.0-20191206065243-da761ea9ff43 h1:gQ6GUSD102fPgli+Yb4cR/cGaHF7tNBt+GYoRCpGC7s=
golang.org/x/image v0.0.0-20191206065243-da761ea9ff43/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

39
main.go Normal file
View File

@ -0,0 +1,39 @@
package main
import (
"go-cal/pkg/config"
"log"
_ "github.com/joho/godotenv/autoload"
)
func main() {
var err error
if err = config.Load(); err != nil {
log.Fatalf("Load config file fail, %v\n", err)
}
// fb := gofb.NewFramebuffer()
// fb.Init()
// defer fb.Release()
// fb.Fill(0, 0, 0, 0)
// dc := gg.NewContext(fb.Xres, fb.Yres)
// dc.DrawCircle(10, 10, 10)
// dc.SetRGB(125, 0, 0)
// dc.Fill()
// dc.DrawCircle(100, 100, 40)
// dc.SetRGB(0, 120, 0)
// dc.Fill()
// fb.DrawImage(0, 0, dc.Image())
// dc.DrawCircle(600, 10, 222)
// dc.Fill()
// dc.DrawString("Test string ", 10, 400)
// fb.DrawImage(0, 0, dc.Image())
}

75
pkg/apis/apis.go Normal file
View File

@ -0,0 +1,75 @@
package apis
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"reflect"
"github.com/sirupsen/logrus"
)
// Errors
var (
ErrAPICall = errors.New("API call fail")
)
// GenRequest -
func GenRequest(targetURL, method, bearerToken string, headers map[string]string, body io.Reader) (*http.Request, error) {
logrus.Infof("[APIS][GenRequest] Generate Request from url : %s\n", targetURL)
req, err := http.NewRequest(method, targetURL, body)
if err != nil {
logrus.Errorf("[APIS][GenRequest] Create request fail : %s\n", err.Error())
return nil, err
}
if len(headers) > 0 {
for k, v := range headers {
req.Header.Set(k, v)
}
}
if len(bearerToken) > 0 {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", bearerToken))
}
return req, nil
}
// SendRequest -
func SendRequest(req *http.Request, data interface{}) (status int, b []byte, err error) {
logrus.Infof("[APIS][SendRequest] Send Request\n")
if req == nil {
logrus.Errorf("[APIS][SendRequest] Request Object Empty\n")
return 0, nil, errors.New("request object is empty")
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
logrus.Errorf("[APIS][SendRequest] HTTP Request fail : %s\n", err.Error())
return -1, nil, err
}
defer resp.Body.Close()
b, err = ioutil.ReadAll(resp.Body)
if err != nil {
logrus.Errorf("[APIS][SendRequest] Read RespBody fail : %s\n", err.Error())
return resp.StatusCode, b, err
}
if data != nil {
t := reflect.TypeOf(data)
if t.Kind() == reflect.Ptr {
err = json.Unmarshal(b, data)
if err != nil {
logrus.Errorf("[APIS][SendRequest] JSON parse fail : %s\n", err.Error())
return resp.StatusCode, b, err
}
}
}
return resp.StatusCode, b, nil
}

View File

@ -0,0 +1,13 @@
package google
// GetCalendarList -
func GetCalendarList() (err error) {
p := "/calendar/v3/users/me/calendarList"
u, err := getURL(p)
if err != nil {
return
}
_ = u
return
}

24
pkg/apis/google/google.go Normal file
View File

@ -0,0 +1,24 @@
package google
import "net/url"
var baseURL = "https://www.googleapis.com/"
func getURL(p string) (string, error) {
u, err := url.Parse(baseURL)
if err != nil {
return "", err
}
u, err = u.Parse(p)
if err != nil {
return "", err
}
return u.String(), nil
}
func getHeaders() map[string]string {
obj := make(map[string]string)
// conf := config.Get()
return obj
}

14
pkg/calendar/calendar.go Normal file
View File

@ -0,0 +1,14 @@
package calendar
import (
"image"
"github.com/fogleman/gg"
)
// Draw -
func Draw(w, h int) *image.Image {
dc := gg.NewContext(w, h)
_ = dc
return nil
}

62
pkg/config/config.go Normal file
View File

@ -0,0 +1,62 @@
package config
import (
"errors"
"io/ioutil"
"os"
"path"
"git.trj.tw/golang/utils"
"github.com/otakukaze/envconfig"
"gopkg.in/yaml.v2"
)
// Google config
type Google struct {
APIKey string `yaml:"api_key" env:"GOOGLE_API_KEY"`
}
// Config main struct
type Config struct {
Port int `yaml:"port" env:"PORT"`
Google Google `yaml:"google"`
}
var conf *Config
// Load config from file and env
func Load(p ...string) (err error) {
fp := ""
if len(p) > 0 && len(p[0]) > 0 {
fp = p[0]
} else {
wd, err := os.Getwd()
if err != nil {
return err
}
fp = path.Join(wd, "config.yml")
}
fp = utils.ParsePath(fp)
if !utils.CheckExists(fp, false) {
return errors.New("config file not found")
}
conf = &Config{}
b, err := ioutil.ReadFile(fp)
if err != nil {
return
}
err = yaml.Unmarshal(b, conf)
if err != nil {
return
}
envconfig.Parse(conf)
return
}
// Get config struct
func Get() *Config { return conf }

70
pkg/storage/storage.go Normal file
View File

@ -0,0 +1,70 @@
package storage
import (
"encoding/json"
"errors"
"io/ioutil"
"git.trj.tw/golang/utils"
)
// Storage -
type Storage struct {
m map[string]interface{}
filePath string
}
var s *Storage
// New storage
func New() *Storage {
if s == nil {
s = &Storage{
m: make(map[string]interface{}),
}
}
return s
}
// Get storage
func Get() *Storage { return s }
// Load storage data from file
func (p *Storage) Load(filePath string) (err error) {
filePath = utils.ParsePath(filePath)
if !utils.CheckExists(filePath, false) {
return errors.New("storage file not found")
}
p.filePath = filePath
b, err := ioutil.ReadFile(filePath)
if err != nil {
return
}
err = json.Unmarshal(b, &p.m)
return
}
func (p *Storage) Write() (err error) {
b, err := json.Marshal(p.m)
if err != nil {
return
}
err = ioutil.WriteFile(p.filePath, b, 0664)
return
}
// Get data by key
func (p *Storage) Get(key string) (data interface{}, ok bool) {
data, ok = p.m[key]
return
}
// Set data by key
func (p *Storage) Set(key string, value interface{}) { p.m[key] = value }
// Delete data by key
func (p *Storage) Delete(key string) { delete(p.m, key) }