diff --git a/README.md b/README.md index 204b2fa..bc8b41e 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,7 @@ After trying to decode some .ics files and review available go packages, I decid First attempt was from a fixed structure, similar to that needed. Later, I started to investigate the format and discovered that it is a pain, and has many variants, depending on who implements it. For this reason I evolved it to a tookit for decode and encode the format. -**Check examples dir for user cases:** - -[Demo app encoding an ical, using a mysql DB source](examples/mysqlsource) +Examples can be found in [this repo](https://github.com/jordic/goics). Features implemented: @@ -38,6 +36,7 @@ CHANGELOG: - v00. First api traial - v01. Api evolves to a ical toolkit - v02. Added example of integration with a mysql db. +- v03. Added go.mod; moved examples to separate repo. Thanks to: Joe Shaw Reviewing first revision. diff --git a/examples/mysqlsource/README.md b/examples/mysqlsource/README.md deleted file mode 100644 index 91ad8de..0000000 --- a/examples/mysqlsource/README.md +++ /dev/null @@ -1,7 +0,0 @@ - - -This example demostrates the use of goics, with a Mysql data source. -Obviously it will not work instead you have the correct tables setup -on the mysql side. - -The interesting part is in models.go \ No newline at end of file diff --git a/examples/mysqlsource/config.json b/examples/mysqlsource/config.json deleted file mode 100644 index 1afd408..0000000 --- a/examples/mysqlsource/config.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "dsn": "root:jordi@tcp(docker:3306)/test?charset=utf8&parseTime=true", - "ServerAddress": "localhost:9000" -} diff --git a/examples/mysqlsource/main.go b/examples/mysqlsource/main.go deleted file mode 100644 index 00b6bc1..0000000 --- a/examples/mysqlsource/main.go +++ /dev/null @@ -1,79 +0,0 @@ -package main - -import ( - "encoding/json" - "flag" - "fmt" - "io/ioutil" - "log" - "net/http" - - _ "github.com/go-sql-driver/mysql" - "github.com/gorilla/mux" - "github.com/jmoiron/sqlx" - "github.com/jordic/goics" -) - -var ( - configFile = flag.String("config", "config.json", "Path to config file") - Db *sqlx.DB -) - -var config struct { - Dsn string `json:"dsn"` - ServerAddress string -} - -const version = "0.1" - -func main() { - - flag.Parse() - load_config() - //fmt.Printf("%v", config) - var err error - Db, err = sqlx.Connect("mysql", config.Dsn) - if err != nil { - panic("Cant connect to database") - } - - m := mux.NewRouter() - m.Handle("/version", http.HandlerFunc(Version)) - m.Handle("/limpieza", http.HandlerFunc(LimpiezaHandler)) - - http.Handle("/", m) - log.Print("Server Started") - log.Fatal(http.ListenAndServe(config.ServerAddress, nil)) -} - -func Version(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "version %s", version) - return -} - -func LimpiezaHandler(w http.ResponseWriter, r *http.Request) { - - log.Print("Calendar request") - // Setup headers for the calendar - w.Header().Set("Content-type", "text/calendar") - w.Header().Set("charset", "utf-8") - w.Header().Set("Content-Disposition", "inline") - w.Header().Set("filename", "calendar.ics") - // Get the Collection models - collection := GetReservas() - // Encode it. - goics.NewICalEncode(w).Encode(collection) -} - -// Load and parse config json file -func load_config() { - file, err := ioutil.ReadFile(*configFile) - if err != nil { - log.Fatalf("Error reading config file %s", err) - } - - err = json.Unmarshal(file, &config) - if err != nil { - log.Fatalf("Error decoding config file %s", err) - } -} diff --git a/examples/mysqlsource/models.go b/examples/mysqlsource/models.go deleted file mode 100644 index 46cd3f4..0000000 --- a/examples/mysqlsource/models.go +++ /dev/null @@ -1,74 +0,0 @@ -package main - -import ( - "database/sql" - "fmt" - "log" - "time" - - _ "github.com/go-sql-driver/mysql" - "github.com/jordic/goics" -) - -// We define our struct as mysql row -type Reserva struct { - Clau string `db:"clau"` - Data time.Time `db:"data"` - Apartament string `db:"apartament"` - Nits int `db:"nits"` - Extra sql.NullString `db:"limpieza"` - Llegada sql.NullString `db:"llegada"` - Client string `db:"client"` -} - -// A collection of rous -type ReservasCollection []*Reserva - -// We implement ICalEmiter interface that will return a goics.Componenter. -func (rc ReservasCollection) EmitICal() goics.Componenter { - - c := goics.NewComponent() - c.SetType("VCALENDAR") - c.AddProperty("CALSCAL", "GREGORIAN") - c.AddProperty("PRODID;X-RICAL-TZSOURCE=TZINFO", "-//tmpo.io") - - for _, ev := range rc { - s := goics.NewComponent() - s.SetType("VEVENT") - dtend := ev.Data.AddDate(0, 0, ev.Nits) - k, v := goics.FormatDateField("DTEND", dtend) - s.AddProperty(k, v) - k, v = goics.FormatDateField("DTSTART", ev.Data) - s.AddProperty(k, v) - s.AddProperty("UID", ev.Clau+"@whotells.com") - des := fmt.Sprintf("%s Llegada: %s", ev.Extra.String, ev.Llegada.String) - s.AddProperty("DESCRIPTION", des) - s.AddProperty("SUMMARY", fmt.Sprintf("Reserva de %s", ev.Client)) - s.AddProperty("LOCATION", ev.Apartament) - - c.AddComponent(s) - } - - return c - -} - -// Get data from database populating ReservasCollection -func GetReservas() ReservasCollection { - - t := time.Now() - q := `SELECT clau, data, nits, limpieza, llegada, - CONCAT(b.tipus, " ", b.title) as apartament, - CONCAT(c.nom, " ", c.cognom) as client - FROM reserves_reserva as a LEFT join crm_client as c on a.client_id = c.id - LEFT JOIN reserves_apartament as b on a.apartament_id = b.id - WHERE data>=? and a.status<=3` - - reservas := ReservasCollection{} - err := Db.Select(&reservas, q, t.Format("2006-01-02")) - _ = Db.Unsafe() - if err != nil { - log.Println(err) - } - return reservas -} diff --git a/go.mod b/go.mod index f72ef48..8aeb35e 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,3 @@ module github.com/jordic/goics go 1.14 - -require ( - github.com/go-sql-driver/mysql v1.5.0 - github.com/gorilla/mux v1.8.0 - github.com/jmoiron/sqlx v1.3.1 -) diff --git a/go.sum b/go.sum index b672ff2..e69de29 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +0,0 @@ -github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= -github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= -github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= -github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= -github.com/jmoiron/sqlx v1.3.1 h1:aLN7YINNZ7cYOPK3QC83dbM6KT0NMqVMw961TqrejlE= -github.com/jmoiron/sqlx v1.3.1/go.mod h1:2BljVx/86SuTyjE+aPYlHCTNvZrnJXghYGpNiXLBMCQ= -github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0= -github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= -github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg= -github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=