1st commit
commit
e439115501
|
@ -0,0 +1,7 @@
|
|||
pkg/
|
||||
.go/
|
||||
.vscode/
|
||||
|
||||
config.json
|
||||
chromagies
|
||||
log.txt
|
|
@ -0,0 +1,5 @@
|
|||
module chromagies
|
||||
|
||||
go 1.24.2
|
||||
|
||||
require github.com/gorilla/mux v1.8.1
|
|
@ -0,0 +1,2 @@
|
|||
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
|
||||
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
|
|
@ -0,0 +1,107 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"chromagies/src/api"
|
||||
"chromagies/src/config"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
var router *mux.Router
|
||||
var logFile *os.File
|
||||
|
||||
func main() {
|
||||
startTime := time.Now()
|
||||
log.SetOutput(os.Stdout)
|
||||
log.Println("============================================================")
|
||||
log.Println("Starting server...")
|
||||
|
||||
go handleStopSignal()
|
||||
|
||||
initConfig()
|
||||
startLogger()
|
||||
|
||||
initRouter()
|
||||
|
||||
stopTime := time.Now()
|
||||
log.Printf("Server ready in %d microseconds\n", stopTime.Sub(startTime).Microseconds())
|
||||
|
||||
var conf config.Server = (config.GetConfig()).Server
|
||||
|
||||
http.ListenAndServe(fmt.Sprintf("%s:%s", conf.IP, conf.Port), router)
|
||||
stopServer()
|
||||
}
|
||||
|
||||
func startLogger() {
|
||||
var conf config.Logger = (config.GetConfig()).Logger
|
||||
if conf.Output != "" {
|
||||
var err error
|
||||
logFile, err = os.OpenFile(conf.Output, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
log.Printf("Output file \"%s\" un-openable : %s", conf.Output, err)
|
||||
return
|
||||
}
|
||||
log.Printf("Switching logs to output file \"%s\"\nStopping to log in the console", conf.Output)
|
||||
log.SetOutput(logFile)
|
||||
log.Println("============================================================")
|
||||
log.Printf("Hi there ! Server is starting...")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func initConfig() {
|
||||
var err error
|
||||
err = config.ReadConfigFile()
|
||||
|
||||
if err != nil {
|
||||
err = config.CreateConfigFile()
|
||||
|
||||
if err == nil {
|
||||
err = config.ReadConfigFile()
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Fatal("Cannot read/write config file !")
|
||||
}
|
||||
}
|
||||
|
||||
func initRouter() {
|
||||
router = mux.NewRouter()
|
||||
|
||||
// routeur.NotFoundHandler = TODO
|
||||
|
||||
api.Init(router)
|
||||
|
||||
router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
|
||||
|
||||
}
|
||||
|
||||
func handleStopSignal() {
|
||||
sigs := make(chan os.Signal, 1)
|
||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-sigs
|
||||
close(sigs)
|
||||
stopServer()
|
||||
}
|
||||
|
||||
func stopServer() {
|
||||
log.Println("Stopping server...")
|
||||
|
||||
// Close everything
|
||||
|
||||
if logFile != nil {
|
||||
log.Println("Closing log file, giving back log output to console")
|
||||
logFile.Close()
|
||||
log.SetOutput(os.Stdout)
|
||||
}
|
||||
log.Println("Server stopped")
|
||||
os.Exit(0)
|
||||
}
|
|
@ -0,0 +1,98 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
var apiRootPath string = "/api/v1"
|
||||
|
||||
func Init(router *mux.Router) {
|
||||
|
||||
router.HandleFunc(apiRootPath, apiRoot).Methods("GET")
|
||||
|
||||
routerAuth := router.PathPrefix(apiRootPath + "/auth").Subrouter()
|
||||
routerPage := router.PathPrefix(apiRootPath + "/page").Subrouter()
|
||||
routerUser := router.PathPrefix(apiRootPath + "/user").Subrouter()
|
||||
routerTag := router.PathPrefix(apiRootPath + "/tag").Subrouter()
|
||||
|
||||
routerAuth.HandleFunc("", apiAuth)
|
||||
routerAuth.HandleFunc("/login", apiAuthLogin)
|
||||
routerAuth.HandleFunc("/logout", apiAuthLogout)
|
||||
|
||||
routerPage.HandleFunc("", apiPage)
|
||||
routerPage.HandleFunc("/{folder}", apiPageFolder)
|
||||
routerPage.HandleFunc("/{folder}/{page}", apiPageFolderPage)
|
||||
routerPage.HandleFunc("/{folder}/{page}/content", apiPageFolderPageContent)
|
||||
|
||||
routerUser.HandleFunc("", apiUser)
|
||||
routerUser.HandleFunc("/{name}", apiUserName)
|
||||
|
||||
routerTag.HandleFunc("", apiTag)
|
||||
routerTag.HandleFunc("/{name}", apiTagName)
|
||||
}
|
||||
|
||||
// API ROOT
|
||||
|
||||
func apiRoot(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, "API ROOT")
|
||||
}
|
||||
|
||||
// API Auth
|
||||
|
||||
func apiAuth(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, "API Auth")
|
||||
}
|
||||
|
||||
func apiAuthLogin(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, "API Auth Login")
|
||||
}
|
||||
|
||||
func apiAuthLogout(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, "API Auth Logout")
|
||||
}
|
||||
|
||||
// API Page
|
||||
|
||||
func apiPage(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, "API Page")
|
||||
}
|
||||
|
||||
func apiPageFolder(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
fmt.Fprintf(w, "API Page Folder(%s)", vars["folder"])
|
||||
}
|
||||
|
||||
func apiPageFolderPage(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
fmt.Fprintf(w, "API Page Folder(%s) Page(%s)", vars["folder"], vars["page"])
|
||||
}
|
||||
|
||||
func apiPageFolderPageContent(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
fmt.Fprintf(w, "API Page Folder(%s) Page(%s) Content", vars["folder"], vars["page"])
|
||||
}
|
||||
|
||||
// API User
|
||||
|
||||
func apiUser(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, "API User")
|
||||
}
|
||||
|
||||
func apiUserName(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
fmt.Fprintf(w, "API User Name(%s)", vars["name"])
|
||||
}
|
||||
|
||||
// API Tag
|
||||
|
||||
func apiTag(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, "API Tag")
|
||||
}
|
||||
|
||||
func apiTagName(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
fmt.Fprintf(w, "API Tag Name(%s)", vars["name"])
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
const CONFIG_FILE_NAME string = "./config.json"
|
||||
const CONFIG_FILE_DEFAULT_CONF string = `{
|
||||
"Database": {
|
||||
"Host": "localhost",
|
||||
"Port": "3306",
|
||||
"Database": "Chromagies",
|
||||
"User": "chromagies_user",
|
||||
"Password": "12345678"
|
||||
},
|
||||
|
||||
"Logger": {
|
||||
"Output": "./log.txt"
|
||||
},
|
||||
"Server": {
|
||||
"Port": "12345"
|
||||
}
|
||||
}`
|
||||
|
||||
type Database struct {
|
||||
Host string
|
||||
Port string
|
||||
Database string
|
||||
User string
|
||||
Password string
|
||||
}
|
||||
|
||||
type Logger struct {
|
||||
Output string
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
IP string
|
||||
Port string
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Database Database
|
||||
Logger Logger
|
||||
Server Server
|
||||
}
|
||||
|
||||
var config Config
|
||||
|
||||
func ReadConfigFile() error {
|
||||
log.Println("Reading config file")
|
||||
file, err := os.Open(CONFIG_FILE_NAME)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
err = json.NewDecoder(file).Decode(&config)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
log.Println("Reading config OK")
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateConfigFile() error {
|
||||
log.Println("Creating config file")
|
||||
file, err := os.Create(CONFIG_FILE_NAME)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = file.WriteString(CONFIG_FILE_DEFAULT_CONF)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetConfig() Config {
|
||||
return config
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"chromagies/src/config"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var db *sql.DB
|
||||
var dbConfig config.Database
|
||||
var dbPath string
|
||||
|
||||
func Init(conf config.Config) {
|
||||
dbConfig = conf.Database
|
||||
dbPath = fmt.Sprintf("%s:%s@(%s:%d)/%s?parseTime=true", dbConfig.Host, dbConfig.Password, dbConfig.Host, dbConfig.Port, dbConfig.Database)
|
||||
}
|
||||
|
||||
func openDB() {
|
||||
var err error
|
||||
|
||||
if db != nil {
|
||||
db.Close()
|
||||
db = nil
|
||||
}
|
||||
|
||||
db, err = sql.Open("mysql", dbPath)
|
||||
if err != nil {
|
||||
|
||||
}
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 434 KiB |
Loading…
Reference in New Issue